Thursday, April 13, 2017

DataWeave 101!

DataWeave! DataWeave! DataWeave! It's fun to do dataweaving....

How to create a lookup function in DataWeave?

%var gendertable = { "Male": "M", "Female": "F", "Unknown": "U"}
- - -
...
genderCode: gendertable[payload.inputGender]
...

How to display a data with a valid xml descriptor?

Input:
<root>
    <Address>
        <country>USA</country>
        <type>
            <reference descriptor="Home">nvm</reference>
        </type>
    </Address>
    <Address>
        <country>PH</country>
        <type>
            <reference descriptor="Work">nvm</reference>
        </type>
    </Address>
</root>

Transform:

Option 1 =

%dw 1.0
%output application/json
---
payload.root.*Address map (
      { homeCountry: $.country } when $.type.reference.@descriptor == "Home" otherwise { homeCountry: "" }
)

Option 2 =

(
  payload.root.*Address 
    filter $.type.reference.@descriptor == 'Home' 
    map {
      homeCountry: $.country
    }
)[0]

Output:

{
    "homeCountry" : "USA"
}


How to use the value to be the key?

Input:

<ns6:ProductListResponse xmlns:ns5="http://www.twcable.com/common/types/v1x0"
  xmlns:ns6="http://www.twcable.com/soa/types/csg/v2x21">
         <ns6:Business>
            <ns6:BusinessUnit>815020000070</ns6:BusinessUnit>
         </ns6:Business>
         <ns6:ProductList>
            <ns6:Count>2</ns6:Count>
            <ns6:Product>
               <ns6:Code>$F</ns6:Code>
               <ns6:AvailableCatalogIdentifier>60060840415</ns6:AvailableCatalogIdentifier>
               <ns6:CatalogIdentifier>600606044</ns6:CatalogIdentifier>
               <ns6:ChildCount>2</ns6:ChildCount>
               <ns6:ParameterCount>1</ns6:ParameterCount>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093417</ns6:Identifier>
                  <ns6:Name>FROM_REFERENCE_NAME</ns6:Name>
                  <ns6:DomainDefinition>N</ns6:DomainDefinition>
               </ns6:Parameter>
            </ns6:Product>
            <ns6:Product>
               <ns6:Code>$H</ns6:Code>
               <ns6:AvailableCatalogIdentifier>60060840416</ns6:AvailableCatalogIdentifier>
               <ns6:CatalogIdentifier>600606045</ns6:CatalogIdentifier>
               <ns6:ParameterCount>2</ns6:ParameterCount>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093453</ns6:Identifier>
                  <ns6:Name>RSTRCT CALLER ID</ns6:Name>
               </ns6:Parameter>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093446</ns6:Identifier>
                  <ns6:Name>BLOCK 900/976</ns6:Name>
                </ns6:Parameter>
           </ns6:Product>
       </ns6:ProductList>
</ns6:ProductListResponse>

Transform:

%dw 1.0
%output application/java
%var products = payload.ProductListResponse.ProductList.*Product
---
{
  Status:"Success",
  Response: {(
    products map {
      ($.CatalogIdentifier): {($.*Parameter map {
          ($.Name): $.Identifier
        }
      )}
    }
  )}
}

Output:

Status:"Success",
Response:{
  600606044 : {
    "FROM_REFERENCE_NAME","6006060093417"
  },
  600606045 : {
    "RSTRCT CALLER ID":"6006060093453",
    "BLOCK 900/976":"6006060093446"
  }
}



How can I filter the objects where "UniqueId": "0"

{
    Header: {
        MsgId: flowVars.msgCId,
        Key: flowVars.key
    },
    Data: {
        Addresses: 
payload.ns0#GetCustomerDataResponse.ns0#OCust.*ns0#OCadRecord[?($.ns0#OCAddrId != "0")]
 map ((oCadRecord , indexOfOCadRecord) -> {
            Type: oCadRecord.ns0#OCAddrUsageCd,
            DescriptionOfOther: oCadRecord.ns0#OCAddrDesc,
            CountryCd: oCadRecord.ns0#OCCntryCd,
            Line1: oCadRecord.ns0#OCStAddrTx,
            Line2: oCadRecord.ns0#OCStAddr2Tx,
            Line3: oCadRecord.ns0#OCStAddr3Tx,
            City: oCadRecord.ns0#OCAddrCityNm,
            State: oCadRecord.ns0#OCAddrStateCd,
            Zip: oCadRecord.ns0#OCPostalCd,
            Zip4: oCadRecord.ns0#OCUsZipSuffCd,
            UniqueId: oCadRecord.ns0#OCAddrId
        })
    }
}




MuleSoft MUnit Cheatsheet!

MUnit!

How to Set Inbound Parameter?

<munit:set payload="#[]" doc:name="Set Message">
   <munit:inbound-properties>
       <munit:inbound-property key="http.query.params" value="#[['param':'a-param']]"/>
   </munit:inbound-properties>
</munit:set>