-
Notifications
You must be signed in to change notification settings - Fork 14
/
json_parse_node.go
695 lines (640 loc) · 16 KB
/
json_parse_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
// Package jsonserialization is the default Kiota serialization implementation for JSON.
// It relies on the standard Go JSON library.
package jsonserialization
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"time"
"github.com/google/uuid"
abstractions "github.com/microsoft/kiota-abstractions-go"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)
// JsonParseNode is a ParseNode implementation for JSON.
type JsonParseNode struct {
value interface{}
onBeforeAssignFieldValues absser.ParsableAction
onAfterAssignFieldValues absser.ParsableAction
}
// NewJsonParseNode creates a new JsonParseNode.
func NewJsonParseNode(content []byte) (*JsonParseNode, error) {
if len(content) == 0 {
return nil, errors.New("content is empty")
}
if !json.Valid(content) {
return nil, errors.New("invalid json type")
}
decoder := json.NewDecoder(bytes.NewReader(content))
value, err := loadJsonTree(decoder)
return value, err
}
func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch token.(type) {
case json.Delim:
switch token.(json.Delim) {
case '{':
v := make(map[string]*JsonParseNode)
for decoder.More() {
key, err := decoder.Token()
if err != nil {
return nil, err
}
keyStr, ok := key.(string)
if !ok {
return nil, errors.New("key is not a string")
}
childNode, err := loadJsonTree(decoder)
if err != nil {
return nil, err
}
v[keyStr] = childNode
}
decoder.Token() // skip the closing curly
result := &JsonParseNode{value: v}
return result, nil
case '[':
v := make([]*JsonParseNode, 0)
for decoder.More() {
childNode, err := loadJsonTree(decoder)
if err != nil {
return nil, err
}
v = append(v, childNode)
}
decoder.Token() // skip the closing bracket
result := &JsonParseNode{value: v}
return result, nil
case ']':
case '}':
}
case json.Number:
number := token.(json.Number)
i, err := number.Int64()
c := &JsonParseNode{}
if err == nil {
c.setValue(&i)
} else {
f, err := number.Float64()
if err == nil {
c.setValue(&f)
} else {
return nil, err
}
}
return c, nil
case string:
v := token.(string)
c := &JsonParseNode{}
c.setValue(&v)
return c, nil
case bool:
c := &JsonParseNode{}
v := token.(bool)
c.setValue(&v)
return c, nil
case int8:
c := &JsonParseNode{}
v := token.(int8)
c.setValue(&v)
return c, nil
case byte:
c := &JsonParseNode{}
v := token.(byte)
c.setValue(&v)
return c, nil
case float64:
c := &JsonParseNode{}
v := token.(float64)
c.setValue(&v)
return c, nil
case float32:
c := &JsonParseNode{}
v := token.(float32)
c.setValue(&v)
return c, nil
case int32:
c := &JsonParseNode{}
v := token.(int32)
c.setValue(&v)
return c, nil
case int64:
c := &JsonParseNode{}
v := token.(int64)
c.setValue(&v)
return c, nil
case nil:
return nil, nil
default:
}
}
return nil, nil
}
// SetValue is obsolete, parse nodes are not meant to be settable externally
func (n *JsonParseNode) SetValue(value interface{}) {
n.setValue(value)
}
// setValue sets the value represented by the node
func (n *JsonParseNode) setValue(value interface{}) {
n.value = value
}
// GetChildNode returns a new parse node for the given identifier.
func (n *JsonParseNode) GetChildNode(index string) (absser.ParseNode, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if index == "" {
return nil, errors.New("index is empty")
}
childNodes, ok := n.value.(map[string]*JsonParseNode)
if !ok || len(childNodes) == 0 {
return nil, nil
}
childNode := childNodes[index]
if childNode != nil {
err := childNode.SetOnBeforeAssignFieldValues(n.GetOnBeforeAssignFieldValues())
if err != nil {
return nil, err
}
err = childNode.SetOnAfterAssignFieldValues(n.GetOnAfterAssignFieldValues())
if err != nil {
return nil, err
}
}
return childNode, nil
}
// GetObjectValue returns the Parsable value from the node.
func (n *JsonParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Parsable, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if ctor == nil {
return nil, errors.New("constructor is nil")
}
result, err := ctor(n)
if err != nil {
return nil, err
}
_, isUntypedNode := result.(absser.UntypedNodeable)
if isUntypedNode {
switch value := n.value.(type) {
case *bool:
return absser.NewUntypedBoolean(*value), nil
case *string:
return absser.NewUntypedString(*value), nil
case *float32:
return absser.NewUntypedFloat(*value), nil
case *float64:
return absser.NewUntypedDouble(*value), nil
case *int32:
return absser.NewUntypedInteger(*value), nil
case *int64:
return absser.NewUntypedLong(*value), nil
case nil:
return absser.NewUntypedNull(), nil
case map[string]*JsonParseNode:
properties := make(map[string]absser.UntypedNodeable)
for key, value := range value {
parsable, err := value.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return nil, errors.New("cannot parse object value")
}
if parsable == nil {
parsable = absser.NewUntypedNull()
}
property, ok := parsable.(absser.UntypedNodeable)
if ok {
properties[key] = property
}
}
return absser.NewUntypedObject(properties), nil
case []*JsonParseNode:
collection := make([]absser.UntypedNodeable, len(value))
for index, node := range value {
parsable, err := node.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return nil, errors.New("cannot parse object value")
}
if parsable == nil {
parsable = absser.NewUntypedNull()
}
property, ok := parsable.(absser.UntypedNodeable)
if ok {
collection[index] = property
}
}
return absser.NewUntypedArray(collection), nil
default:
return absser.NewUntypedNode(value), nil
}
}
abstractions.InvokeParsableAction(n.GetOnBeforeAssignFieldValues(), result)
properties, ok := n.value.(map[string]*JsonParseNode)
fields := result.GetFieldDeserializers()
if ok && len(properties) != 0 {
itemAsHolder, isHolder := result.(absser.AdditionalDataHolder)
var itemAdditionalData map[string]interface{}
if isHolder {
itemAdditionalData = itemAsHolder.GetAdditionalData()
if itemAdditionalData == nil {
itemAdditionalData = make(map[string]interface{})
itemAsHolder.SetAdditionalData(itemAdditionalData)
}
}
for key, value := range properties {
field := fields[key]
if value != nil {
err := value.SetOnBeforeAssignFieldValues(n.GetOnBeforeAssignFieldValues())
if err != nil {
return nil, err
}
err = value.SetOnAfterAssignFieldValues(n.GetOnAfterAssignFieldValues())
if err != nil {
return nil, err
}
}
if field == nil {
if value != nil && isHolder {
rawValue, err := value.GetRawValue()
if err != nil {
return nil, err
}
itemAdditionalData[key] = rawValue
}
} else {
err := field(value)
if err != nil {
return nil, err
}
}
}
}
abstractions.InvokeParsableAction(n.GetOnAfterAssignFieldValues(), result)
return result, nil
}
// GetCollectionOfObjectValues returns the collection of Parsable values from the node.
func (n *JsonParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory) ([]absser.Parsable, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if ctor == nil {
return nil, errors.New("ctor is nil")
}
nodes, ok := n.value.([]*JsonParseNode)
if !ok {
return nil, errors.New("value is not a collection")
}
result := make([]absser.Parsable, len(nodes))
for i, v := range nodes {
if v != nil {
val, err := (*v).GetObjectValue(ctor)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
}
return result, nil
}
// GetCollectionOfPrimitiveValues returns the collection of primitive values from the node.
func (n *JsonParseNode) GetCollectionOfPrimitiveValues(targetType string) ([]interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if targetType == "" {
return nil, errors.New("targetType is empty")
}
nodes, ok := n.value.([]*JsonParseNode)
if !ok {
return nil, errors.New("value is not a collection")
}
result := make([]interface{}, len(nodes))
for i, v := range nodes {
if v != nil {
val, err := v.getPrimitiveValue(targetType)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
}
return result, nil
}
func (n *JsonParseNode) getPrimitiveValue(targetType string) (interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
switch targetType {
case "string":
return n.GetStringValue()
case "bool":
return n.GetBoolValue()
case "uint8":
return n.GetInt8Value()
case "byte":
return n.GetByteValue()
case "float32":
return n.GetFloat32Value()
case "float64":
return n.GetFloat64Value()
case "int32":
return n.GetInt32Value()
case "int64":
return n.GetInt64Value()
case "time":
return n.GetTimeValue()
case "timeonly":
return n.GetTimeOnlyValue()
case "dateonly":
return n.GetDateOnlyValue()
case "isoduration":
return n.GetISODurationValue()
case "uuid":
return n.GetUUIDValue()
case "base64":
return n.GetByteArrayValue()
default:
return nil, fmt.Errorf("targetType %s is not supported", targetType)
}
}
// GetCollectionOfEnumValues returns the collection of Enum values from the node.
func (n *JsonParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if parser == nil {
return nil, errors.New("parser is nil")
}
nodes, ok := n.value.([]*JsonParseNode)
if !ok {
return nil, errors.New("value is not a collection")
}
result := make([]interface{}, len(nodes))
for i, v := range nodes {
if v != nil {
val, err := v.GetEnumValue(parser)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
}
return result, nil
}
// GetStringValue returns a String value from the nodes.
func (n *JsonParseNode) GetStringValue() (*string, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
val, ok := n.value.(*string)
if !ok {
return nil, fmt.Errorf("type '%T' is not compatible with type string", n.value)
}
return val, nil
}
// GetBoolValue returns a Bool value from the nodes.
func (n *JsonParseNode) GetBoolValue() (*bool, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
val, ok := n.value.(*bool)
if !ok {
return nil, fmt.Errorf("type '%T' is not compatible with type bool", n.value)
}
return val, nil
}
// GetInt8Value returns a int8 value from the nodes.
func (n *JsonParseNode) GetInt8Value() (*int8, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val int8
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetBoolValue returns a Bool value from the nodes.
func (n *JsonParseNode) GetByteValue() (*byte, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val byte
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetFloat32Value returns a Float32 value from the nodes.
func (n *JsonParseNode) GetFloat32Value() (*float32, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val float32
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetFloat64Value returns a Float64 value from the nodes.
func (n *JsonParseNode) GetFloat64Value() (*float64, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val float64
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetInt32Value returns a Int32 value from the nodes.
func (n *JsonParseNode) GetInt32Value() (*int32, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val int32
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetInt64Value returns a Int64 value from the nodes.
func (n *JsonParseNode) GetInt64Value() (*int64, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val int64
if err := as(n.value, &val); err != nil {
return nil, err
}
return &val, nil
}
// GetTimeValue returns a Time value from the nodes.
func (n *JsonParseNode) GetTimeValue() (*time.Time, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
// if string does not have timezone information, add local timezone
if len(*v) == 19 {
*v = *v + time.Now().Format("-07:00")
}
parsed, err := time.Parse(time.RFC3339, *v)
return &parsed, err
}
// GetISODurationValue returns a ISODuration value from the nodes.
func (n *JsonParseNode) GetISODurationValue() (*absser.ISODuration, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseISODuration(*v)
}
// GetTimeOnlyValue returns a TimeOnly value from the nodes.
func (n *JsonParseNode) GetTimeOnlyValue() (*absser.TimeOnly, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseTimeOnly(*v)
}
// GetDateOnlyValue returns a DateOnly value from the nodes.
func (n *JsonParseNode) GetDateOnlyValue() (*absser.DateOnly, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseDateOnly(*v)
}
// GetUUIDValue returns a UUID value from the nodes.
func (n *JsonParseNode) GetUUIDValue() (*uuid.UUID, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
parsed, err := uuid.Parse(*v)
return &parsed, err
}
// GetEnumValue returns a Enum value from the nodes.
func (n *JsonParseNode) GetEnumValue(parser absser.EnumFactory) (interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if parser == nil {
return nil, errors.New("parser is nil")
}
s, err := n.GetStringValue()
if err != nil {
return nil, err
}
if s == nil {
return nil, nil
}
return parser(*s)
}
// GetByteArrayValue returns a ByteArray value from the nodes.
func (n *JsonParseNode) GetByteArrayValue() ([]byte, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
s, err := n.GetStringValue()
if err != nil {
return nil, err
}
if s == nil {
return nil, nil
}
return base64.StdEncoding.DecodeString(*s)
}
// GetRawValue returns a ByteArray value from the nodes.
func (n *JsonParseNode) GetRawValue() (interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
switch v := n.value.(type) {
case *JsonParseNode:
return v.GetRawValue()
case []*JsonParseNode:
result := make([]interface{}, len(v))
for i, x := range v {
val, err := x.GetRawValue()
if err != nil {
return nil, err
}
result[i] = val
}
return result, nil
case map[string]*JsonParseNode:
m := make(map[string]interface{})
for key, element := range v {
elementVal, err := element.GetRawValue()
if err != nil {
return nil, err
}
m[key] = elementVal
}
return m, nil
default:
return n.value, nil
}
}
func (n *JsonParseNode) GetOnBeforeAssignFieldValues() absser.ParsableAction {
return n.onBeforeAssignFieldValues
}
func (n *JsonParseNode) SetOnBeforeAssignFieldValues(action absser.ParsableAction) error {
n.onBeforeAssignFieldValues = action
return nil
}
func (n *JsonParseNode) GetOnAfterAssignFieldValues() absser.ParsableAction {
return n.onAfterAssignFieldValues
}
func (n *JsonParseNode) SetOnAfterAssignFieldValues(action absser.ParsableAction) error {
n.onAfterAssignFieldValues = action
return nil
}