DataWeave Overtaking MEL in Mule 4

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.

MEL OR 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 Header Content

DataWeave 1.0DataWeave 2.0
%dw 1.0%dw 2.0
%outputoutput
%varvar
%functionfun
%namespacens

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" }

Pattern Matcher:

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 }

Removing Coercion:

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: object
Mule 4 Example: DataWeave 2
var payload = {a: 1, b:2} --- payload.&a}

Type Names

DataWeave 1 payload.foo as :string
payload.foo as String payload.foo as String

Operators Are Now Functions

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)

Intermediate State:

In Mule 3, you cannot access XML or Json object directly using MEL but now you can using DataWeave.

JSON Payload
Mule 3 Example
Mule 4 Example

Session Properties/Outbound Properties/Inbound Properties:

Transport 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..

Accessing Context Variables

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.

Traits Are Functions:

  • DataWeave 1 payload is: empty

  • DataWeave 2 isEmpty(payload)

Additional Capabilities

  • You can type reusable functions

  • Write Multi-line comments

  • Calling Java code easily

  • Multipart, URL-encoded text readers and writers

A Little More on DataWeave

  • Imports and modules: You can now import scripts, qualifying it to be used again by sharing the code.
  • Java interoperability: Static methods can be executed through DataWeave.
  • New data formats: We can add text/plain, application/x-www-form-urlencoded, and provide multipart support
  • Type system: You can indicate the type for functions and variables with type inference helping you out
  • Simplified syntax: Everything is a function and if/else clauses are now the norm

Better Application Configurability with Royal Cyber

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

  1. Justin jeosep says:

    Great Article On Mule. I really appreciate the blog. Thanks for sharing, Such a nice Information. Please keep sharing.

  2. Mule-Soft says:

    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.

Leave a Reply