Since the beta version of Mule 4 to the most recent stable release, we have been hearing a lot of Mule 4 and what has changed in it from the previous version. Let’s take some time and learn some of the changes that have occurred in DataWeave.
If you are a Mule 3 developer you would wonder about the heading that both are part of Mule 3 and serve separate purposes so it’s not fair to compare, but in Mule 4 it is fair to compare because, you do not have MEL (Mule Expression Language) in Mule 4, DataWeave is the default expression language for coding and as well as transformation.
Now, you do not need to learn two separate languages for Mule, you can use one standardized language to serve all your needs i.e. DataWeave Expression Language.
So, Let us discuss some of the changes that have occurred in DataWeave in Mule
DataWeave 1.0 | DataWeave 2.0 |
%dw 1.0 | %dw 2.0 |
%output | output |
%var | var |
%function | fun |
%namespace | ns |
When Otherwise
The when otherwise statement is replaced by if else, for example:
Mule 3 Example: DataWeave 1{ orderStatus: "complete" when flowVars.purchaseOrderStatus == "C" otherwise "incomplete" }Mule 4 Example: DataWeave 2
{ orderStatus: if(vars.purchaseOrderStatus == "C") "complete" else "incomplete" }
For pattern matching now you can use case and else keyword instead of default.
Mule 3 Example: DataWeave 1'world' match { :string -> true, default -> false }Mule 4 Example: DataWeave 2
'world' match { case is String -> true else -> false }
DataWeave 2.0 has removed coercion and a new selector & is presented to select key-value pairs.
Mule 3 Example: DataWeave 1%var payload = {a: 1, b: 2} --- payload.a as: objectMule 4 Example: DataWeave 2
var payload = {a: 1, b:2} --- payload.&a}
DataWeave 1 | payload.foo as :string |
payload.foo as String | payload.foo as String |
DataWeave 1 | sizeOf payload filter $.age > 30 |
DataWeave 2 - Function Syntax | sizeOf(filter(payload, (value) → value.age > 30))) |
DataWeave 2 - Shortcut Syntax | sizeOf(payload filter $.age > 30) |
In Mule 3, you cannot access XML or Json object directly using MEL but now you can using DataWeave.
JSON PayloadTransport barriers do not exist in Mule 4 so session properties have been removed. Outbound properties have been removed but you can set outgoing status code, response or headers using DataWeave Expression. Inbound properties are removed and a new feature attribute is introduced..
DataWeave 1.0 | DataWeave 2.0 |
MEL | DataWeave |
flowVars | Renamed to vars |
sessionVars | Removed |
exception | Replaced: exceptions within a flow are now represented by a Mule Error you can access with the error variable. |
message.inboundProperties | Removed: |
message.outboundProperties | Removed |
message.inboundAttachments and message.outboundAttachments | Removed |
message.id, message.rootId and message.replyTo | Removed |
message.dataType | Renamed to dataType. |
DataWeave 1 payload is: empty
DataWeave 2 isEmpty(payload)
You can type reusable functions
Write Multi-line comments
Calling Java code easily
Multipart, URL-encoded text readers and writers
Mule 4 combines expressions and alterations in a single, modern, consolidated language that has performance at its core. For more information on MuleSoft and to bring improved abilities in configuring and management of the application development process email us at [email protected] or visit www.royalcyber.com.
2 Comments
Great Article On Mule. I really appreciate the blog. Thanks for sharing, Such a nice Information. Please keep sharing.
I am feeling motivated and now work harder to start the career in Mule-Soft, hope will get similar success. Thanks for sharing your Mule-Soft experience.