forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
949 lines (888 loc) · 19.6 KB
/
node.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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
package ajson
import (
"math"
"sort"
"strconv"
"sync/atomic"
)
// Node is a main struct, presents any type of JSON node.
// Available types are:
//
// const (
// Null NodeType = iota
// Numeric
// String
// Bool
// Array
// Object
// )
//
// Every type has its own methods to be called.
// Every Node contains link to a byte data, parent and children, also calculated type of value, atomic value and internal information.
type Node struct {
parent *Node
children map[string]*Node
key *string
index *int
_type NodeType
data *[]byte
borders [2]int
value atomic.Value
dirty bool
}
// NodeType is a kind of reflection of JSON type to a type of golang
type NodeType int32
// Reflections:
//
// Null = nil.(interface{})
// Numeric = float64
// String = string
// Bool = bool
// Array = []*Node
// Object = map[string]*Node
//
const (
// Null is reflection of nil.(interface{})
Null NodeType = iota
// Numeric is reflection of float64
Numeric
// String is reflection of string
String
// Bool is reflection of bool
Bool
// Array is reflection of []*Node
Array
// Object is reflection of map[string]*Node
Object
)
// NullNode is constructor for Node with Null value
func NullNode(key string) *Node {
return &Node{
_type: Null,
key: &key,
dirty: true,
}
}
// NumericNode is constructor for Node with a Numeric value
func NumericNode(key string, value float64) (current *Node) {
current = &Node{
_type: Numeric,
key: &key,
dirty: true,
}
current.value.Store(value)
return
}
// StringNode is constructor for Node with a String value
func StringNode(key string, value string) (current *Node) {
current = &Node{
_type: String,
key: &key,
dirty: true,
}
current.value.Store(value)
return
}
// BoolNode is constructor for Node with a Bool value
func BoolNode(key string, value bool) (current *Node) {
current = &Node{
_type: Bool,
key: &key,
dirty: true,
}
current.value.Store(value)
return
}
// ArrayNode is constructor for Node with an Array value
func ArrayNode(key string, value []*Node) (current *Node) {
current = &Node{
data: nil,
_type: Array,
key: &key,
dirty: true,
}
current.children = make(map[string]*Node, len(value))
if value != nil {
current.value.Store(value)
for i, val := range value {
var index = i
current.children[strconv.Itoa(i)] = val
val.parent = current
val.index = &index
}
}
return
}
// ObjectNode is constructor for Node with an Object value
func ObjectNode(key string, value map[string]*Node) (current *Node) {
current = &Node{
_type: Object,
key: &key,
children: value,
dirty: true,
}
if value != nil {
current.value.Store(value)
for key, val := range value {
val.parent = current
val.key = &key
}
} else {
current.children = make(map[string]*Node)
}
return
}
func newNode(parent *Node, buf *buffer, _type NodeType, key **string) (current *Node, err error) {
current = &Node{
parent: parent,
data: &buf.data,
borders: [2]int{buf.index, 0},
_type: _type,
key: *key,
dirty: false,
}
if _type == Object || _type == Array {
current.children = make(map[string]*Node)
}
if parent != nil {
if parent.IsArray() {
size := len(parent.children)
current.index = &size
parent.children[strconv.Itoa(size)] = current
} else if parent.IsObject() {
if *key == nil {
err = errorSymbol(buf)
} else {
parent.children[**key] = current
*key = nil
}
} else {
err = errorSymbol(buf)
}
}
return
}
func valueNode(parent *Node, key string, _type NodeType, value interface{}) (current *Node) {
current = &Node{
parent: parent,
data: nil,
borders: [2]int{0, 0},
_type: _type,
key: &key,
dirty: true,
}
if value != nil {
current.value.Store(value)
}
return
}
// Parent returns link to the parent of current node, nil for root
func (n *Node) Parent() *Node {
if n == nil {
return nil
}
return n.parent
}
// Source returns slice of bytes, which was identified to be current node
func (n *Node) Source() []byte {
if n == nil {
return nil
}
if n.ready() && !n.dirty && n.data != nil {
return (*n.data)[n.borders[0]:n.borders[1]]
}
return nil
}
// String is implementation of Stringer interface, returns string based on source part
func (n *Node) String() string {
if n == nil {
return ""
}
if n.ready() && !n.dirty {
return string(n.Source())
}
val, err := Marshal(n)
if err != nil {
return "Error: " + err.Error()
}
return string(val)
}
// Type will return type of current node
func (n *Node) Type() NodeType {
if n == nil {
return Null
}
return n._type
}
// Key will return key of current node, please check, that parent of this node has an Object type
func (n *Node) Key() string {
if n == nil {
return ""
}
if n.key == nil {
return ""
}
return *n.key
}
// Index will return index of current node, please check, that parent of this node has an Array type
func (n *Node) Index() int {
if n == nil {
return -1
}
if n.index == nil {
return -1
}
return *n.index
}
// Size will return count of children of current node, please check, that parent of this node has an Array type
func (n *Node) Size() int {
if n == nil {
return 0
}
return len(n.children)
}
// Keys will return count all keys of children of current node, please check, that parent of this node has an Object type
func (n *Node) Keys() (result []string) {
if n == nil {
return nil
}
result = make([]string, 0, len(n.children))
for key := range n.children {
result = append(result, key)
}
return
}
// IsArray returns true if current node is Array
func (n *Node) IsArray() bool {
if n == nil {
return false
}
return n._type == Array
}
// IsObject returns true if current node is Object
func (n *Node) IsObject() bool {
if n == nil {
return false
}
return n._type == Object
}
// IsNull returns true if current node is Null
func (n *Node) IsNull() bool {
if n == nil {
return false
}
return n._type == Null
}
// IsNumeric returns true if current node is Numeric
func (n *Node) IsNumeric() bool {
if n == nil {
return false
}
return n._type == Numeric
}
// IsString returns true if current node is String
func (n *Node) IsString() bool {
if n == nil {
return false
}
return n._type == String
}
// IsBool returns true if current node is Bool
func (n *Node) IsBool() bool {
if n == nil {
return false
}
return n._type == Bool
}
// Value is calculating and returns a value of current node.
//
// It returns nil, if current node type is Null.
//
// It returns float64, if current node type is Numeric.
//
// It returns string, if current node type is String.
//
// It returns bool, if current node type is Bool.
//
// It returns []*Node, if current node type is Array.
//
// It returns map[string]*Node, if current node type is Object.
//
// BUT! Current method doesn't calculate underlying nodes (use method Node.Unpack for that).
//
// Value will be calculated only once and saved into atomic.Value.
func (n *Node) Value() (value interface{}, err error) {
if n == nil {
return nil, errorUnparsed()
}
switch n._type {
case Null:
return n.GetNull()
case Numeric:
return n.GetNumeric()
case String:
return n.GetString()
case Bool:
return n.GetBool()
case Array:
return n.GetArray()
case Object:
return n.GetObject()
}
return nil, errorType()
}
func (n *Node) getValue() (value interface{}, err error) {
value = n.value.Load()
if value == nil {
switch n._type {
case Null:
return nil, nil
case Numeric:
value, err = strconv.ParseFloat(string(n.Source()), 64)
if err != nil {
return
}
n.value.Store(value)
case String:
var ok bool
value, ok = unquote(n.Source(), quotes)
if !ok {
return "", errorAt(n.borders[0], (*n.data)[n.borders[0]])
}
n.value.Store(value)
case Bool:
if len(n.Source()) == 0 {
return nil, errorUnparsed()
}
b := n.Source()[0]
value = b == 't' || b == 'T'
n.value.Store(value)
case Array:
children := make([]*Node, len(n.children))
for _, child := range n.children {
children[*child.index] = child
}
value = children
n.value.Store(value)
case Object:
result := make(map[string]*Node)
for key, child := range n.children {
result[key] = child
}
value = result
n.value.Store(value)
}
}
return
}
// GetNull returns nil, if current type is Null, else: WrongType error
func (n *Node) GetNull() (interface{}, error) {
if n == nil {
return nil, errorUnparsed()
}
if n._type != Null {
return nil, errorType()
}
return nil, nil
}
// GetNumeric returns float64, if current type is Numeric, else: WrongType error
func (n *Node) GetNumeric() (value float64, err error) {
if n == nil {
return 0, errorUnparsed()
}
if n._type != Numeric {
return value, errorType()
}
iValue, err := n.getValue()
if err != nil {
return 0, err
}
value, ok := iValue.(float64)
if !ok {
return value, errorType()
}
return value, nil
}
// GetString returns string, if current type is String, else: WrongType error
func (n *Node) GetString() (value string, err error) {
if n == nil {
return "", errorUnparsed()
}
if n._type != String {
return value, errorType()
}
iValue, err := n.getValue()
if err != nil {
return "", err
}
value, ok := iValue.(string)
if !ok {
return value, errorType()
}
return value, nil
}
// GetBool returns bool, if current type is Bool, else: WrongType error
func (n *Node) GetBool() (value bool, err error) {
if n == nil {
return value, errorUnparsed()
}
if n._type != Bool {
return value, errorType()
}
iValue, err := n.getValue()
if err != nil {
return false, err
}
value, ok := iValue.(bool)
if !ok {
return value, errorType()
}
return value, nil
}
// GetArray returns []*Node, if current type is Array, else: WrongType error
func (n *Node) GetArray() (value []*Node, err error) {
if n == nil {
return nil, errorUnparsed()
}
if n._type != Array {
return value, errorType()
}
iValue, err := n.getValue()
if err != nil {
return nil, err
}
value, ok := iValue.([]*Node)
if !ok {
return value, errorType()
}
return value, nil
}
// GetObject returns map[string]*Node, if current type is Object, else: WrongType error
func (n *Node) GetObject() (value map[string]*Node, err error) {
if n == nil {
return nil, errorUnparsed()
}
if n._type != Object {
return value, errorType()
}
iValue, err := n.getValue()
if err != nil {
return nil, err
}
value, ok := iValue.(map[string]*Node)
if !ok {
return value, errorType()
}
return value, nil
}
// MustNull returns nil, if current type is Null, else: panic if error happened
func (n *Node) MustNull() (value interface{}) {
value, err := n.GetNull()
if err != nil {
panic(err)
}
return
}
// MustNumeric returns float64, if current type is Numeric, else: panic if error happened
func (n *Node) MustNumeric() (value float64) {
value, err := n.GetNumeric()
if err != nil {
panic(err)
}
return
}
// MustString returns string, if current type is String, else: panic if error happened
func (n *Node) MustString() (value string) {
value, err := n.GetString()
if err != nil {
panic(err)
}
return
}
// MustBool returns bool, if current type is Bool, else: panic if error happened
func (n *Node) MustBool() (value bool) {
value, err := n.GetBool()
if err != nil {
panic(err)
}
return
}
// MustArray returns []*Node, if current type is Array, else: panic if error happened
func (n *Node) MustArray() (value []*Node) {
value, err := n.GetArray()
if err != nil {
panic(err)
}
return
}
// MustObject returns map[string]*Node, if current type is Object, else: panic if error happened
func (n *Node) MustObject() (value map[string]*Node) {
value, err := n.GetObject()
if err != nil {
panic(err)
}
return
}
// Unpack will produce current node to it's interface, recursively with all underlying nodes (in contrast to Node.Value).
func (n *Node) Unpack() (value interface{}, err error) {
if n == nil {
return nil, errorUnparsed()
}
switch n._type {
case Null:
return nil, nil
case Numeric:
value, err = n.Value()
if _, ok := value.(float64); !ok {
return nil, errorType()
}
case String:
value, err = n.Value()
if _, ok := value.(string); !ok {
return nil, errorType()
}
case Bool:
value, err = n.Value()
if _, ok := value.(bool); !ok {
return nil, errorType()
}
case Array:
children := make([]interface{}, len(n.children))
for _, child := range n.children {
val, err := child.Unpack()
if err != nil {
return nil, err
}
children[*child.index] = val
}
value = children
case Object:
result := make(map[string]interface{})
for key, child := range n.children {
result[key], err = child.Unpack()
if err != nil {
return nil, err
}
}
value = result
}
return
}
// GetIndex will return child node of current array node. If current node is not Array, or index is unavailable, will return error
func (n *Node) GetIndex(index int) (*Node, error) {
if n == nil {
return nil, errorUnparsed()
}
if n._type != Array {
return nil, errorType()
}
if index < 0 {
index += len(n.children)
}
child, ok := n.children[strconv.Itoa(index)]
if !ok {
return nil, errorRequest("out of index %d", index)
}
return child, nil
}
// MustIndex will return child node of current array node. If current node is not Array, or index is unavailable, raise a panic
func (n *Node) MustIndex(index int) (value *Node) {
value, err := n.GetIndex(index)
if err != nil {
panic(err)
}
return
}
// GetKey will return child node of current object node. If current node is not Object, or key is unavailable, will return error
func (n *Node) GetKey(key string) (*Node, error) {
if n == nil {
return nil, errorUnparsed()
}
if n._type != Object {
return nil, errorType()
}
value, ok := n.children[key]
if !ok {
return nil, errorRequest("wrong key '%s'", key)
}
return value, nil
}
// MustKey will return child node of current object node. If current node is not Object, or key is unavailable, raise a panic
func (n *Node) MustKey(key string) (value *Node) {
value, err := n.GetKey(key)
if err != nil {
panic(err)
}
return
}
// HasKey will return boolean value, if current object node has custom key
func (n *Node) HasKey(key string) bool {
if n == nil {
return false
}
_, ok := n.children[key]
return ok
}
// Empty method check if current container node has no children
func (n *Node) Empty() bool {
if n == nil {
return false
}
return len(n.children) == 0
}
// Path returns full JsonPath of current Node
func (n *Node) Path() string {
if n == nil {
return ""
}
if n.parent == nil {
return "$"
}
if n.key != nil {
return n.parent.Path() + "['" + n.Key() + "']"
}
return n.parent.Path() + "[" + strconv.Itoa(n.Index()) + "]"
}
// Eq check if nodes value are the same
func (n *Node) Eq(node *Node) (result bool, err error) {
if n == nil || node == nil {
return false, errorUnparsed()
}
if n.Type() == node.Type() {
switch n.Type() {
case Bool:
lnum, rnum, err := _bools(n, node)
if err != nil {
return false, err
}
result = lnum == rnum
case Numeric:
lnum, rnum, err := _floats(n, node)
if err != nil {
return false, err
}
result = lnum == rnum
case String:
lnum, rnum, err := _strings(n, node)
if err != nil {
return false, err
}
result = lnum == rnum
case Null:
// Null type always is the same
result = true
case Array:
lnum, rnum, err := _arrays(n, node)
if err != nil {
return false, err
}
if len(lnum) == len(rnum) {
result = true
for i := range lnum {
result, err = lnum[i].Eq(rnum[i])
if err != nil {
return false, err
}
if !result {
return false, err
}
}
}
case Object:
lnum, rnum, err := _objects(n, node)
if err != nil {
return false, err
}
if len(lnum) == len(rnum) {
result = true
for i := range lnum {
element, ok := rnum[i]
if !ok {
return false, nil
}
result, err = lnum[i].Eq(element)
if err != nil {
return false, err
}
if !result {
return false, err
}
}
}
}
}
return
}
// Neq check if nodes value are not the same
func (n *Node) Neq(node *Node) (result bool, err error) {
result, err = n.Eq(node)
return !result, err
}
// Le check if nodes value is lesser than given
func (n *Node) Le(node *Node) (result bool, err error) {
if n == nil || node == nil {
return false, errorUnparsed()
}
if n.Type() == node.Type() {
switch n.Type() {
case Numeric:
lnum, rnum, err := _floats(n, node)
if err != nil {
return false, err
}
result = lnum < rnum
case String:
lnum, rnum, err := _strings(n, node)
if err != nil {
return false, err
}
result = lnum < rnum
default:
return false, errorType()
}
}
return
}
// Leq check if nodes value is lesser or equal than given
func (n *Node) Leq(node *Node) (result bool, err error) {
if n == nil || node == nil {
return false, errorUnparsed()
}
if n.Type() == node.Type() {
switch n.Type() {
case Numeric:
lnum, rnum, err := _floats(n, node)
if err != nil {
return false, err
}
result = lnum <= rnum
case String:
lnum, rnum, err := _strings(n, node)
if err != nil {
return false, err
}
result = lnum <= rnum
default:
return false, errorType()
}
}
return
}
// Ge check if nodes value is greater than given
func (n *Node) Ge(node *Node) (result bool, err error) {
if n == nil || node == nil {
return false, errorUnparsed()
}
if n.Type() == node.Type() {
switch n.Type() {
case Numeric:
lnum, rnum, err := _floats(n, node)
if err != nil {
return false, err
}
result = lnum > rnum
case String:
lnum, rnum, err := _strings(n, node)
if err != nil {
return false, err
}
result = lnum > rnum
default:
return false, errorType()
}
}
return
}
// Geq check if nodes value is greater or equal than given
func (n *Node) Geq(node *Node) (result bool, err error) {
if n == nil || node == nil {
return false, errorUnparsed()
}
if n.Type() == node.Type() {
switch n.Type() {
case Numeric:
lnum, rnum, err := _floats(n, node)
if err != nil {
return false, err
}
result = lnum >= rnum
case String:
lnum, rnum, err := _strings(n, node)
if err != nil {
return false, err
}
result = lnum >= rnum
default:
return false, errorType()
}
}
return
}
func (n *Node) ready() bool {
return n.borders[1] != 0
}
func (n *Node) isContainer() bool {
return n._type == Array || n._type == Object
}
func (n *Node) getInteger() (int, error) {
if !n.IsNumeric() {
return 0, errorType()
}
float, err := n.GetNumeric()
if err != nil {
return 0, err
}
if math.Mod(float, 1.0) != 0 {
return 0, errorRequest("node is not INT")
}
return int(float), nil
}
func (n *Node) getUInteger() (uint, error) {
result, err := n.getInteger()
if err != nil {
return 0, err
}
if result < 0 {
return 0, errorRequest("node is not UINT")
}
return uint(result), nil
}
// Inheritors return sorted by keys/index slice of children
func (n *Node) Inheritors() (result []*Node) {
if n == nil {
return nil
}
size := len(n.children)
if n.IsObject() {
result = make([]*Node, size)
keys := n.Keys()
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for i, key := range keys {
result[i] = n.children[key]
}
} else if n.IsArray() {
result = make([]*Node, size)
for _, element := range n.children {
result[*element.index] = element
}
}
return
}
// JSONPath evaluate path for current node
func (n *Node) JSONPath(path string) (result []*Node, err error) {
commands, err := ParseJSONPath(path)
if err != nil {
return nil, err
}
return ApplyJSONPath(n, commands)
}
// root returns the root node
func (n *Node) root() (node *Node) {
node = n
for node.parent != nil {
node = node.parent
}
return node
}