-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
79 lines (73 loc) · 1.25 KB
/
example_test.go
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
package jsondiff
import (
"fmt"
"os"
"github.com/itchyny/gojq"
)
var (
lhs = map[string]interface{}{"a": 1, "b": 2, "c": 3, "d": 4}
rhs = map[string]interface{}{"a": 1, "b": 1, "c": 2, "d": 3}
)
func ExampleDiffFromFiles() {
query, err := gojq.Parse(".d")
if err != nil {
panic(err)
}
from, err := os.Open("./testdata/from.json")
if err != nil {
panic(err)
}
to, err := os.Open("./testdata/to.json")
if err != nil {
panic(err)
}
diff, err := DiffFromFiles(from, to, Only(query))
if err != nil {
panic(err)
}
fmt.Print(diff)
// Output:
// --- from.json
// +++ to.json
// @@ -1,2 +1,2 @@
// -4
// +3
}
func ExampleDiffFromObjects_only() {
query, err := gojq.Parse(".d")
if err != nil {
panic(err)
}
diff, err := DiffFromObjects(lhs, rhs, Only(query))
if err != nil {
panic(err)
}
fmt.Println(diff)
// Output:
// --- from
// +++ to
// @@ -1,2 +1,2 @@
// -4
// +3
}
func ExampleDiffFromObjects_ignore() {
query, err := gojq.Parse(".b, .c")
if err != nil {
panic(err)
}
diff, err := DiffFromObjects(lhs, rhs, Ignore(query))
if err != nil {
panic(err)
}
fmt.Println(diff)
// Output:
// --- from
// +++ to
// @@ -2,6 +2,6 @@
// "a": 1,
// "b": null,
// "c": null,
// - "d": 4
// + "d": 3
// }
}