DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Import a Function/Module in Dataweave
  • Java: A Time-Tested Programming Language Still Going Strong
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices

Trending

  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • How to Write for DZone Publications: Trend Reports and Refcards
  1. DZone
  2. Coding
  3. Languages
  4. Dynamically Evaluate Dataweave Scripts

Dynamically Evaluate Dataweave Scripts

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

By 
Vikalp Bhalia user avatar
Vikalp Bhalia
·
Updated Jul. 20, 20 · Analysis
Likes (3)
Comment
Save
Tweet
Share
14.3K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Java (programming language) Use case Object (computer science) EGS (program) Snippet (programming) Coding (social sciences) dev optimization

Opinions expressed by DZone contributors are their own.

Related

  • Import a Function/Module in Dataweave
  • Java: A Time-Tested Programming Language Still Going Strong
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Memory Optimization and Utilization in Java 25 LTS: Practical Best Practices

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook