Introduction
In Mule 4, the mel2 functionalities are replaced by the Dataweave 2 ones. For those who worked in Mule 3, you will find the mule 4 ways of using function modules are much more advanced than the old way (see my post). In this article, I am going to present Mule 4 ways to define and use global functions.
Define Functions In Dataweave Modules
Here is my project layout:
As you can see, I place the function modules in the dir: src/main/resources/modules. And the dataweave function module is defined in CommonFunc.dwl.
1 2 3 4 5 | %dw 2.0 fun concatName(aPerson) = aPerson.firstName ++ ' ' ++ aPerson.lastName fun stringToDateUTC(dateString) = ((dateString as String {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" } >> "UTC" )) as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSS" } |
Use Function Modules
I place the normal dataweave modules in the dir of src/main/resources/dataweave. The usage of the functions is demonstrated at the following dwl file:
1 2 3 4 5 6 7 8 | %dw 2.0 import * from modules::CommonFunc output application/json --- { "Name" : concatName(payload), "CreatedDate" : stringToDateUTC(payload.createdDate) } |
Here are few points:
- import functions
- import modules::CommonFuc
- import * from modules::CommonFunc
- import concatName stringToDateUTC from modules::CommonFunc
- use functions in dwl
- CommonFunc::concatName(payload)
- concatName(payload)
For those who know python, you may find the syntax is very similar between the two languages, in terms of modules, and references.
Testing Data
Here is the complete flow:1 2 3 4 5 6 7 8 9 10 11 | <flow name= "use-functionsFlow" doc:id= "ccb2b5cc-b78f-4db2-9337-f96fed2de729" > <http:listener doc:name= "Listener" doc:id= "e13b3ae1-ac0c-4200-badf-0acb5039efe1" config-ref= "HTTP_Listener_config" path= "/mule4/func/one" > <ee:repeatable-file-store-stream> </ee:repeatable-file-store-stream></http:listener> <ee:transform doc:name= "Use Global Functions" doc:id= "be1466e6-6c0d-44c3-b876-c191b5a0b2a3" > <ee:message> <ee:set-payload resource= "dataweave/useFunctionModules.dwl" > </ee:set-payload></ee:message> </ee:transform> <logger level= "INFO" doc:name= "Logger" doc:id= "9c689ad2-f9bd-47ec-86ba-8d6a7d278e88" message= "#[payload]" category= "mule4-datweave-in-action" > </logger></flow> |
1 2 3 4 5 | { "firstName" : "Gary" , "lastName" : "Liu" , "createdDate" : "2018-07-17T16:18:03+00:00" } |
For Mulesoft ESB Real Time Training Contact Me at muleesbtrainer@gmail.com and 9985333565
ReplyDeleteI will provide real time online training at low price for demo videos visit my youtube channel.
http://youtube.com/channel/UC1lFGEh4xAPJnIm4FWmQBfA
ReplyDeleteThank you so much for your useful information
Mulesoft Online Training
Mulesoft Training in Hyderabad
Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDeleteThanks for Posting such an useful & informative stuff..
ReplyDeletemulesoft training
Thank you for sharing wonderful information with us to get some idea about it.
ReplyDeleteMulesoft Training in Bangalore
Best Mulesoft Online Training
I really liked your blog post.Much thanks again. Awesome.
ReplyDeleteMulesoft Self Learning
Mulesoft Online Course in Hyderabad