-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
904 lines (863 loc) · 20.3 KB
/
ast.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
package airp
import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
)
// JSONType is an enum for any JSON-types
type JSONType uint8
//go:generate stringer -type JSONType
// JSONTypes to caompare nodes of an ast with. The zero value signals invalid.
const (
Error JSONType = iota
Null
Bool
Number
String
Array
Object
)
// Node is one node of a tree building a JSON-AST.
// Depending on its internal type it holds a different value:
// JSONType ValueType
// Error nil
// Null nil
// Bool bool
// Number float64
// String string
// Array []*Node
// Object []KeyNode
type Node struct {
jsonType JSONType
value interface{}
parent *Node
}
type KeyNode struct {
Key string
*Node
}
// EqNode compares the nodes and all their children. Object keys order is
// abitary.
func EqNode(a, b *Node) bool {
if a == b {
return true
}
if a == nil || b == nil || a.jsonType != b.jsonType {
return false
}
if a.jsonType == Array {
an, bn := a.value.([]*Node), b.value.([]*Node)
if len(an) != len(bn) {
return false
}
for i := range an {
if !EqNode(an[i], bn[i]) {
return false
}
}
return true
} else if a.jsonType == Object {
an, bn := a.value.([]KeyNode), b.value.([]KeyNode)
if len(an) != len(bn) {
return false
}
for i := range an {
if m, ok := b.GetChild(an[i].Key); !ok || !EqNode(an[i].Node, m) {
return false
}
}
return true
} else if a.value == b.value {
return true
}
return false
}
// NewJSON reads from b and generates an AST
func NewJSON(b []byte) (*Node, error) {
return parse(lex(bytes.NewReader(b)))
}
// NewJSONReader reads from r and generates an AST
func NewJSONReader(r io.Reader) (*Node, error) {
return parse(lex(r))
}
// NewJSONString reads from s and generates an AST
func NewJSONString(s string) (*Node, error) {
return parse(lex(strings.NewReader(s)))
}
// NewJSONGo reads in a Go-value and generates a json ast that can be
// manipulated easily.
func NewJSONGo(val interface{}) (*Node, error) {
if val == nil {
return &Node{jsonType: Null}, nil
}
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Bool:
return &Node{jsonType: Bool, value: v.Bool()}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return &Node{jsonType: Number, value: float64(v.Int())}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &Node{jsonType: Number, value: float64(v.Uint())}, nil
case reflect.Float32, reflect.Float64:
return &Node{jsonType: Number, value: v.Float()}, nil
case reflect.String:
return &Node{jsonType: String, value: v.String()}, nil
case reflect.Slice:
if v.Type().Elem().Kind() == reflect.Uint8 {
return &Node{jsonType: String, value: string(v.Bytes())}, nil
}
fallthrough
case reflect.Array:
nn := []*Node(nil)
for i := 0; i < v.Len(); i++ {
n, err := NewJSONGo(v.Index(i).Interface())
if err != nil {
return nil, err
}
nn = append(nn, n)
}
o := &Node{jsonType: Array, value: nn}
for _, m := range nn {
m.parent = o
}
return o, nil
case reflect.Map:
nn := []KeyNode(nil)
for _, key := range v.MapKeys() {
elem := v.MapIndex(key)
n, err := NewJSONGo(elem.Interface())
if err != nil {
return nil, err
}
nn = append(nn, KeyNode{key.String(), n})
}
o := &Node{jsonType: Object, value: nn}
for _, m := range nn {
m.parent = o
}
return o, nil
case reflect.Struct:
nn := []KeyNode(nil)
t := v.Type()
outer:
for i := 0; i < v.NumField(); i++ {
if r, _ := utf8.DecodeRuneInString(t.Field(i).Name); !unicode.IsUpper(r) {
continue
}
elemT := t.Field(i)
tags := strings.Split(elemT.Tag.Get("json"), ",")
key := tags[0]
if key == "-" {
continue
}
if key == "" {
key = elemT.Name
}
iVal := v.Field(i)
for _, tag := range tags[1:] {
if tag == "omitempty" &&
iVal.Interface() == reflect.Zero(iVal.Type()).Interface() {
continue outer
}
if tag == "string" {
iVal = reflect.ValueOf(fmt.Sprint(iVal.Interface()))
}
}
n, err := NewJSONGo(iVal.Interface())
if err != nil {
return nil, err
}
nn = append(nn, KeyNode{key, n})
}
o := &Node{jsonType: Object, value: nn}
for _, m := range nn {
m.parent = o
}
return o, nil
case reflect.Ptr:
return NewJSONGo(v.Elem().Interface())
default:
return nil, fmt.Errorf("invalid type %s", v.Kind())
}
}
// StandaloneNode generates a single json value of str.
// It panics if str is a compund json expression.
func StandaloneNode(key, str string) KeyNode {
n, err := NewJSONString(str)
if err != nil {
panic(err)
}
if cc, ok := n.value.([]*Node); ok && len(cc) > 0 {
panic("given value must be single!")
}
if cc, ok := n.value.([]KeyNode); ok && len(cc) > 0 {
panic("given value must be single!")
}
return KeyNode{key, n}
}
// Type returns the JSONType of a node.
func (n *Node) Type() JSONType {
if n == nil {
return Error
}
return n.jsonType
}
// Key returns the name of a Node.
func (n *Node) Key() string {
if n == nil {
return ""
}
ss := make([]string, 0, 8)
outer:
for o, p := n, n.parent; p != nil; o, p = p, p.parent {
switch p.jsonType {
case Object:
kn := p.value.([]KeyNode)
for i := range kn {
if o == kn[i].Node {
ss = append(ss, kn[i].Key)
continue outer
}
}
if len(kn) != 0 {
panic(fmt.Errorf("invariant violation: %s", maxParent(o)))
}
case Array:
nn := p.value.([]*Node)
for i := range nn {
if o == nn[i] {
ss = append(ss, strconv.Itoa(i))
continue outer
}
}
if len(nn) != 0 {
panic(fmt.Errorf("invariant violation: %s", maxParent(o)))
}
default:
break outer
}
}
rr := make([]string, len(ss))
for i, s := range ss {
rr[len(ss)-i-1] = s
}
return strings.Join(rr, ".")
}
// Value creates the Go representation of a JSON-Node.
// Like encoding/json the possible underlying types of the first return
// parameter are:
// Object map[string]interface{}
// Array []interface{}
// String string
// Number float64
// Bool bool
// Null nil (with the error being nil too)
func (n *Node) Value() (interface{}, error) {
if !isValid(n) {
return nil, fmt.Errorf("internal type mismatch; want %s, got %T",
n.jsonType, n.value)
}
switch n.jsonType {
default:
return n.value, nil
case Object:
m := make(map[string]interface{}, 2)
for _, f := range n.value.([]KeyNode) {
itf, err := f.Value()
if err != nil {
return nil, err
}
m[f.Key] = itf
}
return m, nil
case Array:
s := make([]interface{}, 0, 2)
for _, f := range n.value.([]*Node) {
itf, err := f.Value()
if err != nil {
return nil, err
}
s = append(s, itf)
}
return s, nil
}
}
// String formats an ast as valid JSON with no whitspace.
func (n *Node) String() string {
b := &strings.Builder{}
_, err := n.format(b, "", "", "", "")
if err != nil {
return ""
}
return b.String()
}
// MarshalJSON implements the json.Mashaler interface for Node
func (n *Node) MarshalJSON() ([]byte, error) {
b := &bytes.Buffer{}
_, err := n.format(b, "", "", " ", " ")
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
// UnmarshalJSON implements the json.Unmashaler interface for Node
func (n *Node) UnmarshalJSON(data []byte) error {
m, err := NewJSON(data)
if err != nil {
return err
}
*n = *m
return nil
}
// WriteJSON writes the AST hold by n to w with the same representation as
// n.String() and no whitspace.
func (n *Node) WriteJSON(w io.Writer) (int, error) {
return n.format(w, "", "", "", "")
}
// WriteIndent writes the AST hold by n to w with the given indent
// (preferably spaces or a tab).
func (n *Node) WriteIndent(w io.Writer, indent string) (int, error) {
return n.format(w, indent, "\n", "", " ")
}
// JSON2Go reads contents from n and writes them into val.
// val has to be a pointer value and may panic if types don't match.
func (n *Node) JSON2Go(val interface{}) (err error) {
return json2Go(n, val, false)
}
// AddChildren appends nn nodes to the Array or Object n.
// It panics if n is not of the two mentioned types or if appended values
// in an object don't have keys.
func (n *Node) AddChildren(nn ...KeyNode) {
if n.jsonType == Object {
for _, n := range nn {
if n.Key == "" {
panic("empty key for object value")
}
}
n.value = append(n.value.([]KeyNode), nn...)
} else if n.jsonType == Array {
for _, m := range nn {
n.value = append(n.value.([]*Node), m.Node)
}
} else {
panic(errors.Wrapf(ErrNotArrayOrObject, "n is %s", n.jsonType))
}
}
// GetChild returns the node specifiend by name.
// The key "" always returns the node itself.
func (n *Node) GetChild(name string) (*Node, bool) {
keys := strings.Split(name, ".")
if len(keys) == 1 && keys[0] == "" {
return n, true
}
switch n.jsonType {
case Object:
kn := n.value.([]KeyNode)
for i := range kn {
if kn[i].Key == keys[0] {
return kn[i].GetChild(strings.Join(keys[1:], "."))
}
}
return nil, false
case Array:
i, err := strconv.Atoi(keys[0])
if err != nil {
return nil, false
}
nn := n.value.([]*Node)
if len(nn) < i {
return nil, false
}
return nn[i].GetChild(strings.Join(keys[1:], "."))
default:
return nil, false
//panic(errors.Wrapf(ErrNotArrayOrObject, "is %s", n.jsonType))
}
}
// SetChild adds or replaces the child key of n with val.
// SetChild does not add multiple level of objects.
// SetChild panics a to extended object is not array or object.
func (n *Node) SetChild(kn KeyNode) error {
m, ok := n.GetChild(kn.Key)
_ = strings.Split(kn.Key, ".")
if ok {
m.jsonType = kn.Node.jsonType
m.value = kn.Node.value
return nil
}
/*
if len(keys) > 1 {
m, ok = n.GetChild(keys[len(keys)-2])
if ok {
if m.jsonType == Array {
idx, err := strconv.Atoi(keys[len(keys)-1])
if err != nil {
return err
}
if m.Len() < idx+1 {
return fmt.Errorf("too short")
}
} else if m.jsonType == Object {
}
return ErrNotArrayOrObject
}
}
*/
return ErrNotArrayOrObject
}
// RemoveChild removes key from the ast corrctly reducing arrays
func (n *Node) RemoveChild(key string) error {
keys := strings.Split(key, ".")
if keys[0] == "" {
return fmt.Errorf("empty key supplied")
}
if len(keys) > 1 {
var ok bool
n, ok = n.GetChild(strings.Join(keys[:len(keys)-1], "."))
if !ok {
return fmt.Errorf("Node n does not have child %s", key)
}
}
if n.jsonType == Object {
nn := n.value.([]KeyNode)
for i, m := range nn {
if keys[0] == m.Key {
m.parent = nil
n.value = append(nn[:i], nn[i+1:]...)
return nil
}
}
return fmt.Errorf("Node n does not have child %s", key)
} else if n.jsonType == Array {
i, err := strconv.Atoi(keys[0])
if err != nil {
return fmt.Errorf("not-a-number key in array")
}
nn := n.value.([]*Node)
if i >= len(nn) {
return fmt.Errorf("index out of range")
}
nn[i].parent = nil
n.value = append(nn[:i], nn[i+1:]...)
return nil
} else {
return errors.Wrapf(ErrNotArrayOrObject, "in %s", n.jsonType)
}
}
// GetChildrenKeys returns a slice of all keys an Object or array holds.
// It is nil if n is not array or object and is not nil but is non-nil with
// a lengh of 0 if n is an empty array or object.
func (n *Node) GetChildrenKeys() []string {
switch n.Type() {
case Object:
nn := n.value.([]KeyNode)
ss := make([]string, len(nn))
for i, m := range nn {
ss[i] = m.Key
}
return ss
case Array:
nn := n.value.([]*Node)
ss := make([]string, len(nn))
for i := range nn {
ss[i] = strconv.Itoa(i)
}
return ss
default:
return nil
}
}
// Copy creates a deep copy of a Node.
func (n *Node) Copy() *Node {
switch n.jsonType {
case Null, Bool, Number, String:
return &Node{jsonType: n.jsonType, value: n.value}
case Array:
nn := n.value.([]*Node)
mm := make([]*Node, len(nn))
o := &Node{jsonType: Array, value: mm}
for i, m := range nn {
mm[i] = m.Copy()
mm[i].parent = o
}
return o
case Object:
kn := n.value.([]KeyNode)
mm := make([]KeyNode, len(kn))
o := &Node{jsonType: Object, value: mm}
for i, m := range kn {
mm[i].Key = m.Key
mm[i].Node = m.Copy()
mm[i].parent = o
}
return o
default:
return nil
}
}
// Len gives the length of an array or items in an object
func (n *Node) Len() int {
switch n.Type() {
case Array:
return len(n.value.([]*Node))
case Object:
return len(n.value.([]KeyNode))
case Error:
return 0
default:
return 1
}
}
// Total returns the number of total nodes hold by n
func (n *Node) Total() int {
switch n.Type() {
case Array:
i := 0
for _, eml := range n.value.([]*Node) {
i += eml.Total()
}
return i + 1
case Object:
i := 0
for _, eml := range n.value.([]KeyNode) {
i += eml.Total()
}
return i + 1
default:
return n.Len()
}
}
// like bytes.Repeat but with a initial buffer
func bytesRepeatBuf(buf []byte, rep string, num int) []byte {
if num < 0 {
panic("negative num value in bytesRepeatBuf")
}
for i := 0; i < num; i++ {
buf = append(buf, rep...)
}
return buf
}
// format writes a valid json representation to w with prefix as indent,
// postfix after values or opening objects/arrays, colonSep after keys and
// commaSep after each comma.
func (n *Node) format(w io.Writer, prefix, postfix, commaSep, colonSep string) (int, error) {
if n == nil {
return 0, fmt.Errorf("<nil>")
}
var (
inner func(int) error
m, o *Node = n, nil
buf = make([]byte, 0, 1024)
)
inner = func(level int) error { // closure with single buffer
if !isValid(m) {
return fmt.Errorf("format; assertion failure")
}
switch m.jsonType {
case Null:
buf = append(buf, "null"...)
return nil
case Bool:
if m.value.(bool) {
buf = append(buf, "true"...)
return nil
}
buf = append(buf, "false"...)
return nil
case Number:
buf = strconv.AppendFloat(buf, m.value.(float64), 'g', -1, 64)
return nil
case String:
buf = append(buf, (`"` + m.value.(string) + `"`)...)
return nil
case Array:
cc := m.value.([]*Node)
if len(cc) == 0 {
buf = bytesRepeatBuf(buf, prefix, level)
buf = append(buf, "[]"...)
return nil
}
buf = append(buf, ("[" + postfix)...)
for _, c := range cc[:len(cc)-1] {
buf = bytesRepeatBuf(buf, prefix, level+1)
m, o = c, m
err := inner(level + 1)
if err != nil {
return err
}
m = o
buf = append(buf, ("," + commaSep + postfix)...)
}
buf = bytesRepeatBuf(buf, prefix, level+1)
m, o = cc[len(cc)-1], m
err := inner(level + 1)
if err != nil {
return err
}
m = o
buf = append(buf, postfix...)
buf = bytesRepeatBuf(buf, prefix, level)
buf = append(buf, ']')
return nil
case Object:
cc := m.value.([]KeyNode)
if len(cc) == 0 {
buf = bytesRepeatBuf(buf, prefix, level)
buf = append(buf, "{}"...)
return nil
}
buf = append(buf, ("{" + postfix)...)
for _, c := range cc[:len(cc)-1] {
buf = bytesRepeatBuf(buf, prefix, level+1)
buf = append(buf, ("\"" + c.Key + "\":" + colonSep)...)
m, o = c.Node, m
err := inner(level + 1)
if err != nil {
return err
}
buf = append(buf, ("," + commaSep + postfix)...)
m = o
}
buf = bytesRepeatBuf(buf, prefix, level+1)
buf = append(buf, ("\"" + cc[len(cc)-1].Key + "\":" + colonSep)...)
m, o = cc[len(cc)-1].Node, m
err := inner(level + 1)
if err != nil {
return err
}
m = o
buf = append(buf, postfix...)
buf = bytesRepeatBuf(buf, prefix, level)
buf = append(buf, "}"...)
return nil
case Error:
buf = append(buf, "<error>"...)
return nil
default:
return fmt.Errorf("node of unknown type: %#v", m)
}
}
err := inner(0)
if err != nil {
return 0, err
}
return w.Write(buf)
}
// TODO(JMH): add case insensitive match on struct tags
func json2Go(n *Node, val interface{}, stringify bool) (err error) {
v := reflect.ValueOf(val)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("v %v not pointer", v)
}
switch inner := v.Elem(); inner.Kind() {
case reflect.Bool:
if n.jsonType != Bool {
return fmt.Errorf("mismatched type: want Bool got %s", n.jsonType)
}
inner.SetBool(n.value.(bool))
return nil
case reflect.Float64, reflect.Float32,
reflect.Int, reflect.Int64, reflect.Int32,
reflect.Uint, reflect.Uint64, reflect.Uint32:
if n.jsonType != Number {
return fmt.Errorf("mismatched type: want Number got %s", n.jsonType)
}
inner.Set(reflect.ValueOf(n.value).Convert(inner.Type()))
return nil
case reflect.String:
if !stringify {
if n.jsonType != String {
return fmt.Errorf("mismatched type: want String got %s", n.jsonType)
}
inner.SetString(n.value.(string))
return nil
}
switch n.jsonType {
case Null:
inner.SetString("null")
return nil
case Bool:
if n.value.(bool) {
inner.SetString("true")
return nil
}
inner.SetString("false")
return nil
case Number:
inner.SetString(strconv.FormatFloat(n.value.(float64), 'b', -1, 64))
return nil
case String:
inner.SetString(n.value.(string))
return nil
default:
return fmt.Errorf("mismatched type: can not convert %s to string", n.jsonType)
}
case reflect.Slice:
if n.jsonType != Array {
return fmt.Errorf("mismatched type: want Array got %s", n.jsonType)
}
t := inner.Type().Elem() // interface{}
nn := n.value.([]*Node)
defer func() {
if e := recover(); e != nil {
switch val := e.(type) {
case error:
err = val
case string:
err = fmt.Errorf(val)
default:
err = fmt.Errorf("incompatible types in array")
}
}
}()
for _, m := range nn {
inner.Set(reflect.Append(inner, reflect.
ValueOf(m.value).
Convert(t)))
}
return nil
case reflect.Struct:
t := inner.Type()
for i := 0; i < t.NumField(); i++ {
elemT := t.Field(i)
if r, _ := utf8.DecodeRuneInString(elemT.Name); !unicode.IsUpper(r) {
continue
}
tags := strings.Split(elemT.Tag.Get("json"), ",")
if len(tags) == 1 && tags[0] == "-" {
continue
}
key := tags[0]
if key == "" {
key = elemT.Name
}
elm, ok := n.GetChild(key)
// case insensitive match without struct tag-name
if !ok && tags[0] == "" {
keys := n.GetChildrenKeys()
ss := make([]string, len(keys))
for i, k := range keys {
ss[i] = strings.ToUpper(k)
}
for i, s := range ss {
if s == strings.ToUpper(key) {
key = keys[i]
break
}
}
elm, ok = n.GetChild(key)
}
omitempty := false
for _, tag := range tags[1:] {
if tag == "omitempty" {
omitempty = true
break
}
}
// return if not found and not omitempty
if !ok && omitempty {
continue
} else if !ok {
return fmt.Errorf("key \"%s\" in json missing %t", key, omitempty)
}
strfy := false
for _, tag := range tags[1:] {
if tag == "string" {
strfy = true
}
}
err = json2Go(elm, inner.Field(i).Addr().Interface(), strfy)
if err != nil {
return err
}
}
return nil
case reflect.Map:
if n.jsonType != Object {
return fmt.Errorf("mismatched type: want Object got %s", n.jsonType)
}
t := inner.Type()
defer func() {
if e := recover(); e != nil {
switch val := e.(type) {
case error:
err = val
case string:
err = fmt.Errorf(val)
default:
err = fmt.Errorf("incompatible types in array")
}
}
}()
for _, nn := range n.value.([]KeyNode) {
inner.SetMapIndex(reflect.ValueOf(nn.Key), reflect.
ValueOf(nn.value).
Convert(t.Elem()))
}
return nil
default:
return fmt.Errorf("invalid type supplied")
}
}
func isValid(n *Node) bool {
if n == nil {
return false
}
switch n.value.(type) {
case nil:
return n.jsonType == Null || n.jsonType == Error
case bool:
return n.jsonType == Bool
case float64:
return n.jsonType == Number
case string:
return n.jsonType == String
case []*Node:
return n.jsonType == Array
case []KeyNode:
return n.jsonType == Object
default:
return false
}
}
func cyclicTest(n *Node) bool {
if n.parent == nil {
return true
}
switch n.parent.jsonType {
case Array:
nn := n.parent.value.([]*Node)
for i := range nn {
if nn[i] == n {
return true
}
}
return false
case Object:
kn := n.parent.value.([]KeyNode)
for i := range kn {
if kn[i].Node == n {
return true
}
}
return false
default:
return true
}
}
// -> Root
func maxParent(n *Node) *Node {
if n == nil || n.parent == nil {
return nil
}
m := n.parent
for ; m.parent != nil; m = m.parent {
}
return m
}