Search

Dark theme | Light theme
Showing posts with label DataWeave:Core. Show all posts
Showing posts with label DataWeave:Core. Show all posts

April 7, 2023

DataWeave Delight: Using The log Function

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.

In the following example we use the log function for several use cases:

Source

%dw 2.0

import upper from dw::core::Strings

// Sample object we want to use for logging.
var user = {
    alias: "mrhaki",
    name: {
        firstName: "Hubert",
        lastName: "Klein Ikkink"
    }
}

output application/json
---
{
    // Log a value.
    logValue: log("DataWeave"),

    // Log expression.
    logUpper: log(upper("DataWeave")),

    // Log object property.
    logExpr: log(user.alias),

    // Log with prefix.
    logExprWithPrefix: log("alias", user.alias),

    // Log object.
    logName: log("name", user.name)
}

Output

{
  "logValue": "DataWeave",
  "logUpper": "DATAWEAVE",
  "logExpr": "mrhaki",
  "logExprWithPrefix": "mrhaki",
  "logName": {
    "firstName": "Hubert",
    "lastName": "Klein Ikkink"
  }
}

We see the output simply returns the original object that we passed to the log function. But when we look at a log viewer, for example the console of a Mule application, we see a representation of the object. And if we specified a prefix the prefix is also visible in the logging output.

The previous code returns the following log output:

"DataWeave"
"DATAWEAVE"
"mrhaki"
alias - "mrhaki"
name - { firstName: "Hubert", lastName: "Klein Ikkink" }

Written with DataWeave 2.4.

June 27, 2022

DataWeave Delight: Check Type Of Value

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.

In the following examples we use is on several types:

Source

%dw 2.0

import every from dw::core::Arrays

// Closed object type with two properties alias and name
// that must have the String type.
type User = {|alias: String,name: String|}

// Helper function to check if argument value
// is an Array type and
// only contains elements of type String.
fun checkArrayString(value: Any): Boolean = 
    (value is Array) and
    ((value as Array) every ((element) -> element is String))

// Literal type.
type Language = "DataWeave" | "Java" | "Groovy" | "Clojure"

output application/json
---
{
    string: "DataWeave" is String,
    not_string: 42 is String,
    
    number: 42 is Number,

    // We can also use the Null type to check.
    null_value: null is Null,

    object: {alias: "mrhaki", name: "Hubert A. Klein Ikkink"} is Object,

    // We can check on our own object type.
    user: {alias: "mrhaki", name: "Hubert"} is User,

    // Only the alias property is valid.
    not_user1: {alias: "mrhaki", firstName: "Hubert", lastName: "Klein Ikkink"} is User,

    // User is a closed object so extra properties 
    // don't make it of type User anymore.
    not_user2: {alias: "mrhaki", name: "Hubert", location: "Tilburg"} is User,

    // Type of alias property is not correct.
    not_user3: {alias: 42, name: "Hubert"} is User,
    
    array: ["one", "two"] is Array,

    // To also check the types of the elements
    // we must use an extra check as defined
    // in the checkArrayString helper function.
    array_string: checkArrayString(["one", "two"]),
    not_array_string: checkArrayString([1, 2]),
    not_array: checkArrayString(42),

    // Literal types are supported.
    language: "DataWeave" is Language,
}

Output

{
  "string": true,
  "not_string": false,
  "number": true,
  "null_value": true,
  "object": true,
  "user": true,
  "not_user1": false,
  "not_user2": false,
  "not_user3": false,
  "array": true,
  "array_string": true,
  "not_array_string": false,
  "not_array": false,
  "language": true
}

Written with DataWeave 2.4.

June 21, 2022

DataWeave Delight: Zipping Arrays

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.

In the following code example we use the zip function for different arrays:

Source

%dw 2.0

import take from dw::core::Arrays

var fruit = ["Apple", "Pear", "Banana"]
var prices = [2.30, 1.82, 2.06]

output application/json
---
{
    // Create new array where each element is
    // an array with first element a fruit and
    // second element a price value. 
    zip: fruit zip prices,

    // When we have an array that contains arrays
    // of 2 items (a pair) we can easily turn it into an object
    // with key/value pairs using reduce.
    zipObj: fruit zip prices 
        reduce ((item, acc = {}) -> acc ++ {(item[0]): item[1]}),

    // The resulting array will have no more items
    // then the smallest array that is used with the zip function.
    // In the following example the second array only has 2 items
    // so the resulting array also has 2 items in total. 
    // The fruit "Banana" is now ignored.
    zipMinimal: fruit zip (prices take 2)
}

Source

{
  "zip": [
    [
      "Apple",
      2.30
    ],
    [
      "Pear",
      1.82
    ],
    [
      "Banana",
      2.06
    ]
  ],
  "zipObj": {
    "Apple": 2.30,
    "Pear": 1.82,
    "Banana": 2.06
  },
  "zipMinimal": [
    [
      "Apple",
      2.30
    ],
    [
      "Pear",
      1.82
    ]
  ]
}

Written with DataWeave 2.4.

February 23, 2022

DataWeave Delight: Reversing order of keys in objects

An object in DataWeave consists of key and value pairs. If we want to create a new object sorted by key or value we can use the orderBy function from the dw::Core module. The orderBy function takes as first argument the object. The second argument is a criteria function that can the value and key to determine how to order the object. But if we want to reverse the ordering of the key and value pairs we cannot simply use the orderBy function, because we don't have access to the index value of the key/value pair in the criteria function. We would love to have that index value, so we could negate the index value to reverse the ordering. What we need to do is first turn our object in an array of objects using the pluck function, where each object has our key and value. We can now use the orderBy function on that array of objects to reverse the order. Finally we need to reduce the array of objects into a new object using the reduce function, where the ordering of keys will be reversed.

In the following example we use :

Source

%dw 2.0

var object = {
    name: "Hubert Klein Ikkink",
    alias: "mrhaki",
    city: "Tilburg"
}

output application/json
---
{
    reverseKeys: object 
        // Turn into array: [{name: "Hubert Klein Ikkink"}, ...]
        pluck ((value, key, index) -> {(key): value})

        // Reverse the ordering based on negated index value
        orderBy ((item, index) -> -index) 

        // Transform array back to an object.
        reduce ((item, accumulator = {}) -> accumulator ++ item)
}

Output

{
  "reverseKeys": {
    "city": "Tilburg",
    "alias": "mrhaki",
    "name": "Hubert Klein Ikkink"
  }
}

Written with DataWeave 2.4.

February 17, 2022

DataWeave Delight: Removing duplicates from an array

If we want to remove duplicate items from an array in DataWeave we can use the distinctBy function from the dw::Core module. The first argument is an array and the second argument is a criteria function. The criteria function gets an item of the array and the index value of that item in the array. The return value of the function defines the uniqueness of that item. This can simply be the item value itself or something more complex like a calculation or selector.

In the following example we use several criteria functions to remove duplicate items from an array:

Source

%dw 2.0

var items = [
    { id: "DW", name: "DataWeave" },
    { id: "CLJ", name: "Clojure" },
    { id: "DW", name: "DataWeave" }
]
var s = "aabccdeff"

output application/json 
---
{ 
    // An array with numbers where we want to remove duplicates.
    numbers1: [1, 5, 5, 6, 2, 3, 3, 1] distinctBy ((item) -> item),

    // We can also use a bit more complex function to define uniqueness.
    numbers2: [1, -2, 2, 3, -1] distinctBy (item) -> abs(item),

    // We can use shorthand notation $ and
    // distinctBy also works on objects in the array.
    items1: items distinctBy $,

    // Instead of doing a distinctBy on the whole object
    // we can also use matching function and use properties
    // from the object to define unique objects.
    items2: items distinctBy ((item, index) -> item.id),

    // With the help of toArray and joinBy we can remove duplicates
    // from a string value.
    string: dw::util::Coercions::toArray(s) distinctBy $ joinBy ""
}

Output

{
  "numbers1": [
    1,
    5,
    6,
    2,
    3
  ],
  "numbers2": [
    1,
    -2,
    3
  ],
  "items1": [
    {
      "id": "DW",
      "name": "DataWeave"
    },
    {
      "id": "CLJ",
      "name": "Clojure"
    }
  ],
  "items2": [
    {
      "id": "DW",
      "name": "DataWeave"
    },
    {
      "id": "CLJ",
      "name": "Clojure"
    }
  ],
  "string": "abcdef"
}

Written with DataWeave 2.4.

February 15, 2022

DataWeave Delight: Trimming string values

In DataWeave we can use the trim function from the dw::Core module to remove any space character before and after a string value. The characters that are removed are the space, tab and newline characters. There is an overloaded function definition that will accept a null value and returns a null value.

In the following example we trim string values that start and end with spaces, tabs and newline characters:

Source

%dw 2.0

output application/json
---
{
    // Spaces at the start and end ar removed.
    spaces: trim("   mrhaki  "),

    // Also tabs are removed.
    tabs: trim("\t mrhaki"),

    // And newline characters are removed
    newline: trim("\tmrhaki \r\n"),

    // trim will return null when a null value is used.
    nullValue: trim(null),
}

Output

{
  "spaces": "mrhaki",
  "tabs": "mrhaki",
  "newline": "mrhaki",
  "nullValue": null
}

Written with DataWeave 2.4.