Archive: June 2022

Groovy Goodness: Using The Switch Expression

Posted on by  
Hubert Klein Ikkink

Groovy supports more classifiers for a switch case statement than Java. Since Groovy 4 we can use switch also as an expression. This means the switch statement returns a value without having to use return. Instead of using a colon (:) and break we use the notation for a case. We specify the value that the switch expressions returns after . When we need a code block we simply put the code between curly braces ({…​}).

Continue reading →

DataWeave Delight: Check Type Of Value

Posted on by  
Hubert Klein Ikkink

To check if a value is of a certain type in DataWeave we must use the is operator. We must specify the type after the is operator and the value before the is operator. For example to check if the value 42 is a Number we write 42 is Number. The result is a Boolean that is either true or false. In the previous example the result is true.

Continue reading →

DataWeave Delight: Unzipping Arrays

Posted on by  
Hubert Klein Ikkink

In a previous blog post we learned about the zip function. DataWeave also gives us the unzip function that will do the opposite for an array with arrays. The input argument of the unzip function is an array where the elements are also arrays. This could be created by the zip function or just defined as data structure directly. The unzip function will take from each array the same index element and return it as an array with the index elements. For example with the input array [[1, "A"], [2, "B"]] will be unzipped to [[1, 2], ["A", "B"]]. When the number of elements in the arrays that need to unzipped are not equal, the unzip function will only return the elements from the index with the most elements.

Continue reading →

DataWeave Delight: Zipping Arrays

Posted on by  
Hubert Klein Ikkink

DataWeave has a zip function in the dw::Core module. The function will merge two arrays into a new array. Each element in the new array is also an array and will have a value from the two original arrays from the same index grouped together. So for example we have an input array ["A", "B"] and another input array [1, 2]. The result of the zip function will be [["A", 1], ["B", 2]]. The size of the resulting array is the same as the minimal size of both input arrays. Any value from an array that cannot be merged is simply ignored and left out of the resulting array.

Continue reading →

DataWeave Delight: Measure Function Duration With time And duration Functions

Posted on by  
Hubert Klein Ikkink

To measure the time it takes to execute a function in DataWeave we can use the time and duration functions from the module dw::util::Timer. Both functions take a zero argument function that needs to be executed as argument (() → T). But the output of the functions is different. The time function returns a TimeMeasurement object that has a result key containing the result of the function we passed as argument. We also get a start key that has the date and time value when the function gets executed. And finally we have the end key that stores the date and time value when the function is finished. To calculate the total duration time of the function we could use the start and end keys, but when we want the duration time we can better use the duration function. The duration function returns a DurationMeasurement object with also a key result that has the output of the function that is executed. The other key is time and contains the time it took for the function to be executed in milliseconds.

Continue reading →

DataWeave Delight: Partition An Array

Posted on by  
Hubert Klein Ikkink

In DataWeave we can partition the items in an array using a predicate function by using the partition function from the dw::core::Arrays module. The function takes an array as first argument and a predicate function as second argument. The predicate function should return true or false for each item of the array. The result is an object with the key success containing all items from the array that returned true for the predicate function and a key failure for the items that returned false.

Continue reading →

DataWeave Delight: Splitting An Array Or Object

Posted on by  
Hubert Klein Ikkink

The module dw::core::Arrays has extra functions that are useful when working with arrays in DataWeave. In this post we will look at the functions splitAt and splitWhere. We can use these functions to split an array into two arrays. The result of both functions is actually a Pair type, which is defined as an object with the keys l and r. An example is { "l": 1, "r": 2 } which we can read as the left side of the pair has value 1 and the right side of the pair has value 2. The result of the splitAt and splitWhere function will have one part of the split array in the left side of the pair and the rest of the array in the right side.

Continue reading →

DataWeave Delight: Importing Functions With An Alias

Posted on by  
Hubert Klein Ikkink

When we want to reference functions from other modules than the dw::Core module we must us the import statement. We can use the as keyword to map a function name that we want to import to another name that we want to use in our DataWeave code. We provide an alias for the function that we can use in our code. This could be useful if we think our code gets more readable in a certain context by using different function names, but still want to use the original function implementation. Next to providing an alias for function names, we can also provide an alias for a module name, when we use the import <ModuleName> as <NewModuleName> statement. In our DataWeave code we can then refer to the new alias to call functions from the module.

Continue reading →

DataWeave Delight: Reversing the items in an array

Posted on by  
Hubert Klein Ikkink

To reverse the order of the items in the array we have two possible solutions in DataWeave. The first solution uses the index operator to access items in an array ([…​]). We can define a range as value for the index operator to get all elements that have a index value that is part of the range. For example we could use [1 to 3] to get the second up to the fourth items from the array. The value -1 can be used to indicate the last index in the array, so we could get a new array where the items are in reversed order by using the range [-1 to 0].

Continue reading →

DataWeave Delight: Convert string value to a boolean

Posted on by  
Hubert Klein Ikkink

If we need to convert a string value "true" or "false" to a boolean we can use the toBoolean function from the dw::util::Coercions module. The function will return a boolean true if the string value is "true", mixed casing is allowed. And the function returns false for a mixed casing string value of "false". Any other string value will throw an exception and will not return a boolean value.

Continue reading →

shadow-left