-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
856 lines (804 loc) · 20.9 KB
/
decode.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
package ini
import (
"encoding"
"encoding/base64"
"fmt"
"math"
"reflect"
"strconv"
"time"
)
const (
documentNode = 1 << iota
inheritNode
sectionNode
mappingNode
scalarNode
commentNode
)
type node struct {
kind int
line, column int
tag string
value string
children []*node
}
// ----------------------------------------------------------------------------
// Parser, produces a node tree out of a ini document.
type parser struct {
parser ini_parser_t
event ini_event_t
doc *node
}
func newParser(b []byte) *parser {
p := parser{}
if !ini_parser_initialize(&p.parser) {
panic("failed to initialize INI parser")
}
if len(b) == 0 {
b = []byte{}
}
ini_parser_set_input_string(&p.parser, b)
p.skip()
if p.event.typ != ini_DOCUMENT_START_EVENT {
panic("expected ini_DOCUMENT_START_EVENT, got " + p.event.event_type())
}
return &p
}
func (p *parser) destroy() {
if p.event.typ != ini_NO_EVENT {
ini_event_delete(&p.event)
}
ini_parser_delete(&p.parser)
}
func (p *parser) skip() {
if p.event.typ != ini_NO_EVENT {
if p.event.typ == ini_DOCUMENT_END_EVENT {
failf("attempted to go past the end of document; corrupted value?")
}
ini_event_delete(&p.event)
}
if !ini_parser_parse(&p.parser, &p.event) {
p.fail()
}
}
func (p *parser) fail() {
var where string
var line int
if p.parser.problem_mark.line != 0 {
line = p.parser.problem_mark.line
} else if p.parser.context_mark.line != 0 {
line = p.parser.context_mark.line
}
if line != 0 {
where = "line " + strconv.Itoa(line) + ": "
}
var msg string
if len(p.parser.problem) > 0 {
msg = p.parser.problem
} else {
msg = "unknown problem parsing INI content"
}
failf("%s%s", where, msg)
}
func (p *parser) parse() *node {
switch p.event.typ {
case ini_DOCUMENT_START_EVENT:
return p.document()
case ini_SECTION_INHERIT_EVENT:
return p.inherit()
case ini_SECTION_ENTRY_EVENT:
return p.section()
case ini_MAPPING_EVENT:
return p.mapping()
case ini_SCALAR_EVENT:
return p.scalar()
case ini_COMMENT_EVENT:
return p.comment()
case ini_DOCUMENT_END_EVENT:
// Happens when attempting to decode an empty buffer.
return nil
default:
panic("attempted to parse unknown event: " + p.event.event_type())
}
}
func (p *parser) node(kind int) *node {
return &node{
kind: kind,
line: p.event.start_mark.line,
column: p.event.start_mark.column,
}
}
func (p *parser) clone_node(n *node) *node {
thisNode := p.node(n.kind)
thisNode.tag = n.tag
thisNode.value = n.value
for _, childNode := range n.children {
thisNode.children = append(thisNode.children, p.clone_node(childNode))
}
return thisNode
}
/**
targetNode is: "hello": [1: "world"]
sourceNode is: "hello": [1: [2: "world"]]
overwrite result is:
"hello": [1: [2: "world"]]
no overwrite result is:
"hello": [1: "world"]
Specific scene:
when inherit a section , the expect operation for the same node is "no overwrite"
in the same section, the expect operation for the same node is "overwrite"
*/
func (p *parser) merge_node(targetNode *node, sourceNode *node, overwrite bool) {
if targetNode.kind == sourceNode.kind {
targetNodeCount := len(targetNode.children)
sourceNodeCount := len(sourceNode.children)
swapChildNodes := make([]*node, 0)
for i := 0; i < sourceNodeCount; i += 2 {
nodeExist := false
for j := 0; j < targetNodeCount; j += 2 {
if sourceNode.children[i].kind == scalarNode && targetNode.children[j].kind == scalarNode && sourceNode.children[i].value == targetNode.children[j].value {
nodeExist = true
if sourceNode.children[i+1].kind == targetNode.children[j+1].kind {
if len(sourceNode.children[i+1].children) > 0 && len(targetNode.children[j+1].children) > 0 {
p.merge_node(targetNode.children[j+1], p.clone_node(sourceNode.children[i+1]), overwrite)
}
} else {
if overwrite {
targetNode.children[j + 1] = p.clone_node(sourceNode.children[i + 1])
}
}
break
}
}
if !nodeExist {
swapChildNodes = append(swapChildNodes, sourceNode.children[i], sourceNode.children[i+1])
}
}
swapChildNodeCount := len(swapChildNodes)
for i := 0; i < swapChildNodeCount; i += 2 {
targetNode.children = append(targetNode.children, swapChildNodes[i], swapChildNodes[i+1])
}
}
return
}
func (p *parser) document() *node {
n := p.node(documentNode)
p.doc = n
p.skip()
for p.event.typ != ini_DOCUMENT_END_EVENT {
keyNode := p.parse()
nextNode := p.parse()
if nextNode.kind == inheritNode {
childNode := p.parse()
// inherit
sectionExists := false
for i := 0; i < len(p.doc.children); i += 2 {
if p.doc.children[i].kind == scalarNode && p.doc.children[i].value == nextNode.value {
sectionExists = true
p.merge_node(childNode, p.clone_node(p.doc.children[i+1]), false)
break
}
}
if !sectionExists && nextNode.value != DEFAULT_SECTION {
failf("inherit section '%s' does not exists", nextNode.value)
}
n.children = append(n.children, keyNode, childNode)
} else if nextNode.kind == sectionNode {
n.children = append(n.children, keyNode, nextNode)
}
p.skip()
}
return n
}
func (p *parser) section() *node {
thisNode := p.node(sectionNode)
// until next ini_SECTION_START_EVENT
p.skip()
parentNode := thisNode
for p.event.typ != ini_SECTION_ENTRY_EVENT {
currentNodeKey := p.parse()
if currentNodeKey == nil {
p.fail()
}
if currentNodeKey.kind == scalarNode {
currentNodeValue := p.parse()
swapChildNodes := make([]*node, 0)
for i := 0; i < len(parentNode.children); i += 2 {
if parentNode.children[i].value == currentNodeKey.value {
if parentNode.children[i+1].kind == currentNodeValue.kind {
swapChildNodes = append(swapChildNodes, parentNode.children[i], parentNode.children[i+1])
}
} else {
swapChildNodes = append(swapChildNodes, parentNode.children[i], parentNode.children[i+1])
}
}
parentNode.children = swapChildNodes
nodeExist := false
for i := 0; i < len(parentNode.children); i += 2 {
// condition:
// 1. current node type
// 2. current node value
if currentNodeKey.kind == scalarNode && parentNode.children[i].kind == scalarNode && currentNodeKey.value == parentNode.children[i].value {
nodeExist = true
// if current node value type is different, overwrite it
if parentNode.children[i+1].kind != currentNodeValue.kind {
parentNode.children[i+1] = p.clone_node(currentNodeValue)
} else {
p.merge_node(parentNode.children[i+1], p.clone_node(currentNodeValue), true)
}
break
}
}
if !nodeExist {
parentNode.children = append(parentNode.children, currentNodeKey, currentNodeValue)
}
}
parentNode = thisNode
}
return thisNode
}
func (p *parser) mapping() *node {
thisNode := p.node(mappingNode)
// until next ini_SECTION_START_EVENT
p.skip()
parentNode := thisNode
currentNodeKey := p.parse()
if currentNodeKey == nil {
p.fail()
}
if currentNodeKey.kind == scalarNode {
currentNodeValue := p.parse()
nodeExist := false
i := 0
for ; i < len(parentNode.children); i += 2 {
// condition:
// 1. current node type
// 2. current node value
if currentNodeKey.kind == parentNode.children[i].kind && currentNodeKey.value == parentNode.children[i].value {
nodeExist = true
break
}
}
if nodeExist {
if len(parentNode.children) > 0 {
// if node type is different, overwrite it
if parentNode.children[i+1].kind != currentNodeValue.kind {
parentNode.children[i+1] = p.clone_node(currentNodeValue)
} else {
p.merge_node(parentNode.children[i+1], p.clone_node(currentNodeValue), true)
}
parentNode = parentNode.children[i+1]
}
} else {
parentNode.children = append(parentNode.children, currentNodeKey, currentNodeValue)
}
if currentNodeValue.kind == mappingNode {
parentNode = currentNodeValue
}
}
return thisNode
}
func (p *parser) inherit() *node {
thisNode := p.node(inheritNode)
thisNode.value = string(p.event.value)
p.skip()
return thisNode
}
func (p *parser) comment() *node {
thisNode := p.node(commentNode)
thisNode.value = string(p.event.value)
p.skip()
return thisNode
}
func (p *parser) scalar() *node {
thisNode := p.node(scalarNode)
thisNode.value = string(p.event.value)
thisNode.tag = string(p.event.tag)
p.skip()
return thisNode
}
// ----------------------------------------------------------------------------
// Decoder, unmarshals a node into a provided value.
type decoder struct {
doc *node
mapType reflect.Type
terrors []string
}
var (
mapItemType = reflect.TypeOf(MapItem{})
durationType = reflect.TypeOf(time.Duration(0))
defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
ifaceType = defaultMapType.Elem()
)
func newDecoder() *decoder {
d := &decoder{mapType: defaultMapType}
return d
}
func (d *decoder) terror(n *node, tag string, out reflect.Value) {
if n.tag != "" {
tag = n.tag
}
value := n.value
if len(value) > 10 {
value = " `" + value[:7] + "...`"
} else {
value = " `" + value + "`"
}
d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, tag, value, out.Type()))
}
func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
terrlen := len(d.terrors)
err := u.UnmarshalINI(func(v interface{}) (err error) {
defer handleErr(&err)
d.unmarshal(n, reflect.ValueOf(v))
if len(d.terrors) > terrlen {
issues := d.terrors[terrlen:]
d.terrors = d.terrors[:terrlen]
return &TypeError{issues}
}
return nil
})
if e, ok := err.(*TypeError); ok {
d.terrors = append(d.terrors, e.Errors...)
return false
}
if err != nil {
fail(err)
}
return true
}
// d.prepare initializes and dereferences pointers and calls UnmarshalINI
// if a value is found to implement it.
// It returns the initialized and dereferenced out value, whether
// unmarshalling was already done by UnmarshalINI, and if so whether
// its types unmarshalled appropriately.
//
// If n holds a null value, prepare returns before doing anything.
func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
if n.tag == ini_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "") {
return out, false, false
}
again := true
for again {
again = false
if out.Kind() == reflect.Ptr {
if out.IsNil() {
out.Set(reflect.New(out.Type().Elem()))
}
out = out.Elem()
again = true
}
if out.CanAddr() {
if u, ok := out.Addr().Interface().(Unmarshaler); ok {
good = d.callUnmarshaler(n, u)
return out, true, good
}
}
}
return out, false, false
}
func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
out, unmarshaled, good := d.prepare(n, out)
if unmarshaled {
return good
}
switch n.kind {
case documentNode:
good = d.document(n, out)
case sectionNode:
good = d.mapping(n, out)
case mappingNode:
good = d.mapping(n, out)
case scalarNode:
good = d.scalar(n, out)
default:
panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
}
return good
}
func (d *decoder) document(n *node, out reflect.Value) (good bool) {
if len(n.children) > 0 {
d.doc = n
switch out.Kind() {
case reflect.Struct:
sinfo, err := getStructInfo(out.Type())
if err != nil {
panic(err)
}
k := settableValueOf("")
l := len(n.children)
for i := 0; i < l; i += 2 {
if !d.unmarshal(n.children[i], k) {
continue
}
if n.children[i].value == DEFAULT_SECTION {
ll := len(n.children[i+1].children)
for j := 0; j < ll; j += 2 {
if !d.unmarshal(n.children[i+1].children[j], k) {
continue
}
if info, ok := sinfo.FieldsMap[k.String()]; ok {
var field reflect.Value
if info.Inline == nil {
field = out.Field(info.Num)
} else {
field = out.FieldByIndex(info.Inline)
}
d.unmarshal(n.children[i+1].children[j+1], field)
}
}
} else {
if info, ok := sinfo.FieldsMap[k.String()]; ok {
var field reflect.Value
if info.Inline == nil {
field = out.Field(info.Num)
} else {
field = out.FieldByIndex(info.Inline)
}
d.unmarshal(n.children[i+1], field)
}
}
}
return true
case reflect.Slice:
outt := out.Type()
if outt.Elem() != mapItemType {
d.terror(n, ini_MAP_TAG, out)
return false
}
mapType := d.mapType
d.mapType = outt
var slice []MapItem
var l = len(n.children)
for i := 0; i < l; i += 2 {
item := MapItem{}
k := reflect.ValueOf(&item.Key).Elem()
if !d.unmarshal(n.children[i], k) {
continue
}
if n.children[i].value == DEFAULT_SECTION {
ll := len(n.children[i+1].children)
for j := 0; j < ll; j += 2 {
item := MapItem{}
k := reflect.ValueOf(&item.Key).Elem()
if !d.unmarshal(n.children[j], k) {
continue
}
v := reflect.ValueOf(&item.Value).Elem()
if d.unmarshal(n.children[i+1], v) {
slice = append(slice, item)
}
}
} else {
v := reflect.ValueOf(&item.Value).Elem()
if d.unmarshal(n.children[i+1], v) {
slice = append(slice, item)
}
}
}
out.Set(reflect.ValueOf(slice))
d.mapType = mapType
return true
case reflect.Map:
//okay
case reflect.Interface:
if d.mapType.Kind() == reflect.Map {
iface := out
out = reflect.MakeMap(d.mapType)
iface.Set(out)
} else {
slicev := reflect.New(d.mapType).Elem()
if !d.mappingSlice(n, slicev) {
return false
}
out.Set(slicev)
return true
}
default:
d.terror(n, ini_MAP_TAG, out)
return false
}
outt := out.Type()
kt := outt.Key()
et := outt.Elem()
mapType := d.mapType
if outt.Key() == ifaceType && outt.Elem() == ifaceType {
d.mapType = outt
}
if out.IsNil() {
out.Set(reflect.MakeMap(outt))
}
l := len(n.children)
for i := 0; i < l; i += 2 {
k := reflect.New(kt).Elem()
if !d.unmarshal(n.children[i], k) {
continue
}
kkind := k.Kind()
if kkind == reflect.Interface {
kkind = k.Elem().Kind()
}
if kkind == reflect.Map || kkind == reflect.Slice {
failf("invalid map key: %#v", k.Interface())
}
e := reflect.New(et).Elem()
if n.children[i].value == DEFAULT_SECTION {
d.unmarshal(n.children[i+1], out)
} else {
if d.unmarshal(n.children[i+1], e) {
out.SetMapIndex(k, e)
}
}
}
d.mapType = mapType
return true
}
return false
}
var zeroValue reflect.Value
func resetMap(out reflect.Value) {
for _, k := range out.MapKeys() {
out.SetMapIndex(k, zeroValue)
}
}
func settableValueOf(i interface{}) reflect.Value {
v := reflect.ValueOf(i)
sv := reflect.New(v.Type()).Elem()
sv.Set(v)
return sv
}
func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
switch out.Kind() {
case reflect.Struct:
return d.mappingStruct(n, out)
case reflect.Slice:
return d.mappingSlice(n, out)
case reflect.Map:
// okay
case reflect.Interface:
if d.mapType.Kind() == reflect.Map {
iface := out
out = reflect.MakeMap(d.mapType)
iface.Set(out)
} else {
slicev := reflect.New(d.mapType).Elem()
if !d.mappingSlice(n, slicev) {
return false
}
out.Set(slicev)
return true
}
default:
d.terror(n, ini_MAP_TAG, out)
return false
}
outt := out.Type()
kt := outt.Key()
et := outt.Elem()
mapType := d.mapType
if outt.Key() == ifaceType && outt.Elem() == ifaceType {
d.mapType = outt
}
if out.IsNil() {
out.Set(reflect.MakeMap(outt))
}
l := len(n.children)
for i := 0; i < l; i += 2 {
k := reflect.New(kt).Elem()
if d.unmarshal(n.children[i], k) {
kkind := k.Kind()
if kkind == reflect.Interface {
kkind = k.Elem().Kind()
}
if kkind == reflect.Map || kkind == reflect.Slice {
failf("invalid map key: %#v", k.Interface())
}
e := reflect.New(et).Elem()
if d.unmarshal(n.children[i+1], e) {
out.SetMapIndex(k, e)
}
}
}
d.mapType = mapType
return true
}
func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
outt := out.Type()
if outt.Elem() != mapItemType {
d.terror(n, ini_MAP_TAG, out)
return false
}
mapType := d.mapType
d.mapType = outt
var slice []MapItem
var l = len(n.children)
for i := 0; i < l; i += 2 {
item := MapItem{}
k := reflect.ValueOf(&item.Key).Elem()
if d.unmarshal(n.children[i], k) {
v := reflect.ValueOf(&item.Value).Elem()
if d.unmarshal(n.children[i+1], v) {
slice = append(slice, item)
}
}
}
out.Set(reflect.ValueOf(slice))
d.mapType = mapType
return true
}
func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
sinfo, err := getStructInfo(out.Type())
if err != nil {
panic(err)
}
name := settableValueOf("")
l := len(n.children)
for i := 0; i < l; i += 2 {
if !d.unmarshal(n.children[i], name) {
continue
}
if info, ok := sinfo.FieldsMap[name.String()]; ok {
var field reflect.Value
if info.Inline == nil {
field = out.Field(info.Num)
} else {
field = out.FieldByIndex(info.Inline)
}
d.unmarshal(n.children[i+1], field)
}
}
return true
}
func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
var tag string
var resolved interface{}
tag, resolved = resolve(n.tag, n.value)
if tag == ini_BINARY_TAG {
data, err := base64.StdEncoding.DecodeString(resolved.(string))
if err != nil {
failf("!!binary value contains invalid base64 data")
}
resolved = string(data)
}
if resolved == nil {
if out.Kind() == reflect.Map && !out.CanAddr() {
resetMap(out)
} else {
out.Set(reflect.Zero(out.Type()))
}
return true
}
if s, ok := resolved.(string); ok && out.CanAddr() {
if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
err := u.UnmarshalText([]byte(s))
if err != nil {
fail(err)
}
return true
}
}
switch out.Kind() {
case reflect.String:
if tag == ini_BINARY_TAG {
out.SetString(resolved.(string))
good = true
} else if resolved != nil {
out.SetString(n.value)
good = true
}
case reflect.Interface:
if resolved == nil {
out.Set(reflect.Zero(out.Type()))
} else {
// TODO Not sure if we should resolve interface type of scalar
switch resolved.(type) {
//case bool:
// var resolvedString string = strconv.FormatBool(resolved.(bool))
// out.Set(reflect.ValueOf(resolvedString))
//case string:
// var resolvedString string = resolved.(string)
// out.Set(reflect.ValueOf(resolvedString))
//case int:
// var resolvedString string = strconv.FormatInt(int64(resolved.(int)), 10)
// out.Set(reflect.ValueOf(resolvedString))
//case int64:
// var resolvedString string = strconv.FormatInt(resolved.(int64), 10)
// out.Set(reflect.ValueOf(resolvedString))
//case uint64:
// var resolvedString string = strconv.FormatUint(resolved.(uint64), 10)
// out.Set(reflect.ValueOf(resolvedString))
default:
out.Set(reflect.ValueOf(resolved))
}
}
good = true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch resolved := resolved.(type) {
case int:
if !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
}
case int64:
if !out.OverflowInt(resolved) {
out.SetInt(resolved)
good = true
}
case uint64:
if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
}
case float64:
if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
}
case string:
if out.Type() == durationType {
d, err := time.ParseDuration(resolved)
if err == nil {
out.SetInt(int64(d))
good = true
}
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
switch resolved := resolved.(type) {
case int:
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
}
case int64:
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
}
case uint64:
if !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
}
case float64:
if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
}
}
case reflect.Bool:
switch resolved := resolved.(type) {
case bool:
out.SetBool(resolved)
good = true
}
case reflect.Float32, reflect.Float64:
switch resolved := resolved.(type) {
case int:
out.SetFloat(float64(resolved))
good = true
case int64:
out.SetFloat(float64(resolved))
good = true
case uint64:
out.SetFloat(float64(resolved))
good = true
case float64:
out.SetFloat(resolved)
good = true
}
case reflect.Ptr:
if out.Type().Elem() == reflect.TypeOf(resolved) {
// TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
elem := reflect.New(out.Type().Elem())
elem.Elem().Set(reflect.ValueOf(resolved))
out.Set(elem)
good = true
}
}
if !good {
d.terror(n, tag, out)
}
return good
}