There are two types of directives you can use to define a function, through %var (as with variables) using a lambda, or through %function.
Lambdas can be used inside operators such as map, mapObject, etc or they can be assign to a variable. When using them with an operator it can be either named or anonymous.
%dw 1.0
%output application/json
%var toUser = (user) -> {firstName: user.givenName, lastName: user.sn}
---
{
"user" : toUser({ givenName : "Annie", sn : "Point" })
}
{
"user": {
"firstName": "Annie",
"lastName": "Point"
}
}
%dw 1.0
%output application/json
---
users: ["jhon", "petter", "mat"] map ((name) -> upper name)
{
"users": ["JHON","PETTER","MAT"]
}
You can declare functions in the Header and these can be invoked at any point in the Body. You refer to them as you do to any variable or constant: using the form $<function-name>() passing an expression as argument. The result of the expression that is passed as an argument is used in the execution of the function body.
%dw 1.0
%output application/json
%function toUser(user){firstName: user.givenName, lastName: user.sn}
---
{
"user" : toUser({ givenName : "Annie", sn : "Point" })
}
{
"user": {
"firstName": "Annie",
"lastName": "Point"
}
}
From a DataWeave transform, you can trigger the calling of a different flow in your Mule application, and whatever the flow returns will be what the expression returns.
You can do this through the following expression:
lookup(“flowName”,$)
Which takes two parmeters:
-
The name of the flow that must be called
-
The payload to send to this flow, as a map
%dw 1.0
%output application/json
---
{
a: lookup("mySecondFlow",{b:"Hello"})
}
<flow name="mySecondFlow">
<set-payload doc:name="Set Payload" value="#[payload + ' world!' ]"/>
</flow>
{
"a": "Hello world!"
}