-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonwalk.go
More file actions
315 lines (290 loc) · 9.14 KB
/
jsonwalk.go
File metadata and controls
315 lines (290 loc) · 9.14 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package jsonwalk
import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
)
type NodeValueType int
const (
Nil NodeValueType = iota // "null" in JSON terminology.
Bool // Can be type asserted as v.(bool)
String // Can be type asserted as v.(string)
Float64 // "number" in JSON terminology. Can be type asserted as v.(float64)
Array // Can be type asserted as v.([]interface{})
Map // "object" in JSON terminology. Can be type asserted as v.(map[string]interface{})
)
type WalkPath interface {
// Path returns a path leading to the node in a string representation.
Path() string
// Level returns path level, starting with 0 for the first element.
Level() int
// MapEl constructs a child element with the k key.
MapEl(k string) walkPath
// ArrayEl constructs an array child element with index i.
ArrayEl(i int) walkPath
}
// walkPath is a default WalkPath implementation for Walk function
// which can be overridden with the WalkWith call.
type walkPath struct {
parent *walkPath // nil parent for the first node
portion string
preSepErase bool
level int // origin has the level of 0
}
func newWalkPath() walkPath {
return walkPath{} // all default
}
func (w walkPath) Path() string {
var s string
var v = &w
for {
if v != nil && v.parent != nil {
var sep = "."
if v.preSepErase {
sep = ""
}
if v.parent.parent == nil {
sep = ""
}
s = sep + v.portion + s
} else {
break
}
v = v.parent
}
return s
}
func (w walkPath) MapEl(k string) walkPath {
n := walkPath{
parent: &w,
portion: k,
preSepErase: false,
level: w.level + 1,
}
return n
}
func (w walkPath) ArrayEl(i int) walkPath {
n := walkPath{
parent: &w,
portion: "[" + strconv.Itoa(i) + "]",
preSepErase: true,
level: w.level + 1,
}
return n
}
func (w walkPath) Level() int {
return w.level
}
// WalkCallback is an interface with a callback function that is called for both leaf nodes of types
// Nil ("null" in JSON terminology),
// Bool,
// String and
// Float64 ("number" in JSON terminology)
// as well as container nodes of types Array and Map ("object" in JSON terminology).
//
// WalkCallback.C receives the node path in a form of WalkPath, which, if not overridden by WalkWith,
// returns a full non-escaped string-like path, for every discovered Map in a simple "."-separated form and "[int]" form to separate array elements.
//
// No expectations as to the order in which paths based on Map keys are discovered should be made. Array elements arrive in the order
// they are found in JSON. The parent of a node is guaranteed to be discovered before its child.
//
// Print{} returns a simple WalkCallback implementation that prints result of walk.
// NewOutput(io.Writer) gives an option to accept the output destination.
// Callback(c func(path WalkPath, key interface{}, value interface{}, vType NodeValueType)) is a wrapper to pass
// a callback function that returns an object that implements WalkCallback with that function as a delegate.
//
// The key for array elements is of type int, for map it is depending on the key type.
type WalkCallback interface {
C(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType)
}
type f struct {
f func(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType)
}
func (f f) C(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType) {
f.f(path, key, value, nodeValueType)
}
// Callback is a wrapper that accepts a callback function and returns a value that satisfies the WalkCallback interface.
func Callback(c func(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType)) WalkCallback {
return f{c}
}
// Print implements WalkCallback by printing JSON to the os.Stdout by utilizing the NewOutput(os.Stdout).
//
// Passing Print{} to the Walk function is enough to start printing the JSON structure.
type Print struct {
}
func (Print) C(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType) {
NewOutput(os.Stdout).C(path, key, value, nodeValueType)
}
// output implements WalkCallback in a form of a simple tree-like structure
// good enough for debugging. NewOutput allows to specify the output.
type output struct {
w io.Writer
}
// NewOutput returns an Output object initialized with the io.Writer output.
// It satisfies the WalkCallback interface.
//
// The output is a printout of the JSON structure with the paths between two vertical lines and
// hints on the key and value types. Indentation is squeezed to the left to save space. The actual
// level can be assumed from the amount of separate elements in the path.
//
// The first line of the output gives a hint on the root value type if it's a Map:
//
// (m)
// "Actors" |Actors| (a)
//
// or Array:
//
// (a)
// 0:1 |[0]| (0:f)
// 1:2 |[1]| (1:f)
//
// For single values it's as simple as:
//
// "abc" (s)
//
// All subsequent lines provide the type at the end of each line. It can also be in the form
// of a (keyType:valueType) for maps and values of maps, or (index:valueType) for arrays:
//
// "Born At":"New York City, NY" |Actors[1].Born At| (s:s)
//
// 1:"Isabella Jane" |Actors[0].children[1]| (1:s)
//
// "employees" |employees| (s:a)
func NewOutput(w io.Writer) *output {
return &output{w: w}
}
func (o *output) C(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType) {
var level = path.Level()
level = level - 1
if level < 0 {
level = 0
}
keyType := ""
if k, ok := key.(int); ok { // Key is of type int for array indices
keyType = strconv.Itoa(k)
} else {
keyType = strings.ToLower(t(key).String()[:1])
}
levelStr := ""
//levelStr = fmt.Sprintf(" #%d", path.Level())
if nodeValueType == Array || nodeValueType == Map {
// Not a leaf, only dealing with the key
if path.Level() == 0 {
// For root map / array we simply output its type.
_, _ = fmt.Fprintf(o.w, "(%v)%v\n",
strings.ToLower(nodeValueType.String()[:1]), levelStr)
} else {
var lv = strings.Repeat(" ", level)
_, _ = fmt.Fprintf(o.w, "%v%#v |%v| (%v:%v)%v\n",
lv, key, strings.TrimSpace(path.Path()), keyType, strings.ToLower(nodeValueType.String()[:1]), levelStr)
}
} else {
if path.Level() == 0 {
// For root single value we simply output its value and type.
_, _ = fmt.Fprintf(o.w, "%#v (%v)%v\n",
value, strings.ToLower(nodeValueType.String()[:1]), levelStr)
} else {
var lv = strings.Repeat(" ", level)
_, _ = fmt.Fprintf(o.w, "%v%#v:%#v |%v| (%v:%v)%v\n",
lv, key, value, strings.TrimSpace(path.Path()), keyType, strings.ToLower(nodeValueType.String()[:1]), levelStr)
}
return
}
}
// Walk walks unmarshalled arbitrary JSON with any of the root values.
//
// It calls walk.C for every leaf of types Nil, Bool, String or Float64 as well as every non-leaf node of types Array or Map.
// Callback receives discovered type in a form of NodeValueType for any logic to be performed based on that.
//
// Map keys will arrive in unpredictable order.
//
// var f interface{}
// err := json.Unmarshal([]byte(src), &f)
// if err != nil {
// return // deal with error
// }
// jsonwalk.Walk(&f, jsonwalk.Print{})
//
// For a custom callback, the easiest shortcut is a Callback which accepts a callback function.
//
// jsonwalk.Walk(&f, jsonwalk.Callback(c func(path WalkPath, key interface{}, value interface{}, nodeValueType NodeValueType) {
// ...
// }))
func Walk(m *interface{}, walk WalkCallback) {
w(newWalkPath(), nil, m, walk)
}
// WalkWith does the same as Walk except that it accepts starting WalkPath value
// which can be of a different type than the built-in walkPath, allowing for an overriden
// behavior of path construction.
//
// If nil is passed for path, the function falls back to calling Walk.
func WalkWith(path WalkPath, m *interface{}, walk WalkCallback) {
if path == nil {
Walk(m, walk)
} else {
w(path, nil, m, walk)
}
}
func t(k interface{}) NodeValueType {
switch k.(type) {
case nil:
return Nil
case bool:
return Bool
case string:
return String
case float64:
return Float64
case []interface{}:
return Array
case map[string]interface{}:
return Map
default:
panic(fmt.Sprintf("unsupported type conversion for %v (%T)", k, k))
}
}
func w(path WalkPath, k interface{}, v *interface{}, walk WalkCallback) {
switch vt := (*v).(type) {
case nil:
if walk != nil {
walk.C(path, k, vt, Nil)
}
case bool:
if walk != nil {
walk.C(path, k, vt, Bool)
}
case string:
if walk != nil {
walk.C(path, k, vt, String)
}
case float64:
if walk != nil {
walk.C(path, k, vt, Float64)
}
case []interface{}:
if walk != nil {
walk.C(path, k, vt, Array)
}
arrayWalk(path, &vt, walk)
case map[string]interface{}:
if walk != nil {
walk.C(path, k, vt, Map)
}
mapWalk(path, &vt, walk)
default:
panic(fmt.Sprintf("%v=%v (unknown type %v)", k, vt, reflect.TypeOf(vt)))
}
}
func mapWalk(path WalkPath, m *map[string]interface{}, walk WalkCallback) {
for k, v := range *m {
w(path.MapEl(k), k, &v, walk)
}
}
func arrayWalk(path WalkPath, a *[]interface{}, walk WalkCallback) {
for i, v := range *a {
w(path.ArrayEl(i), i, &v, walk)
}
}