-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
98 lines (75 loc) · 1.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package traversal_test
import (
"encoding/json"
"os"
tr "github.com/dougfort/traversal"
)
type testType struct {
A string
B int32
C bool
}
func ExampleStart() {
data, _ := json.Marshal(&testType{A: "a", B: 43, C: true})
tr.Start(data)
}
func ExampleTraversal_End() {
data, _ := json.Marshal(&testType{A: "a", B: 43, C: true})
tr.Start(data).End(os.Stdout)
// Output: {"A":"a","B":43,"C":true}
}
func ExampleTraversal_ObjectKey() {
data, _ := json.Marshal(&testType{A: "a", B: 43, C: true})
tr.Start(data).ObjectKey("B").End(os.Stdout)
// Output: 43
}
func ExampleTraversal_ArraySingleton() {
data, _ := json.Marshal([]testType{{A: "a", B: 43, C: true}})
tr.Start(data).ArraySingleton().End(os.Stdout)
// Output: {"A":"a","B":43,"C":true}
}
func ExampleTraversal_ArraySlice() {
data, _ := json.Marshal([]testType{{A: "a", B: 43, C: true}})
tr.Start(data).ArraySlice().End(os.Stdout)
// Output: [{"A":"a","B":43,"C":true}]
}
func ExampleTraversal_ArrayPredicate() {
data, _ := json.Marshal([]testType{
{A: "a", B: 43, C: true},
{A: "a", B: 41, C: true},
{A: "a", B: 43, C: true},
})
predicate := func(r json.RawMessage) bool {
m, err := tr.GetMapFromRawMessage(r)
if err != nil {
return false
}
n, err := tr.GetInt32FromRawMessage(m["B"])
if err != nil {
return false
}
return n == 41
}
tr.Start(data).ArrayPredicate(predicate).End(os.Stdout)
// Output: {"A":"a","B":41,"C":true}
}
func ExampleTraversal_Selector() {
data, _ := json.Marshal([]testType{
{A: "a", B: 43, C: true},
{A: "a", B: 41, C: true},
{A: "a", B: 43, C: true},
})
predicate := func(r json.RawMessage) bool {
m, err := tr.GetMapFromRawMessage(r)
if err != nil {
return false
}
n, err := tr.GetInt32FromRawMessage(m["B"])
if err != nil {
return false
}
return n == 41
}
tr.Start(data).ArrayPredicate(predicate).End(os.Stdout)
// Output: {"A":"a","B":41,"C":true}
}