在 Dataweave 1.0 中创建全局函数

Create Global Function in Dataweave 1.0

我是 运行 %dataweave 1.0。我需要创建一个函数,该函数应该根据从我的 Mule 流中的 API 调用接收到的特定有效载荷替换一些代码值。 示例 API 输出如下:当前存储在会话变量中。

{
    "CodeMaster": {
        "PrimaryCodes": {
            "PrimarySpecCodes": {
                "ABC": {
                    "code": "Alpha Bravo Charlie",
                    "target": "SALES",
                    "Field": "PrimarySpecCodes"
                },
                "TUV": {
                    "code": "Tango Umbrella Victor",
                    "targetSystemCode": "SALES",
                    "targetCodeFieldName": "PrimarySpecCodes"
                },
                "XYZ": {
                    "code": "X-Ray Yankee Zulu",
                    "targetSystemCode": "SALES",
                    "targetCodeFieldName": "PrimarySpecCodes"
                }
            }
        }
    }
}

如上所示,我需要创建一个函数,用值 "Alpha Bravo Charlie"、"Tango Umbrella Victor" 和 [=22 替换主要负载中的代码(如 ABC、TUV、XYZ) =] 分别。在主要有效负载中,我有如下要替换的数据:

"PY123":
  {
      "Country": "GB",
      "Status": "ACTIVE",
      "Flag": null,
      "SpecCodes": [
        {
          "PrimarySpecCodes": "ABC"
        },
        {
          "PrimarySpecCodes": "TUV"
        },
        {
          "PrimarySpecCodes": "XYZ"
        }
      ]
    }

如何创建一个函数来替换代码值。如果有更好的替换代码的解决方案,请提出。提前致谢。

%dw 1.0
%output application/json

%function buildLookup(codes)
  codes mapObject {($$): $.code}

%var codeLookup = buildLookup(sessionVars.code.CodeMaster.PrimaryCodes.PrimarySpecCodes)

%var verboseCodes = payload.PY123.SpecCodes map (code) ->
  code mapObject {($$): codeLookup[$]}
---
{
  "PY123" : {
    "Country"   : "GB",
    "Status"    : "ACTIVE",
    "Flag"      : null,
    "SpecCodes" : verboseCodes
  }
}