forked from bitly/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjnode.go
693 lines (614 loc) · 16 KB
/
jnode.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Package jpath provides a way to search and manipulate JSON documents
package jpath
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"reflect"
"strconv"
"strings"
)
// Version contains the version number. The API is stable within the same major version.
const Version = 1.0
type (
// Node is a JSON document, or a part of a JSON document
Node struct {
data interface{}
}
// NodeList is a list of nodes
NodeList []*Node
// NodeMap is a map of nodes
NodeMap map[string]*Node
)
// NilNode is an empty node. Used when not finding nodes with Get.
var (
NilNode = &Node{nil}
ErrKeyNotFound = errors.New("key not found")
)
// New returns a pointer to a new `Node` object
// after unmarshaling `body` bytes
func New(body []byte) (*Node, error) {
if len(body) == 0 {
// Use an empty list if no data has been provided
body = []byte("[]")
}
j := new(Node)
err := j.UnmarshalJSON(body)
if err != nil {
return nil, err
}
return j, nil
}
// NewNode returns a pointer to a new, empty `Node` object
func NewNode() *Node {
return &Node{
data: make(map[string]interface{}),
}
}
// Interface returns the underlying data
func (j *Node) Interface() interface{} {
return j.data
}
// JSON returns its marshaled data as `[]byte`
func (j *Node) JSON() ([]byte, error) {
data, err := j.MarshalJSON()
if err != nil {
return []byte{}, err
}
return data, nil
}
// MustJSON returns its marshaled data as `[]byte`
func (j *Node) MustJSON() []byte {
data, err := j.MarshalJSON()
if err != nil {
return []byte{}
}
return data
}
// PrettyJSON returns its marshaled data as `[]byte` with indentation
func (j *Node) PrettyJSON() ([]byte, error) {
return json.MarshalIndent(&j.data, "", " ")
}
// MarshalJSON implements the json.Marshaler interface
func (j *Node) MarshalJSON() ([]byte, error) {
return json.Marshal(&j.data)
}
// Set modifies `Node` map by `key` and `value`
// Useful for changing single key/value in a `Node` object easily.
func (j *Node) Set(key string, val interface{}) {
m, ok := j.CheckMap()
if !ok {
return
}
m[key] = val
}
// SetBranch modifies `Node`, recursively checking/creating map keys for the supplied path,
// and then finally writing in the value.
func (j *Node) SetBranch(branch []string, val interface{}) {
if len(branch) == 0 {
j.data = val
return
}
// in order to insert our branch, we need map[string]interface{}
if _, ok := (j.data).(map[string]interface{}); !ok {
// have to replace with something suitable
j.data = make(map[string]interface{})
}
curr := j.data.(map[string]interface{})
for i := 0; i < len(branch)-1; i++ {
b := branch[i]
// key exists?
if _, ok := curr[b]; !ok {
n := make(map[string]interface{})
curr[b] = n
curr = n
continue
}
// make sure the value is the right sort of thing
if _, ok := curr[b].(map[string]interface{}); !ok {
// have to replace with something suitable
n := make(map[string]interface{})
curr[b] = n
}
curr = curr[b].(map[string]interface{})
}
// add remaining k/v
curr[branch[len(branch)-1]] = val
}
// GetKey returns a pointer to a new `Node` object
// for `key` in its `map` representation
// and a bool identifying success or failure
func (j *Node) GetKey(key string) (*Node, bool) {
m, ok := j.CheckMap()
if ok {
if val, ok := m[key]; ok {
return &Node{val}, true
}
}
return nil, false
}
// GetIndex returns a pointer to a new `Node` object
// for `index` in its slice representation
// and a bool identifying success or failure
func (j *Node) GetIndex(index int) (*Node, bool) {
a, ok := j.CheckList()
if ok {
if len(a) > index {
return &Node{a[index]}, true
}
}
return nil, false
}
// Get searches for the item as specified by the branch
// within a nested Node and returns a new Node pointer
// the pointer is always a valid Node, allowing for chained operations
//
// newJs := js.Get("top_level", "entries", 3, "dict")
func (j *Node) Get(branch ...interface{}) *Node {
jin, ok := j.CheckGet(branch...)
if !ok {
return NilNode
}
return jin
}
// CheckGet is like Get, except it also returns a bool
// indicating whenever the branch was found or not
// the Node pointer may be nil
//
// newJs, ok := js.Get("top_level", "entries", 3, "dict")
func (j *Node) CheckGet(branch ...interface{}) (*Node, bool) {
jin := j
var ok bool
for _, p := range branch {
switch p := p.(type) {
case string:
jin, ok = jin.GetKey(p)
case int:
jin, ok = jin.GetIndex(p)
default:
ok = false
}
if !ok {
return nil, false
}
}
return jin, true
}
// CheckNodeMap returns a copy of a Node map, but with values as Nodes
func (j *Node) CheckNodeMap() (NodeMap, bool) {
m, ok := j.CheckMap()
if !ok {
return nil, false
}
jm := make(NodeMap)
for key, val := range m {
jm[key] = &Node{val}
}
return jm, true
}
// CheckNodeList returns a copy of a slice, but with each value as a Node
func (j *Node) CheckNodeList() ([]*Node, bool) {
a, ok := j.CheckList()
if !ok {
return nil, false
}
ja := make([]*Node, len(a))
for key, val := range a {
ja[key] = &Node{val}
}
return ja, true
}
// CheckMap type asserts to `map`
func (j *Node) CheckMap() (map[string]interface{}, bool) {
if m, ok := (j.data).(map[string]interface{}); ok {
return m, true
}
return nil, false
}
// CheckList type asserts to a slice
func (j *Node) CheckList() ([]interface{}, bool) {
if a, ok := (j.data).([]interface{}); ok {
return a, true
}
return nil, false
}
// CheckBool type asserts to `bool`
func (j *Node) CheckBool() (bool, bool) {
if s, ok := (j.data).(bool); ok {
return s, true
}
return false, false
}
// CheckString type asserts to `string`
func (j *Node) CheckString() (string, bool) {
if s, ok := (j.data).(string); ok {
return s, true
}
return "", false
}
// NodeList guarantees the return of a `[]*Node` (with optional default)
func (j *Node) NodeList(args ...NodeList) NodeList {
var def NodeList
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("NodeList() received too many arguments %d", len(args))
}
if a, ok := j.CheckNodeList(); ok {
return a
}
return def
}
// NodeMap guarantees the return of a `map[string]*Node` (with optional default)
func (j *Node) NodeMap(args ...NodeMap) NodeMap {
var def NodeMap
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("NodeMap() received too many arguments %d", len(args))
}
if a, ok := j.CheckNodeMap(); ok {
return a
}
return def
}
// List guarantees the return of a `[]interface{}` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
//
// for i, v := range js.Get("results").List() {
// fmt.Println(i, v)
// }
func (j *Node) List(args ...[]interface{}) []interface{} {
var def []interface{}
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("List() received too many arguments %d", len(args))
}
if a, ok := j.CheckList(); ok {
return a
}
return def
}
// Map guarantees the return of a `map[string]interface{}` (with optional default)
//
// useful when you want to interate over map values in a succinct manner:
//
// for k, v := range js.Get("dictionary").Map() {
// fmt.Println(k, v)
// }
func (j *Node) Map(args ...map[string]interface{}) map[string]interface{} {
var def map[string]interface{}
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Map() received too many arguments %d", len(args))
}
a, ok := j.CheckMap()
if ok {
return a
}
return def
}
// String guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
//
// myFunc(js.Get("param1").String(), js.Get("optional_param").String("my_default"))
func (j *Node) String(args ...string) string {
var def string
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("String() received too many arguments %d", len(args))
}
s, ok := j.CheckString()
if ok {
return s
}
return def
}
// Int guarantees the return of an `int` (with optional default)
//
// useful when you explicitly want an `int` in a single value return context:
//
// myFunc(js.Get("param1").Int(), js.Get("optional_param").Int(5150))
func (j *Node) Int(args ...int) int {
var def int
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Int() received too many arguments %d", len(args))
}
i, ok := j.CheckInt()
if ok {
return i
}
return def
}
// Float64 guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
//
// myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(5.150))
func (j *Node) Float64(args ...float64) float64 {
var def float64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Float64() received too many arguments %d", len(args))
}
f, ok := j.CheckFloat64()
if ok {
return f
}
return def
}
// Bool guarantees the return of a `bool` (with optional default)
//
// useful when you explicitly want a `bool` in a single value return context:
//
// myFunc(js.Get("param1").Bool(), js.Get("optional_param").Bool(true))
func (j *Node) Bool(args ...bool) bool {
var def bool
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Bool() received too many arguments %d", len(args))
}
b, ok := j.CheckBool()
if ok {
return b
}
return def
}
// Int64 guarantees the return of an `int64` (with optional default)
//
// useful when you explicitly want an `int64` in a single value return context:
//
// myFunc(js.Get("param1").Int64(), js.Get("optional_param").Int64(5150))
func (j *Node) Int64(args ...int64) int64 {
var def int64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Int64() received too many arguments %d", len(args))
}
i, ok := j.CheckInt64()
if ok {
return i
}
return def
}
// Uint64 guarantees the return of an `uint64` (with optional default)
//
// useful when you explicitly want an `uint64` in a single value return context:
//
// myFunc(js.Get("param1").Uint64(), js.Get("optional_param").Uint64(5150))
func (j *Node) Uint64(args ...uint64) uint64 {
var def uint64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Uint64() received too many arguments %d", len(args))
}
i, ok := j.CheckUint64()
if ok {
return i
}
return def
}
// NewFromReader returns a *Node by decoding from an io.Reader
func NewFromReader(r io.Reader) (*Node, error) {
j := new(Node)
dec := json.NewDecoder(r)
err := dec.Decode(&j.data)
return j, err
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (j *Node) UnmarshalJSON(p []byte) error {
return json.Unmarshal(p, &j.data)
}
// CheckFloat64 coerces into a float64
func (j *Node) CheckFloat64() (float64, bool) {
switch j.data.(type) {
case float32, float64:
return reflect.ValueOf(j.data).Float(), true
case int, int8, int16, int32, int64:
return float64(reflect.ValueOf(j.data).Int()), true
case uint, uint8, uint16, uint32, uint64:
return float64(reflect.ValueOf(j.data).Uint()), true
}
return 0, false
}
// CheckInt coerces into an int
func (j *Node) CheckInt() (int, bool) {
switch j.data.(type) {
case float32, float64:
return int(reflect.ValueOf(j.data).Float()), true
case int, int8, int16, int32, int64:
return int(reflect.ValueOf(j.data).Int()), true
case uint, uint8, uint16, uint32, uint64:
return int(reflect.ValueOf(j.data).Uint()), true
}
return 0, false
}
// CheckInt64 coerces into an int64
func (j *Node) CheckInt64() (int64, bool) {
switch j.data.(type) {
case float32, float64:
return int64(reflect.ValueOf(j.data).Float()), true
case int, int8, int16, int32, int64:
return reflect.ValueOf(j.data).Int(), true
case uint, uint8, uint16, uint32, uint64:
return int64(reflect.ValueOf(j.data).Uint()), true
}
return 0, false
}
// CheckUint64 coerces into an uint64
func (j *Node) CheckUint64() (uint64, bool) {
switch j.data.(type) {
case float32, float64:
return uint64(reflect.ValueOf(j.data).Float()), true
case int, int8, int16, int32, int64:
return uint64(reflect.ValueOf(j.data).Int()), true
case uint, uint8, uint16, uint32, uint64:
return reflect.ValueOf(j.data).Uint(), true
}
return 0, false
}
// GetNodes will find the JSON node (and parent node) that corresponds to the given JSON path
func (j *Node) GetNodes(JSONpath string) (*Node, *Node, error) {
parent := j
if JSONpath == "x" || JSONpath == "" {
// If the root node is a map or list with one element or less, use that as the node
if m, ok := j.CheckNodeMap(); ok && len(m) <= 1 {
return parent, NilNode, nil
} else if l, ok := j.CheckNodeList(); ok && len(l) <= 1 {
return parent, NilNode, nil
}
// We may have encountered a list with more than one item, for example
return parent, NilNode, nil
}
// JSON path starting with x[ is a special case.
if strings.HasPrefix(JSONpath, "x[") {
// Add a "." between "x" and "[".
JSONpath = "x." + JSONpath[1:]
}
// The "current node" starts out with being the root node
n := j
if strings.Contains(JSONpath, ".") {
for i, part := range strings.Split(JSONpath, ".") {
if i == 0 && (part == "" || part == "x") {
// If the current node is a map or list with one element or less, use that as the next node
if m, ok := n.CheckNodeMap(); ok && len(m) <= 1 {
n = parent
} else if l, ok := n.CheckNodeList(); ok && len(l) <= 1 {
n = parent
}
} else if strings.Contains(part, "[") {
fields := strings.SplitN(part, "[", 2)
name := fields[0]
secondpart := fields[1]
fields = strings.SplitN(secondpart, "]", 2)
stringIndex := fields[0]
index, err := strconv.Atoi(stringIndex)
if err != nil {
return NilNode, NilNode, errors.New("Invalid index: " + stringIndex)
}
parent = n
if name == "" {
n = n.Get(index)
} else {
parent = n.Get(name)
n = parent.Get(index)
}
} else {
parent = n
n = n.Get(part)
}
}
} else {
parent = n
part := JSONpath
n = n.Get(part)
}
return n, parent, nil
}
// GetNode will find the JSON node that corresponds to the given JSON path, or nil.
func (j *Node) GetNode(JSONpath string) *Node {
node, _, err := j.GetNodes(JSONpath)
if err != nil {
return NilNode
}
return node
}
// AddJSON adds JSON data to a list. The JSON path must refer to a list.
func (j *Node) AddJSON(JSONpath string, JSONdata []byte) error {
node := j.GetNode(JSONpath)
l, ok := node.CheckList()
if !ok {
return errors.New("Can only add JSON data to a list. Not a list: " + node.Info())
}
newNode, err := New(JSONdata)
if err != nil {
return err
}
node.data = append(l, newNode)
return nil
}
// DelKey removes a key in a map, given a JSON path to a map.
// Returns ErrKeyNotFound if the key is not found.
func (j *Node) DelKey(JSONpath string) error {
_, mapnode, err := j.GetNodes(JSONpath)
if err != nil {
return err
}
m, ok := mapnode.CheckMap()
if !ok {
return errors.New("Can only remove a key from a map. Not a map: " + mapnode.Info())
}
keyToRemove := lastpart(JSONpath)
foundKey := false
for k := range m {
if k == keyToRemove {
foundKey = true
break
}
}
if !foundKey {
return ErrKeyNotFound
}
delete(m, lastpart(JSONpath))
return nil
}
// Info returns a description of the node
func (j *Node) Info() string {
var buf bytes.Buffer
if j == NilNode {
buf.WriteString("Nil Node")
} else if m, ok := j.CheckMap(); ok {
buf.WriteString(fmt.Sprintf("Map with %d elements.", len(m)))
} else if l, ok := j.CheckList(); ok {
buf.WriteString(fmt.Sprintf("List with %d elements.", len(l)))
} else if s, ok := j.CheckString(); ok {
buf.WriteString(fmt.Sprintf("String: %s", s))
} else if s, ok := j.CheckInt(); ok {
buf.WriteString(fmt.Sprintf("Int: %d", s))
} else if b, ok := j.CheckBool(); ok {
buf.WriteString(fmt.Sprintf("Bool: %v", b))
} else if i, ok := j.CheckInt64(); ok {
buf.WriteString(fmt.Sprintf("Int64: %v", i))
} else if u, ok := j.CheckUint64(); ok {
buf.WriteString(fmt.Sprintf("Uint64: %v", u))
} else if f, ok := j.CheckFloat64(); ok {
buf.WriteString(fmt.Sprintf("Float64: %v", f))
} else {
buf.WriteString("Unknown node type")
}
return buf.String()
}