This utility module provides functions for handling values as tree-data structures.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::util::Tree
to the header of your
DataWeave script.
Functions
asExpressionString
asExpressionString(path: Path): String
Transforms a Path
value into a string representation of the path.
Parameters
Name | Description |
---|---|
|
The |
Example
This example transforms a Path
value into a String
representation
of a selector for an attribute of an object.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Tree
output application/json
---
asExpressionString([
{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}
])
Output
1
".user.@name"
filterArrayLeafs
filterArrayLeafs(value: Any, criteria: (value: Any, path: Path) -> Boolean): Any
Applies a filtering expression to leaf or Path
values of an array.
The leaf values in the array must be SimpleType
or Null
values. See
Core Types
for descriptions of the types.
Parameters
Name | Description |
---|---|
|
An input value of |
|
Boolean expression to apply to |
Example
This example shows how filterArrayLeafs
behaves with different
inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%dw 2.0
import * from dw::util::Tree
var myArray = [1, {name: ["", true], test: 213}, "123", null]
output application/json
---
{
a: myArray filterArrayLeafs ((value, path) ->
!(value is Null or value is String)),
b: myArray filterArrayLeafs ((value, path) ->
(value is Null or value == 1)),
c: { a : [1,2] } filterArrayLeafs ((value, path) ->
(value is Null or value == 1)),
d: myArray filterArrayLeafs ((value, path) ->
!isArrayType(path))
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"a": [
1,
{
"name": [
true
],
"test": 213
}
],
"b": [
1,
{
"name": [
],
"test": 213
},
null
],
"c": {
"a": [
1
]
},
"d": [
{
"name": [
],
"test": 213
}
]
}
filterObjectLeafs
filterObjectLeafs(value: Any, criteria: (value: Any, path: Path) -> Boolean): Any
Applies a filtering expression to leaf or Path
values of keys in
an object.
The leaf values in the object must be SimpleType
or Null
values. See
Core Types
for descriptions of the types.
Parameters
Name | Description |
---|---|
|
An input value of |
|
Boolean expression to apply to |
Example
This example shows how filterObjectLeafs
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%dw 2.0
import * from dw::util::Tree
var myArray = [{name @(mail: "me@me.com", test:123 ): "", id:"test"},
{name: "Me", id:null}]
output application/json
---
{
a: {
name: "Mariano",
lastName: null,
age: 123,
friends: myArray
} filterObjectLeafs ((value, path) ->
!(value is Null or value is String)),
b: { c : null, d : "hello" } filterObjectLeafs ((value, path) ->
(value is Null and isObjectType(path)))
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"a": {
"age": 123,
"friends": [
{
},
{
}
]
},
"b": {
"c": null
}
}
filterTree
filterTree(value: Any, criteria: (value: Any, path: Path) -> Boolean): Any
Filters the value or path of nodes in an input based on a
specified criteria
.
The function iterates through the nodes in the input. The
criteria
can apply to the value or path in the input. If
the criteria
evaluates to true
, the node remains in the
output. If false
, the function filters out the node.
Parameters
Name | Description |
---|---|
|
The value to filter. |
|
The expression that determines whether to filter the node. |
Example
This example shows how filterTree
behaves with different inputs.
The output is application/dw
for demonstration purposes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
%dw 2.0
import * from dw::util::Tree
output application/dw
---
{
a: {
name : "",
lastName @(foo: ""): "Achaval",
friends @(id: 123): [{id: "", test: true}, {age: 123}, ""]
} filterTree ((value, path) ->
value match {
case s is String -> !isEmpty(s)
else -> true
}
),
b: null filterTree ((value, path) -> value is String),
c: [
{name: "Mariano", friends: []},
{test: [1,2,3]},
{dw: ""}
] filterTree ((value, path) ->
value match {
case a is Array -> !isEmpty(a as Array)
else -> true
})
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
a: {
lastName: "Achaval",
friends @(id: 123): [
{
test: true
},
{
age: 123
}
]
},
b: null,
c: [
{
name: "Mariano"
},
{
test: [
1,
2,
3
]
},
{
dw: ""
}
]
}
isArrayType
isArrayType(path: Path): Boolean
Returns true
if the provided Path
value is an ARRAY_TYPE
expression.
Parameters
Name | Description |
---|---|
|
The |
Example
This example shows how isArrayType
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::utils::Tree
output application/json
---
{
a: isArrayType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
b: isArrayType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ARRAY_TYPE, selector: 0, namespace: null}]),
c: isArrayType([{kind: ARRAY_TYPE, selector: 0, namespace: null}])
}
Output
1
2
3
4
5
{
"a": false,
"b": true,
"c": true
}
isAttributeType
isAttributeType(path: Path): Boolean
Returns true
if the provided Path
value is an ATTRIBUTE_TYPE
expression.
Parameters
Name | Description |
---|---|
|
The |
Example
This example shows how isAttributeType
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::util::Tree
output application/json
---
{
a: isAttributeType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
b: isAttributeType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ARRAY_TYPE, selector: "name", namespace: null}]),
c: isAttributeType([{kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}])
}
Output
1
2
3
4
5
{
"a": true,
"b": false,
"c": true
}
isObjectType
isObjectType(path: Path): Boolean
Returns true
if the provided Path
value is an OBJECT_TYPE
expression.
Parameters
Name | Description |
---|---|
|
The |
Example
This example shows how isObjectType
behaves with different inputs.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::utils::Tree
output application/json
---
{
a: isObjectType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
b: isObjectType([{kind: OBJECT_TYPE, selector: "user", namespace: null},
{kind: OBJECT_TYPE, selector: "name", namespace: null}]),
c: isObjectType([{kind: OBJECT_TYPE, selector: "user", namespace: null}])
}
Output
1
2
3
4
5
{
"a": false,
"b": true,
"c": true
}
mapLeafValues
mapLeafValues(value: Any, callback: (value: Any, path: Path) -> Any): Any
Maps the terminal (leaf) nodes in the tree.
Leafs nodes cannot have an object or an array as a value.
Parameters
Name | Description |
---|---|
|
The value to map. |
|
The mapper function. |
Example
This example transforms all the string values to upper case.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::util::Tree
output application/json
---
{
user: [{
name: "mariano",
lastName: "achaval"
}],
group: "data-weave"
} mapLeafValues (value, path) -> upper(value)
Output
1
2
3
4
5
6
7
8
9
{
"user": [
{
"name": "MARIANO",
"lastName": "ACHAVAL"
}
],
"group": "DATA-WEAVE"
}
Example
This example returns a new value for an object, array, or attribute.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
output application/json
import * from dw::util::Tree
---
{
name: "Mariano",
test: [1,2,3]
} mapLeafValues ((value, path) -> if(isObjectType(path))
"***"
else if(isArrayType(path))
"In an array"
else "Is an attribute")
Output
1
2
3
4
5
6
7
8
{
"name": "***",
"test": [
"In an array",
"In an array",
"In an array"
]
}
nodeExists
nodeExists(value: Any, callback: (value: Any, path: Path) -> Boolean): Boolean
Returns true
if any node in a given tree validates against
the specified criteria.
Parameters
Name | Description |
---|---|
|
The value to search. |
|
The criteria to apply to the input |
Example
This example checks for each user by name and last name. Notice
that you can also reference a value
with $
and the path
with $$
.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%dw 2.0
import * from dw::util::Tree
var myObject = {
user: [{
name: "mariano",
lastName: "achaval",
friends: [
{
name: "julian"
},
{
name: "tom"
}
]
},
{
name: "leandro",
lastName: "shokida",
friends: [
{
name: "peter"
},
{
name: "robert"
}
]
}
]
}
output application/json
---
{
mariano : myObject nodeExists ((value, path) -> path[-1].selector == "name" and value == "mariano"),
julian : myObject nodeExists ((value, path) -> path[-1].selector == "name" and value == "julian"),
tom : myObject nodeExists ($$[-1].selector == "name" and $ == "tom"),
leandro : myObject nodeExists ($$[-1].selector == "name" and $ == "leandro"),
peter : myObject nodeExists ($$[-1].selector == "name" and $ == "peter"),
wrongField: myObject nodeExists ($$[-1].selector == "wrongField"),
teo: myObject nodeExists ($$[-1].selector == "name" and $ == "teo")
}
Output
1
2
3
4
5
6
7
8
9
{
"mariano": true,
"julian": true,
"tom": true,
"leandro": true,
"peter": true,
"wrongField": false,
"teo": false
}
Variables
ARRAY_TYPE
Variable used to identify a PathElement
value as an array.
ATTRIBUTE_TYPE
Variable used to identify a PathElement
value as an attribute.
OBJECT_TYPE
Variable used to identify a PathElement
value as an object.
Types
Path
Type that consists of an array of PathElement
values that
identify the location of a node in a tree. An example is
[{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}] as Path
.
1
Array<PathElement>
PathElement
Type that represents a selection of a node in a path.
An example is {kind: ARRAY_TYPE, selector: "name", namespace: null} as PathElement
.
1
{| kind: "Object" | "Attribute" | "Array", selector: String | Number, namespace: Namespace | Null |}