-
Notifications
You must be signed in to change notification settings - Fork 1
/
traversal.go
261 lines (234 loc) · 4.81 KB
/
traversal.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package traversal for traversing JSON text
package traversal
import (
"bytes"
"encoding/json"
"io"
"github.com/pkg/errors"
)
// Traversal holds the state while travering JSON
type Traversal struct {
err error
msg json.RawMessage
}
// Start begins a Traversal by initializing the internal state with JSON data.
func Start(data []byte) *Traversal {
var t Traversal
if json.Valid(data) {
t.err = t.msg.UnmarshalJSON(data)
} else {
t.err = errors.Errorf("Invalid JSON")
}
return &t
}
// End terminates a Traversal
// If there is no error, End writes the internal state to the writer
// Note that a useful tool in debugging is to dump to os.Stdout
func (t *Traversal) End(w io.Writer) error {
if t.err != nil {
return t.err
}
data, err := t.msg.MarshalJSON()
if err != nil {
return errors.Wrap(err, "io.Write")
}
_, err = io.Copy(w, bytes.NewReader(data))
if err != nil {
return errors.Wrap(err, "o.Copy")
}
return nil
}
// ObjectKey selects a key from a JSON object (go map)
//
// given:
//
// {
// "name": "tagging",
// "category": "http"
// }
// key = "category"
//
// expecting
//
// \"http\" (the output is JSON)
func (t *Traversal) ObjectKey(key string) *Traversal {
if t.err != nil {
return t
}
m, err := GetMapFromRawMessage(t.msg)
if err != nil {
return &Traversal{
err: errors.Wrap(err, "getMapFromRawMessage"),
msg: nil,
}
}
msg, ok := m[key]
if !ok {
return &Traversal{
err: errors.Errorf("No entry for key '%s'", key),
msg: nil,
}
}
return &Traversal{err: nil, msg: msg}
}
// ArraySingleton selects the only entry from an Array.
// It will fail if the Array does not have exactly one item.
//
// given:
//
// [
// {"key": "value"}
// ]
//
// expecting
//
// {"key": "value"}
//
func (t *Traversal) ArraySingleton() *Traversal {
if t.err != nil {
return t
}
s, err := GetSliceFromRawMessage(t.msg)
if err != nil {
return &Traversal{
err: errors.Wrap(err, "getSliceFromRawMessage"),
msg: nil,
}
}
if len(s) != 1 {
return &Traversal{
err: errors.Errorf("Array has %d items", len(s)),
msg: nil,
}
}
return &Traversal{err: nil, msg: s[0]}
}
// ArraySlice selects a whole slice from an Array in JSON
// It will fail if the Array does not have any elemants
//
// given:
//
// [
// {"key": "value"}
// ]
//
// expecting
//
// [
// {"key": "value"}
// ]
//
func (t *Traversal) ArraySlice() *Traversal {
if t.err != nil {
return t
}
s, err := GetMsgFromRawMessage(t.msg)
if err != nil {
return &Traversal{
err: errors.Wrap(err, "getSliceFromRawMessage"),
msg: nil,
}
}
return &Traversal{err: nil, msg: s}
}
// ArrayPredicate selects an entry from an Array based on a predicate
//
// given:
//
// [
// {"key1": "value1"},
// {"key2": "value2"},
// {"key3": "value3"}
// ]
//
// with predicate
//
// func(r json.RawMessage) bool {
// m, err := GetMapFromRawMessage(r)
// if err != nil {
// return false
// }
// n, err := GetStringFromRawMessage(m["key3"])
// if err != nil {
// return false
// }
// return n == "value3"
// }
//
// expecting
//
// {"key3": "value3"}
//
func (t *Traversal) ArrayPredicate(p func(json.RawMessage) bool) *Traversal {
if t.err != nil {
return t
}
s, err := GetSliceFromRawMessage(t.msg)
if err != nil {
return &Traversal{
err: errors.Wrap(err, "getSliceFromRawMessage"),
msg: nil,
}
}
for _, msg := range s {
if p(msg) {
return &Traversal{err: nil, msg: msg}
}
}
return &Traversal{
err: errors.Errorf("No Array item satisfies the predicate: %s", t.msg),
msg: nil,
}
}
// Selector selects whatever you want from the current traversal
//
// The go language does not allow you to add methods to an object outside of its package
// Selector gives you the ability to do just that
// We ask that you adhere to the spirit of composotion
// * No side effects
// * Pass on some proper subset of the incoming json.RawMessage, without making changes to it
//
// for example, you could duplicate ArrayPredicate
// given:
//
// [
// {"key1": "value1"},
// {"key2": "value2"},
// {"key3": "value3"}
// ]
//
// with selector
//
// func(r json.RawMessage) (json.RawMessage, error) {
// s, err := tr.GetSliceFromRawMessage(r)
// if err != nil {
// return nil, err
// }
//
// for _, msg := range s {
// m, err := tr.GetMapFromRawMessage(msg)
// if err != nil {
// return nil, err
// }
// v, ok := m["key3"]
// if ok {
// return v, nil
// }
// }
//
// if we make it here, we didn't find what we are looking for
// return nil, fmt.Errorf("not found")
// }
//
// expecting
//
// "value3"
//
func (t *Traversal) Selector(s func(json.RawMessage) (json.RawMessage, error)) *Traversal {
if t.err != nil {
return t
}
var result Traversal
result.msg, result.err = s(t.msg)
return &result
}