Dynamic Evaluate Implementation In Mule 4

Authors: Pankuri Bansal & Nikhil Kumar

In this blog, we will discuss the dynamic evaluation component in MuleSoft. Let’s have a look.

Dynamic Evaluate

Our core component plays a critical role in evaluating expressions that generate additional scripts, and then further evaluates those scripts to produce the ultimate result.

Advantages:
  • Dynamic evaluation can be a game-changer when it comes to selecting the appropriate DataWeave script (either variables, payload, or attributes ) from multiple DWL files based on specific conditions passed at runtime instead of hardcoding it through Transform Message Components.
  • This feature adds a level of flexibility and efficiency to your data integration processes, enabling you to streamline your workflow with ease.
  • With dynamic evaluation, you have the power to dynamically choose and execute the most suitable DWL file, saving precious time and effort in your development tasks.
Implementation:
  • Create a new Mule application.
  • Drag and Drop the HTTP Listener and configure that.
  • Create multiple DWL files within the “src/main/resources” directory. Specifically, you can create a separate folder dedicated to DWL files for better clarity and organization.
  • This is the payload we are taking before making 3 different DWL files.
[
    {
 	 "name": "Pankuri",
        "muleCompany": "Apisero",
"salesPerson": "Vinod"
    },
    {
 	 "name": "Nikhil",
        "muleCompany": "Apisero",
"salesPerson": "Simran"


    }
]

Now implementing 3 different DWLs

Example:

  • mule-data. dwl:- only took the name and muleCompany field from Payload
%dw 2.0
output application/json
---
payload map ($ - "salesPerson")
  • sales-data. dwl:- only took name and salesPerson field from Payload
%dw 2.0
output application/json
---
payload map ($ - "muleCompany")
  • employee-data. dwl
%dw 2.0
output application/json
---
payload map ($ - "muleCompany" - "salesPerson" ++
{
age: "23"
})
  • Drag and drop the ‘Set Variable’ component and store the ‘queryParam’ in a variable named ‘vqueryparams,’ which is passed when making a request from Postman.
  • Drag and drop the ‘Transform Message’ component and perform the transformation on DWL files stored in the variable named ‘vDwl’ for dynamic calls.
  • So that the variable ‘vDwl’ can evaluate data with the payload coming from Postman.
%dw 2.0
output application/java
var file = "dwl/$(vars.vqueryParams).dwl"
---
readUrl("classpath://" ++ file,  "text/plain")

In this, we call the respective dwl files based on query Params.

Example: If URL hits with queryParam = sales-data, then ‘sales-data. dwl’ data would be taken.

Note:
  • If developers use this as a payload instead of storing it as a variable, the payloads will be overridden, resulting in an error in the Dynamic Evaluate Component.

Error Example:

""You called the function 'map' with these arguments: 
  1: String ("%dw 2.0\r\noutput application/json\r\n---\r\npayload map (\$ - \"muleCompany...)
  2: Function (($:Any, $$:Any) -> ???)

But it expects arguments of these types:
  1: Array
  2: Function


4| payload map ($ - "muleCompany")
           ^^^
Trace:
  at main (line: 4, column: 9)" evaluating the expression: "%dw 2.0

output application/json

---

payload map ($ - "muleCompany")"."

  • Now, drag and drop the Dynamic Evaluate component from the palette.
Configuration:

Parameters (if any): You can easily set parameters for the DataWeave script by specifying key-value pairs. This allows the script to effectively evaluate and process the data according to your specific needs.

Expression: Specify the variable or payload where you transform the DWLs dynamically (as in our case, we made the previous transform using the payload).

Target: If a developer is looking to store the response returned by a component, you can easily assign a variable name of your choice. This way, you can conveniently access and manipulate the response data stored within that same component as the variable.

  • Drag and drop the Logger to print the Response returned on the Console.
Demo Application:
XML:
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="2578e1a7-e7f8-461d-ab47-38e3709ccb09" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<flow name="poc-dynamic-evaluateFlow" doc:id="fad34302-bb59-49f4-b5a8-ef987dce4191" >
		<http:listener doc:name="Listen /demo endpoint" doc:id="3b12d006-d86f-46ee-a070-63c9563d39cc" config-ref="HTTP_Listener_config" path="/demo"/>
		<set-variable value="#[attributes.queryParams.object]" doc:name="Set Var vqueryParams" doc:id="1bf1b598-cfb4-416a-b44d-cd8b1f0e9ac4" variableName="vqueryParams"/>
		<ee:transform doc:name="Transform Dynamic Dwl's" doc:id="9372127b-ad14-4016-b6be-f77cc61c10f0" >
			<ee:message >
			</ee:message>
			<ee:variables >
				<ee:set-variable variableName="vDwl" ><![CDATA[%dw 2.0
output application/java
var file = "dwl/"++ vars.vqueryParams ++ ".dwl"
---
readUrl("classpath://" ++ file,  "text/plain")
]]></ee:set-variable>
			</ee:variables>
		</ee:transform>
		<ee:dynamic-evaluate doc:name="Dynamic Evaluate" doc:id="8b5e6f6e-c4ab-46db-8ea5-77c0ae7898b8" expression="#[vars.vDwl]" target="vRepsonse"/>
		<logger level="INFO" doc:name="Log Response data" doc:id="7f83e612-6fe2-4a40-adc2-b5070b7ac34f" message='#["Response data" : vars.vRepsonse]'/>
	</flow>
</mule>

Testing:

Postman Collection:
curl --location --request GET 'http://localhost:8081/demo?object=sales-data' \
--header 'Content-Type: application/json' \
--data '[
    {
        "name": "Pankuri",
        "muleCompany": "Apisero",
        "salesPerson": "Vinod"
    },
    {
        "name": "Nikhil",
        "muleCompany": "Apisero",
        "salesPerson": "Simran"
    }
]'
  • Now, it’s apparent that we have successfully passed the query parameter as ‘sales-data’. This allows us to retrieve specific data related to sales, so the sales file, named ‘sales-data.dwl,’ has been selected by DWL.”
  • To enhance the efficiency of our component, we have conveniently placed the variable (vResponse) in the same location as the target in the same component. This ensures that all relevant data is stored together, making it easily accessible and streamlining the overall functionality of our component.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.