mulesoft 中的 dataweave 1.0 到 2.0 迁移
dataweave 1.0 to 2.0 migration in mulesoft
我正在尝试将下面的数据编织从 1.0 转换为 2.0,但我尝试过的所有操作都出现以下错误,例如评估表达式错误、无效输入“reduce”和替换。
Mule 3 数据编织 1.0:
%dw 1.0
%output application/xml
%var basicPolicy = payload.DTOApplication.DTOBasicPolicy
%var insuredInfo = payload.DTOApplication.DTOInsured
%var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
%var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
%var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
%var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
%var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
%var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0..4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "") when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
otherwise "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
输入负载:https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml
预期输出:https://github.com/Manikandan99/rate-dtostep/blob/master/policy_response.xml
我试过 dataweave 2.0:
%dw 2.0
output application/xml
var basicPolicy = payload.DTOApplication.DTOBasicPolicy
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "") when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
otherwise "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
关于如何在 dataweave 2.0 中编写相同的内容有什么想法吗?
为@AnuragSharma 的评论添加更多要点
- 在你的 dw 中你有
GLClassCode == primaryExposure
, primaryExposure 不存在于你的输入有效负载中因此这将导致空值并且你不能使用 replace
函数以及 null
- 由于您正在尝试转换为 number ,因此您可以提供
default
值。这里我设置为0.
- 在输出
中根据需要添加了encoding="UTF-8"
为了测试您的代码,我已将 GLClasscode 作为 CANDIS 传递给您,就像您在有效负载中所拥有的那样。
注意 - > 请检查括号。
%dw 2.0
output application/xml encoding="UTF-8"
var basicPolicy = payload.DTOApplication.DTOBasicPolicy
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default [] reduce ($$++$) replace "$" with "" replace "," with "" default 0 as Number
var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default [] reduce ($$++$) replace "$" with "" replace "," with "" default 0 as Number
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: if (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null)
(payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "")
else if (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null)
(payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "")
else "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
输出
<?xml version='1.0' encoding='UTF-8'?>
<Policy>
<PolType>CNPK</PolType>
<Zip>80003</Zip>
<EffDate>20211117</EffDate>
<ExpDate>20221117</ExpDate>
<EBCov>Yes</EBCov>
<PolFeeOR2/>
<PolFeeOR/>
<LiabExp>2000000</LiabExp>
<ProdExp>0</ProdExp>
</Policy>
我正在尝试将下面的数据编织从 1.0 转换为 2.0,但我尝试过的所有操作都出现以下错误,例如评估表达式错误、无效输入“reduce”和替换。
Mule 3 数据编织 1.0:
%dw 1.0
%output application/xml
%var basicPolicy = payload.DTOApplication.DTOBasicPolicy
%var insuredInfo = payload.DTOApplication.DTOInsured
%var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
%var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
%var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
%var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
%var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
%var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0..4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "") when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
otherwise "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
输入负载:https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml
预期输出:https://github.com/Manikandan99/rate-dtostep/blob/master/policy_response.xml
我试过 dataweave 2.0:
%dw 2.0
output application/xml
var basicPolicy = payload.DTOApplication.DTOBasicPolicy
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "$" with "" replace "," with "") as :number)
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "") when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
otherwise "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
关于如何在 dataweave 2.0 中编写相同的内容有什么想法吗?
为@AnuragSharma 的评论添加更多要点
- 在你的 dw 中你有
GLClassCode == primaryExposure
, primaryExposure 不存在于你的输入有效负载中因此这将导致空值并且你不能使用replace
函数以及 null - 由于您正在尝试转换为 number ,因此您可以提供
default
值。这里我设置为0. - 在输出 中根据需要添加了
encoding="UTF-8"
为了测试您的代码,我已将 GLClasscode 作为 CANDIS 传递给您,就像您在有效负载中所拥有的那样。
注意 - > 请检查括号。
%dw 2.0
output application/xml encoding="UTF-8"
var basicPolicy = payload.DTOApplication.DTOBasicPolicy
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default [] reduce ($$++$) replace "$" with "" replace "," with "" default 0 as Number
var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default [] reduce ($$++$) replace "$" with "" replace "," with "" default 0 as Number
---
{
Policy: {
PolType: basicPolicy.@SubTypeCd default "",
Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
EffDate: basicPolicy.@EffectiveDt default "",
ExpDate: basicPolicy.@ExpirationDt default "",
EBCov: lineProperty.@EquipmentBreakdown default "No",
PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
PolFeeOR: if (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null)
(payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "")
else if (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null)
(payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "")
else "",
LiabExp: primaryClassTotalExposure,
ProdExp: primaryClassPLTotalExposure
}
}
输出
<?xml version='1.0' encoding='UTF-8'?>
<Policy>
<PolType>CNPK</PolType>
<Zip>80003</Zip>
<EffDate>20211117</EffDate>
<ExpDate>20221117</ExpDate>
<EBCov>Yes</EBCov>
<PolFeeOR2/>
<PolFeeOR/>
<LiabExp>2000000</LiabExp>
<ProdExp>0</ProdExp>
</Policy>