The log
function in the dw::Core
module allows to log a value or an expression. The function returns the input unchanged. This means we can wrap our code with the log
function and the code is still executed as is, but also logged in a system log. As an extra argument we can specify a String
value that will be a prefix to the expression value in the logging output. The fact that the input is also returned makes it very easy to add the log
function to our DataWeave expressions.
Continue reading →
In a previous post we learned we can turn a string into a string with kebab casing using dasherize
from the dw::core::Strings
module. If we want to turn a string into a string with camel casing we can use the underscore
function. The underscore
function will replace spaces, dashes and camel-casing with underscores, which makes the result snake-casing. Any uppercase characters are transformed to lowercase characters.
Continue reading →
The dw::core::Strings
module has useful functions for working with string values. One of the functions is dasherize
. The function takes a string argument and replaces spaces, underscores and camel-casing into dashes. The resulting string value with hyphens is also called kebab-casing. The dasherize
function also turns any uppercase character to lowercase.
Continue reading →
DataWeave has some very nice features to transform data objects. One of those nice features is the update
operator. With the update
operator we can change values of keys in an object using a very concise syntax. We don’t have to go through all keys and create a new object, but we can pinpoint the exact key and change the value. To get the correct key we use selectors. Once we have the key we can set a new value. We can define a variable to contain the current value if we want to use it to define a new value. Also is it possible to add a condition that needs to be true to change the value. Finally the update
operator supports upserting a value if the key might not exist yet.
Continue reading →
DataWeave has a nice language feature called literal types. Literal types are types with a single predefined values and can be defined using a String
, Number
or Boolean
value. So the value of a literal type is a fixed value. We can combine multiple literal types into a new type using a union type to define an enumaration in DataWeave. The enumaration can only be one of the literal types used to define it.
Together with overloaded functions literal types are very useful. We can define a function where one of the input arguments is a literal type to define specific behaviour based on the literal type. Then we can overload the function for other literal types with different behaviour. DataWeave will make sure the correct function is called based on the value of the input argument and how it matches to the literal type value.
Continue reading →
The dw::core::Strings
has a lot of functions to deal with strings. One of the functions is ordinalize
that takes a number as argument and returns the ordinal value as string.
Continue reading →
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 →
To calculate the modulo of two numbers in DataWeave we can use the mod
function from the dw::Core
module. We provide two arguments where the first argument is the number that needs to be divided by the number provided as second argument. The number that remains after a division of the input arguments is returned as a result.
Continue reading →
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 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 →
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 →
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 →