Part 2: DataWeave 2.0 Syntax Changes in Mule 4 Beta

September 21 2017

dataweave mule 4

This post was written by one of the stars in our developer community, Manik Magar 

In my last post, I discussed DataWeave 2.0 header and body (iterative functions) changes in Mule 4 Beta. In this post, I will dive deeper into the topic by discussing operator changes (within the body) as well as other changes.

DataWeave 2.0 Operator Changes

As a language, DataWeave 1.0 defines many operators like isuppertypeOf, etc. which help in transformations. In DataWeave 2.0, there are several changes to the way operators are used in DataWeave 2.0.

1.1 Operators are now Functions

In DataWeave 2.0, all of these operators are now made as functions. What that means is they should be called like any other function calls, and with their arguments using parenthesis.

Consider the below DataWeave 1.0 and 2.0 code listings, which show the syntax for operators/functions and produce the same results.

Listing 2.2.1.A DataWeave 1.0 Operators

Listing 2.2.1.B DataWeave 2.0 Operators

1.2 Range Selection

DataWeave 1.0 uses .. operator to select ranges. This has been replaced with a new to operator.

Consider the DataWeave 1.0 and 2.0 code listings below, which show the syntax for operators/functions and produce the same results.

Listing 2.2.2.A DataWeave 1.0 Range Selector

Using dynamic ranges with .. was very error-prone and not straightforward. This throws an error in DataWeave 1.0.

Listing 2.2.2.B DataWeave 2.0 Range Selector

Using dynamic variables to define range is straightforward and easy! .. is still a valid descendants selector for DataWeave 2.0.

1.3 Conditional Logic

DataWeave 1.0 uses whenunless (probably rarely used) and otherwise in order to implement conditional logic. All of these keywords have been replaced by if and else in DataWeave 2.0.

Consider the DataWeave 1.0 and 2.0 code listings below, which show the syntax for operators/functions and produce the same results.

Listing 2.2.3.A DataWeave 1.0 Conditional Logic

Listing 2.2.3.B DataWeave 2.0 Conditional Logic

Nested conditionsif .. else if .. else feels so familiar that of in Java!

1.4 Binary functions

Binary functions are the functions need two arguments. In DataWeave 1.0, they have a notation of argument1 function argument2. In DataWeave 2.0, there is now an added alternate notation to all of these binary functions, making them really look like function calls.

Consider the DataWeave 2.0 script below, where all key1 like calls are valid for both DataWeave versions and key2 like calls are new notations added in DataWeave 2.0.

Listing 2.2.4.A DataWeave Binary Functions

Other DataWeave Syntax Changes

Besides the changes outlined above and in my last post, there are also a few other syntax changes that have been made to DataWeave 2.0. Here are some of those changes:

  • Removed : from all type names. They are now written with first upper case letter i.e Object instead of :object
  • Added a new key-value pair selector .& that returns all matching key-values as a single object. For example, {"firstName": "MM1", "addr": "addr1", "addr": "addr2"}.&addr will return an object {"addr": "addr1","addr": "addr2"}
  • Namespace prefixes can no longer contain the character -
  • New namespace selector (.#) returns the namespace used.
  • New supported syntax for match when using arrays [head ~ tail]

The power of DataWeave

I looked at the syntax changes in DataWeave 2.0 compared them to DataWeave 1.0 and if you are thinking that the changes covered in my two posts are the only changes, then you are missing lot of new features!

As I mentioned earlier, DataWeave 2.0 is the default expression language in Mule 4 Beta. This opens multiple opportunities to leverage DataWeave’s power throughout your Mule Flow.

Apart from that, Mule 4 Beta also introduced concepts like reusable modules, which can contain your common functions, operators, etc. Many new functions and operators were also added; this includes features like importing Java classes and calling static methods from within DataWeave, which enrich the overall DataWeave experience.

Finally, I strongly, suggest you go through the DataWeave documentation (reference links are at the bottom) and make yourself familiar with DataWeave 2.0!

Keep learning more about Mule 4 Beta and enjoy the ride, and let me know if you have any thoughts or questions in the comments!

Interested in learning more?

This article first appeared on JavaStreets.