简体   繁体   English

mulesoft 中的 dataweave 1.0 到 2.0 迁移

[英]dataweave 1.0 to 2.0 migration in mulesoft

I'm trying to convert the below dataweave from 1.0 to 2.0, but everything I've tried gives the following errors like evaluating-expression Error, Invalid input "reduce" and replace.我正在尝试将下面的数据编织从 1.0 转换为 2.0,但我尝试过的所有操作都会出现以下错误,例如评估表达式错误、无效输入“减少”和替换。

Mule 3 Dataweave 1.0:骡子 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
    }   
}

Input payload: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml输入载荷: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml

Expected output: https://github.com/Manikandan99/rate-dtostep/blob/master/policy_response.xml预计 output: https://github.com/Manikandan99/rate-dtostep/blob/master/policy_response.xml

I tried dataweave 2.0:我试过数据编织 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 
    }   
}

Any ideas please on how to write the same in dataweave 2.0?关于如何在 dataweave 2.0 中编写相同内容的任何想法?

Adding few more points to @AnuragSharma's comments在@AnuragSharma 的评论中再添加几点

if-else-if 如果-其他-如果

  1. In your dw you have GLClassCode == primaryExposure , primaryExposure is not present in your input payload hence this will result in a null value and you cant use replace function along with null在您的 dw 中,您有GLClassCode == primaryExposure ,输入有效负载中不存在primaryExposure因此这将导致 null 值,您不能replace function 与 null 一起使用
  2. Since you are trying to cast to number, you can give a default value.由于您正在尝试转换为数字,因此您可以提供一个default值。 Here I have set it to 0.这里我设置为0。
  3. Added encoding="UTF-8" as required in output output中要求添加encoding="UTF-8"

To test your code I have passed GLClasscode as CANDIS as you had that in your payload.为了测试您的代码,我已将GLClasscode 作为 CANDIS传递给您,就像您在有效负载中所拥有的那样。

Note - > Please check your parenthesis as well.注意->请同时检查您的括号。

%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 
    }   
}

Output Output

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM