Search

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

July 11, 2022

DataWeave Delight: Using Literal Types

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.

In the following example we define four new literal types (North, East, South, West), use a union type to define the enumaration Compass and the overloaded function direction:

Source

%dw 2.0

import capitalize from dw::core::Strings

// String based literal types with one value.
type North = "north"
type East = "east"
type South = "south"
type West = "west"

// Using union types we can use several literal
// types to define a new type like an enumeration.
type Compass = North | East | South | West

// Simple function that will be invoked if the input
// argument is either "north" or "south".
fun direction(direction: North | South): String = 
    "You are walking to " ++ 
    // We can still use String functions, 
    // because the base literal type is String.
    capitalize(direction) ++ 
    " pole."

// Overloaded function to "catch" the other values
// for the literal type Compass: "east" and "west".    
fun direction(direction: Compass): String = "Where are you going?"

// Simple data structure with String values.
var data = { up: "NORTH", right: "EAST", down: "SOUTH", left: "WEST" }

output application/json
---
{
    north: direction("north"),
    east: direction("east"),

    // We can coerce a String value into a literal type.
    south: direction(lower(data.down) as Compass),
    west: direction(lower(data.left) as Compass)
}

Output

{
    "north": "You are walking to North pole.",
    "east": "Where are you going?",
    "south": "You are walking to South pole.",
    "west": "Where are you going?"
}

In the following example we use Number and Boolean literal types. Instead of defining them explicitly as types we use the literal type in the function definitions directly:

Source

%dw 2.0

// Overloaded functions with literal types defined 
// directly at the argument level of the function.
// Here we use a Number literal type.
fun displayItems(items: 0) = "You have no items"
fun displayItems(items: 1) = "You have 1 item"
fun displayItems(items: Number) = "You have " ++ items ++ " items"

// Also Boolean literal types are supported.
// We can combine default argument values
// and overloaded functions.
fun message(value: String, debug: true) = "value - " ++ message(value)
fun message(value: String, debug: Boolean = false) = value

output application/json
---
{
    items0: displayItems(0),
    items1: displayItems(1),
    items10: displayItems(10),

    message: message("DataWeave literal types"),
    messageWithDebug: message("DataWeave literal types", true)
}

Output

{
    "items0": "You have no items",
    "items1": "You have 1 item",
    "items10": "You have 10 items",
    "message": "DataWeave literal types",
    "messageWithDebug": "value - DataWeave literal types"
}   

Written with DataWeave 2.4.

June 26, 2022

DataWeave Delight: Calculating Remainder After Division

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.

In the following example we use different arguments for the mod function:

Source

%dw 2.0
output application/json
---
{
    result1: 6 mod 3,
    result2: 5 mod 3,
    result3: 5 mod -3,
    result4: -5 mod 3,
    result5: -5 mod -3,
    result6: 1.5 mod 1,
    result7: 5.095 mod 3
}

Output

{
  "result1": 0,
  "result2": 2,
  "result3": 2,
  "result4": -2,
  "result5": -2,
  "result6": 0.5,
  "result7": 2.095
}

Written with DataWave 2.4.

June 24, 2022

DataWeave Delight: Unzipping Arrays

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.

In the following example we use the unzip function with different input arrays:

Source

%dw 2.0

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

var fruitPricesIncomplete = [["Apple", 2.30], ["Pear", 1.82], [2.06]]

output application/json
---
{
    // unzip will break up each array into separate arrays.
    unzip: unzip(fruitPrices),

    // When the arrays to break up don't have the same 
    // number of elemnts unzip can only return elements
    // from the index that has the most elements.
    unzipIncomplete: unzip(fruitPricesIncomplete),

    // When the arrays to unzip have more than 2 elements
    // the results will be the number of arrays equal to the number of elements.
    // In this case we get 3 arrays as a result as the array to
    // unzip has 3 elements.
    unzipMoreElements: unzip([[1, "a", "A"], [2, "b", "B"]])
}

Output

{
  "unzip": [
    [
      "Apple",
      "Pear",
      "Banana"
    ],
    [
      2.30,
      1.82,
      2.06
    ]
  ],
  "unzipIncomplete": [
    [
      "Apple",
      "Pear",
      2.06
    ]
  ],
  "unzipMoreElements": [
    [
      1,
      2
    ],
    [
      "a",
      "b"
    ],
    [
      "A",
      "B"
    ]
  ]
}

Written with DataWeave 2.4.

March 14, 2022

DataWeave Delight: Importing Functions With An Alias

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.

In the following example code we use import using as to define aliases for functions and a module:

Source

%dw 2.0

// Use an alias for functions.
import upper as upperCase from dw::Core
import firstWith as findFirst from dw::core::Arrays

// Assign an alias to a module, so we can reference
// functions from the module as Str::<function>
import dw::core::Strings as Str

output application/json
---
{
    // Use alias upperCase for upper function.
    upperCase: upperCase("DataWeave"),

    // Use alias findFirst for firstWith function.
    findFirst: [1, 2, 3, 4, 5] findFirst ((item, index) -> item > 3),

    // Use function from module with alias.
    str: Str::reverse("DataWeave")
} 

Output

{
  "upperCase": "DATAWEAVE",
  "findFirst": 4,
  "str": "evaeWataD"
}

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.

DataWeave Delight: Reversing the items in an array

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

The second option is to use the orderBy function for an array from the dw::Core module. The function takes as first argument an array and the second argument is a criteria function that is applied for each element. The criteria function will get the array element value and index. The output of the function is used for the ordering of the resulting array. To reverse the order we can negate the index value of the criteria function and we get a new array in reverse order: ((item, index) -> -index).

In the following example we use both ways to reverse the order of an array:

Source

%dw 2.0

var list = [2, 1, 4, 5, 3]

output application/json
---
{
    // Using index operator.
    reverseIndices: list[-1 to 0],

    // Using orderBy with criteria function.
    reverseOrderBy: list orderBy ((item, index) -> -index),

    // Using orderBy with shorthand notation for criteria function.
    reverseOrderByShorthand: list orderBy -$$
}

Output

{
  "reverseOrderBy": [
    3,
    5,
    4,
    1,
    2
  ],
  "reverseIndices": [
    3,
    5,
    4,
    1,
    2
  ],
  "reverseOrderByShorthand": [
    3,
    5,
    4,
    1,
    2
  ]
}

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 6, 2022

DataWeave Delight: Using string interpolation

In DataWeave we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression must be enclosed in parentheses where the first parenthesis is prefixed with a dollar sign: $(<expression>). The expression must return a string value or can be automatically coerced into a string value in order for it to work. The expression can also be a variable.

In the following example we use a variable, selector, the upper function and a calculation that returns a number that is coerced into a string:

Source

%dw 2.0

var prefix = "Hi"

var user = { alias: "mrhaki", name: "Hubert Klein Ikkink"}

var count = 41

output text/plain
---
"$(prefix), 
welcome $(user.name) also known as $(upper(user.alias)).
You're visitor $(count + 1)."

Output

Hi, 
welcome Hubert Klein Ikkink also known as MRHAKI.
You're visitor 42.

Written with DataWeave 2.4.

February 5, 2022

DataWeave Delight: Calculate the average for an array of numbers

In DataWeave we can calculate the average for an array of numbers with the avg function. If we want to calculate the average for other types in an array or object if we first transform the source object to an array of numbers.

In the following example we use the avg function:

Source

%dw 2.0

var numbers = [10, 20, 30, 40, 50]

var words = ["MuleSoft", "DataWeave", "Delight"]

var books = [
    {
        title: "Lord of the rings",
        price: 30.0
    },
    {
        title: "Hitch-hiker's guide to the galaxy",
        price: 42.0
    }
]

output application/json
---
{
    // Average for a array of numbers.
    average: avg(numbers),

    // We can map our source objects to number values 
    // and calculate the average.
    averageWordSize: avg(words map sizeOf($)),

    // The array of book object can be turned into
    // an array with only the prices and we can
    // calculate the average.
    averageBookPrice: avg(books map $.price)
}

Output

{
  "average": 30,
  "averageWordSize": 8,
  "averageBookPrice": 36.0
}

Written with DataWeave 2.4.

February 3, 2022

DataWeave Delight: Creating a UUID

To get a v4 UUID in DataWeave we must use the function uuid from the dw::Core module. The function simply returns a value that is created using random numbers. So each time we invoke the function we will get a different value.

In the following example we invoke the uuid function to get a value:

Source

%dw 2.0

output text/plain
---
uuid()

Output

608c2c26-802a-4fca-9e9d-23931420fed1

Written with DataWeave 2.4.

February 2, 2022

DataWeave Delight: Finding highest or lowest value in an array

DataWeave has several function to find the highest or lowest value in an array. The items in the array need to be of the same type and implement the Comparable interface. To find the highest value we can use the max function. The function returns the maximum value based on the type of the item in the array. If we want to find the highest value based on other criteria we can use the maxBy function. This function takes a lambda as last argument where we can return a Comparable value that is used to determine the highest value. To find the lowest value in an array we have similar functions: min and minBy.

In the following example we use several ways to find the highest and lowest value in arrays with items of different types:

Source

%dw 2.0

// Array with numbers
var numbers = [10, 2, 3, 1, 20]

// Array with string values
var languages = ["DataWeave", "Java", "Groovy", "Clojure"]

// Array with objects representing a user
var users = [
	{ name: "mrhaki", age: 47 },
	{ name: "alice",  age: 32 },
	{ name: "bob",    age: 61 }
]

output application/json
---
{
	// Find maximum for numbers
	maxNumbers: max(numbers),
	
	// Find minimum for numbers
	minNumbers: min(numbers),
	
	// Find max for srings looks at first character
	maxLanguages: max(languages),
	
	// Find min for strings looks at first character
	minLanguages: min(languages),
	
	// Find language with max length
	maxByLength1: maxBy(languages, (lang) -> sizeOf(lang)),
	
	// Find max with infix notation
	maxByLength2: languages maxBy (lang) -> sizeOf(lang),
	
	// Find max with infix notation and $ to reference lambda argument
	maxByLength3: languages maxBy sizeOf($),
	
	// Find language with minimal length
	minByLength: languages minBy ((lang) -> sizeOf(lang)),
	
	// Find oldest user with maxBy on objects
	oldestUser: users maxBy ((user) -> user.age),
	
	// Find youngest user with minBy on objects
	youngestUser: users minBy (user) -> user.age
}

Output

{
  "maxNumbers": 20,
  "minNumbers": 1,
  "maxLanguages": "Java",
  "minLanguages": "Clojure",
  "maxByLength1": "DataWeave",
  "maxByLength2": "DataWeave",
  "maxByLength3": "DataWeave",
  "minByLength": "Java",
  "oldestUser": {
    "name": "bob",
    "age": 61
  },
  "youngestUser": {
    "name": "alice",
    "age": 32
  }
}

Written with DataWeave 2.4.