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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Fun Is the Glue That Makes Everything Stick, Also the OCP
  • JavaFX Goes Mobile
  • Five Java Books Beginners and Professionals Should Read
  • Conditional Breakpoints: A Guide to Effective Debugging
  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.

Vikalp Bhalia user avatar by
Vikalp Bhalia
CORE ·
Jul. 20, 20 · Analysis
Like (3)
Save
Tweet
Share
10.44K 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.

Trending

  • Fun Is the Glue That Makes Everything Stick, Also the OCP
  • JavaFX Goes Mobile
  • Five Java Books Beginners and Professionals Should Read
  • Conditional Breakpoints: A Guide to Effective Debugging

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: