{{announcement.body}}
{{announcement.title}}

Dynamically Evaluate Dataweave Scripts

DZone 's Guide to

Dynamically Evaluate Dataweave Scripts

This article demonstrates how to use Dynamic Evaluate Component in Mule 4 to execute dataweave scripts dynamically.

· Integration Zone ·
Free Resource

This article demonstrates how to use Dynamic Evaluate Component in Mule 4 to execute dataweave scripts dynamically.

It is a very common use case where a developer may have to execute different dwl scripts (account.dwl/customer.dwl  in this case) depending on the input parameter.

dw/customer.dwl:

Java
 




x


 
1
{
2
 "customer":{
3
   "id":payload.customerId,
4
   "name": payload.customerName,
5
   "address": payload.customerAddress,
6
   "mobile": payload.customerMobile,
7
   "accountId": payload.customerAccountId,
8
   "category": payload.customerCategory
9
 } 
10
}



dw/account.dwl:

Java
 




x


 
1
{
2
  "account":{
3
    "id": payload.accountId,
4
    "name": payload.accountName,
5
    "address": payload.accountAddress,
6
    "customerId": payload.accountOwnerId,
7
    "startDate": payload.accountStartDate,
8
    "renewalDate": payload.accountRenewalDate,
9
    "subscriptionType": payload.accountSubscription
10
  }
11
}



The First thing that will hit our mind is a Choice router. But is it an optimized way? What if in the future we may have more objects? We will have to update the code if we use a choice router.

But using Dynamic Component Evaluator or dataweave custom module we can avoid it.

Using Dynamic Component Evaluator

Below are the steps:

Create a dwl file that sets the script name in a variable based on the input parameter.

Java
 




xxxxxxxxxx
1
11


 
1
<ee:transform>
2
  <ee:variables ><ee:set-variable variableName="script" >
3
   <![CDATA[
4
      %dw 2.0
5
      output application/java 
6
      var fileName= "dw/$(vars.inputParameter).dwl"
7
      ---
8
      readUrl("classpath://" ++ fileName,"text/plain")
9
    ]]>
10
  </ee:set-variable> </ee:variables>
11
</ee:transform>



This step should give you the dwl file full path in the script variable. Eg. dw/account.dwl or dw/customer.dwl.

  1. Use the script variable in the Dynamic Evaluate Component
    <ee:dynamic-evaluate doc:name="Dynamic Evaluate"

expression="#[vars.script]"/>

In the future, if we have more objects then add the <object>.dwl file in the resource/dw folder.

Sample flow:

sample flowd

Using Dataweave Custom Module

Below are the steps:

1. Define a custom module for customer and account object. Refer below code snippet:

Java
 




xxxxxxxxxx
1
12


 
1
%dw 2.0
2
var account = (acc: Object) -> {
3
    account: {
4
        id: acc.accountId,
5
        name: acc.accountName,
6
        address: acc.accountAddress,
7
        customerId: acc.accountOwnerId,
8
        startDate: acc.accountStartDate,
9
        renewalDate: acc.accountRenewalDate,
10
        subscriptionType: acc.accountSubscription
11
    }
12
}



2. Import the modules in the transform message component and invoke the function we created:

Java
 




xxxxxxxxxx
1


1
%dw 2.0
2
output application/json
3
import dw::account as Acc
4
import dw::customer as Cust
5
---
6
if (vars.inputParameter == "customer") Cust::customer(payload) else Acc::account(payload)



Happy coding!

Topics:
dataweave, dwl, integration, mule 4, mulesoft

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}