This utility module calculates the difference between two values and returns the list of differences.

The module is included with Mule runtime. To use it, you must import it to your DataWeave code, for example, by adding the line import dw::util::Diff or import * from dw::util::Diff to your header.

Functions

diff

diff(Any, Any, { unordered?: Boolean }, String): Diff

Returns the structural differences between two values.

Differences between objects can be ordered (the default) or unordered. Ordered means that two objects do not differ if their key-value pairs are in the same order. Differences are expressed as Difference type.

Parameters
Name Description

actual

The actual value. Can be any data type.

expected

The expected value to compare to the actual. Can be any data type.

diffConfig

Setting for changing the default to unordered using `{ "unordered" : true} (explained in the introduction).

Example

This example shows a variety of uses of diff.

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
import diff from dw::util::Diff
ns ns0 http://locahost.com
ns ns1 http://acme.com
output application/dw
---
{
  "a": diff({a: 1}, {b:1}),
  "b": diff({ns0#a: 1}, {ns1#a:1}),
  "c": diff([1,2,3], []),
  "d": diff([], [1,2,3]),
  "e": diff([1,2,3], [1,2,3, 4]),
  "f": diff([{a: 1}], [{a: 2}]),
  "g": diff({a @(c: 2): 1}, {a @(c: 3): 1}),
  "h": diff(true, false),
  "i": diff(1, 2),
  "j": diff("test", "other test"),
  "k": diff({a: 1}, {a:1}),
  "l": diff({ns0#a: 1}, {ns0#a:1}),
  "m": diff([1,2,3], [1,2,3]),
  "n": diff([], []),
  "o": diff([{a: 1}], [{a: 1}]),
  "p": diff({a @(c: 2): 1}, {a @(c:2): 1}),
  "q": diff(true, true),
  "r": diff(1, 1),
  "s": diff("other test", "other test"),
  "t": diff({a:1 ,b: 2},{b: 2, a:1}, {unordered: true}),
  "u": [{format: "ssn",data: "ABC"}] diff [{ format: "ssn",data: "ABC"}]
}
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
ns ns0 http://locahost.com
ns ns1 http://acme.com
---
{
  a: {
    matches: false,
    diffs: [
      {
        expected: "Entry (root).a with type Number",
        actual: "was not present in object.",
        path: "(root).a"
      }
    ]
  },
  b: {
    matches: false,
    diffs: [
      {
        expected: "Entry (root).ns0#a with type Number",
        actual: "was not present in object.",
        path: "(root).ns0#a"
      }
    ]
  },
  c: {
    matches: false,
    diffs: [
      {
        expected: "Array size is 0",
        actual: "was 3",
        path: "(root)"
      }
    ]
  },
  d: {
    matches: false,
    diffs: [
      {
        expected: "Array size is 3",
        actual: "was 0",
        path: "(root)"
      }
    ]
  },
  e: {
    matches: false,
    diffs: [
      {
        expected: "Array size is 4",
        actual: "was 3",
        path: "(root)"
      }
    ]
  },
  f: {
    matches: false,
    diffs: [
      {
        expected: "1" as String {mimeType: "application/dw"},
        actual: "2" as String {mimeType: "application/dw"},
        path: "(root)[0].a"
      }
    ]
  },
  g: {
    matches: false,
    diffs: [
      {
        expected: "3" as String {mimeType: "application/dw"},
        actual: "2" as String {mimeType: "application/dw"},
        path: "(root).a.@.c"
      }
    ]
  },
  h: {
    matches: false,
    diffs: [
      {
        expected: "false",
        actual: "true",
        path: "(root)"
      }
    ]
  },
  i: {
    matches: false,
    diffs: [
      {
        expected: "2",
        actual: "1",
        path: "(root)"
      }
    ]
  },
  j: {
    matches: false,
    diffs: [
      {
        expected: "\"other test\"",
        actual: "\"test\"",
        path: "(root)"
      }
    ]
  },
  k: {
    matches: true,
    diffs: []
  },
  l: {
    matches: true,
    diffs: []
  },
  m: {
    matches: true,
    diffs: []
  },
  n: {
    matches: true,
    diffs: []
  },
  o: {
    matches: true,
    diffs: []
  },
  p: {
    matches: true,
    diffs: []
  },
  q: {
    matches: true,
    diffs: []
  },
  r: {
    matches: true,
    diffs: []
  },
  s: {
    matches: true,
    diffs: []
  },
  t: {
    matches: true,
    diffs: []
  },
  u: {
    matches: true,
    diffs: []
  }
}

Types

Diff

Describes the entire difference between two values.

Example with no differences:

{ "matches": true, "diffs": [ ] }

Example with differences:

{ "matches": true, "diffs": [ "expected": "4", "actual": "2", "path": "(root).a.@.d" ] }

See the diff function for another example.

Definition
1
{ matches: Boolean, diffs: Array<Difference> }

Difference

Describes a single difference between two values at a given structure.

Definition
1
{ expected: String, actual: String, path: String }