-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaders.go
793 lines (734 loc) · 22.1 KB
/
readers.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
/*
* Copyright 2020 Stephen Guo ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rtl
import (
"encoding/gob"
"fmt"
"log"
"math/big"
"reflect"
)
type Lenner interface {
Len() int
}
type typeReaderFunc func(length int, vr ValueReader, value reflect.Value, nesting int) error
func unsupported(_ int, _ ValueReader, _ reflect.Value, _ int) error {
return ErrUnsupported
}
func unsupportFunc(typ reflect.Type, th TypeHeader) typeReaderFunc {
return func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return fmt.Errorf("rtl: unsupported headerType:%s decode to (type:%s kind:%s)", th, typ, typ.Kind())
}
}
func getFunc(valueType reflect.Type, funcMap map[TypeHeader]typeReaderFunc, th TypeHeader) typeReaderFunc {
f, _ := funcMap[th]
if f == nil {
f = unsupportFunc(valueType, th)
}
return f
}
// toSmallBigInt decode single byte header value to big.Int
func toSmallBigInt(length int, vr ValueReader, isNegative bool, value reflect.Value) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
i := getOrNewBigInt(value)
if isNegative {
i.SetInt64(Numeric.BytesToInt64(buf, true))
} else {
i.SetUint64(Numeric.BytesToUint64(buf))
}
return nil
}
// toBigBigInt decode multi bytes header value to big.Int
func toBigBigInt(length int, vr ValueReader, value reflect.Value, _ int) error {
buf, err := vr.ReadMultiLengthBytes(length, nil)
if err != nil {
return err
}
i := getOrNewBigInt(value)
i.SetBytes(buf)
return nil
}
func toBigNegBigInt(length int, vr ValueReader, value reflect.Value, _ int) error {
buf, err := vr.ReadMultiLengthBytes(length, nil)
if err != nil {
return err
}
i := getOrNewBigInt(value)
i.SetBytes(buf)
i.Neg(i)
return nil
}
func toSmallGob(length int, vr ValueReader, value reflect.Value, getGob func(reflect.Value) gob.GobDecoder) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
g := getGob(value)
return g.GobDecode(buf)
}
func toBigGob(length int, vr ValueReader, value reflect.Value, getGob func(reflect.Value) gob.GobDecoder) error {
buf, err := vr.ReadMultiLengthBytes(length, nil)
if err != nil {
return err
}
g := getGob(value)
return g.GobDecode(buf)
}
// toInt decode single byte header bytes to int value
func toInt(length int, vr ValueReader, isNegative bool, value reflect.Value) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
i := Numeric.BytesToInt64(buf, isNegative)
value.SetInt(i)
return nil
}
// toUint decode single byte header bytes to uint value
func toUint(length int, vr ValueReader, value reflect.Value, _ int) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
i := Numeric.BytesToUint64(buf)
value.SetUint(i)
return nil
}
// toFloat decode single byte header bytes to float value
func toFloat(length int, vr ValueReader, isNegative bool, value reflect.Value) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
var f float64
if length == 4 {
f = float64(Numeric.BytesToFloat32(buf, isNegative))
} else {
f = Numeric.BytesToFloat64(buf, isNegative)
}
value.SetFloat(f)
return nil
}
func getOrNewBigInt(v reflect.Value) *big.Int {
i := v.Interface().(*big.Int)
if i == nil {
i = new(big.Int)
v.Set(reflect.ValueOf(i))
}
return i
}
func getOrNewBigRat(v reflect.Value) gob.GobDecoder {
g := v.Interface().(*big.Rat)
if g == nil {
g = new(big.Rat)
v.Set(reflect.ValueOf(g))
}
return g
}
func getOrNewBigFloat(v reflect.Value) gob.GobDecoder {
g := v.Interface().(*big.Float)
if g == nil {
g = new(big.Float)
v.Set(reflect.ValueOf(g))
}
return g
}
// stringToArray decode string(byte slice) to array of type which support single byte value
func stringToArray(buf []byte, vr ValueReader, value reflect.Value, nesting int) error {
vl := value.Len()
l := len(buf)
i := 0
etyp := value.Type().Elem()
if etyp == typeOfByte {
i = reflect.Copy(value, reflect.ValueOf(buf))
} else {
for ; i < vl && i < l; i++ {
evalue := value.Index(i)
err := valueReader1(THSingleByte, int(buf[i]), vr, evalue, nesting+1)
if err != nil {
return err
}
}
}
if i != vl || i != l {
log.Printf("rtl: string to array/slice length not match, "+
"len(string)=%d, len(array)=%d, %d elements writed", l, vl, i)
}
return nil
}
func singleByteToArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
vl := value.Len()
if vl >= 1 {
evalue := value.Index(0)
return valueReader1(THSingleByte, length, vr, evalue, nesting+1)
}
log.Printf("rtl: restore nothing for an 0 length array/slice with %s(byte:%x)", THSingleByte, length)
return nil
}
// element could be any type which could expressed by a byte
func stringSingleToArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
return stringToArray(buf, vr, value, nesting)
}
func stringMultiToArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
buf, err := vr.ReadMultiLengthBytes(length, nil)
if err != nil {
return err
}
return stringToArray(buf, vr, value, nesting)
}
func arraySingleToArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toArray0(length, vr, value, nesting)
}
func arrayMultiToArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
return toArray0(int(l), vr, value, nesting)
}
func toArray0(length int, vr ValueReader, value reflect.Value, nesting int) error {
vl := value.Len()
i := 0
nesting++
for ; i < length && i < vl; i++ {
evalue := value.Index(i)
if err := valueReader0(vr, evalue, nesting); err != nil {
return err
}
}
if i != vl || i != length {
log.Printf("rtl: string to array/slice length not match, "+
"len(string)=%d, len(array)=%d, %d elements writed", length, vl, i)
}
return nil
}
func singleByteToSlice0(length int, vr ValueReader, value reflect.Value, nesting int) error {
checkSlice0(1, value)
return singleByteToArray0(length, vr, value, nesting)
}
func stringSingleToSlice0(length int, vr ValueReader, value reflect.Value, nesting int) error {
checkSlice0(length, value)
return stringSingleToArray0(length, vr, value, nesting)
}
func stringMultiToSlice0(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
checkSlice0(int(l), value)
return stringSingleToArray0(int(l), vr, value, nesting)
}
func arraySingleToSlice0(length int, vr ValueReader, value reflect.Value, nesting int) error {
checkSlice0(length, value)
return arraySingleToArray0(length, vr, value, nesting)
}
func arrayMultiToSlice0(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
checkSlice0(int(l), value)
return arraySingleToArray0(int(l), vr, value, nesting)
}
func checkSlice0(length int, value reflect.Value) {
if length > value.Cap() {
newv := reflect.MakeSlice(value.Type(), length, length)
value.Set(newv)
}
if length != value.Len() {
value.SetLen(length)
}
}
func arraySingleToMap0(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toMap0(length, vr, value, nesting)
}
func arrayMultiToMap0(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
return toMap0(int(l), vr, value, nesting)
}
func toMap0(length int, vr ValueReader, value reflect.Value, nesting int) error {
if length%2 != 0 {
return fmt.Errorf("rtl: length of the array must be even when decode to a map, but length=%d", length)
}
typ := value.Type()
if value.IsNil() {
value.Set(reflect.MakeMapWithSize(typ, length/2))
}
ktyp := typ.Key()
vtyp := typ.Elem()
nesting++
for i := 0; i < length; i += 2 {
kvalue := reflect.New(ktyp).Elem()
vvalue := reflect.New(vtyp).Elem()
if err := valueReader0(vr, kvalue, nesting); err != nil {
return err
}
if err := valueReader0(vr, vvalue, nesting); err != nil {
return err
}
value.SetMapIndex(kvalue, vvalue)
}
return nil
}
func arraySingleToStruct0(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toStruct0(length, vr, value, nesting)
}
func arrayMultiToStruct0(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
return toStruct0(int(l), vr, value, nesting)
}
func toStruct0(length int, vr ValueReader, value reflect.Value, nesting int) error {
typ := value.Type()
_, fnames := structFields(typ)
lth := len(fnames)
// if lth > length {
// lth = length
// }
nesting++
nextOrder := 0 // 下一个field对应的order
nextIndex := 0 // 下一个filed的对应下标
if nextIndex < lth {
nextOrder = fnames[nextIndex].order
for i := 0; i < length; i++ {
if i == nextOrder {
fvalue := value.Field(fnames[nextIndex].index)
if err := valueReader0(vr, fvalue, nesting); err != nil {
return err
}
nextIndex++
if nextIndex >= lth {
break
}
nextOrder = fnames[nextIndex].order
} else if i < nextOrder {
if _, err := vr.Skip(); err != nil {
return err
}
} else {
return fmt.Errorf("illegal status found: dataIndex:%d nextIndex:%d %s",
i, nextIndex, fnames[nextIndex])
}
}
}
// 将后面未包含在对象中的字段置空
for ; nextIndex < lth; nextIndex++ {
fvalue := value.Field(fnames[nextIndex].index)
if err := valueReader1(THZeroValue, 0, vr, fvalue, nesting); err != nil {
return err
}
}
return nil
}
var (
// fill in value, which must be a *big.Int
bigIntReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
i := getOrNewBigInt(value)
i.SetInt64(int64(length))
return nil
},
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.Set(reflect.Zero(value.Type()))
return nil
},
THPosNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toSmallBigInt(length, vr, false, value)
},
THNegNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toSmallBigInt(length, vr, true, value)
},
THPosBigInt: toBigBigInt,
THNegBigInt: toBigNegBigInt,
}
bigRatReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: unsupported,
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.Set(reflect.Zero(value.Type()))
return nil
},
THPosNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toSmallGob(length, vr, value, getOrNewBigRat)
},
THNegNumSingle: unsupported,
THPosBigInt: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toBigGob(length, vr, value, getOrNewBigRat)
},
THNegBigInt: unsupported,
}
bigFloatReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: unsupported,
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.Set(reflect.Zero(value.Type()))
return nil
},
THPosNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toSmallGob(length, vr, value, getOrNewBigFloat)
},
THNegNumSingle: unsupported,
THPosBigInt: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toBigGob(length, vr, value, getOrNewBigFloat)
},
THNegBigInt: unsupported,
}
// value SHOULD NOT be a pointer
intReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetInt(int64(length))
return nil
},
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetInt(int64(0))
return nil
},
THPosNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toInt(length, vr, false, value)
},
THNegNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toInt(length, vr, true, value)
},
}
// value SHOULD NOT be a pointer
uintReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetUint(uint64(length))
return nil
},
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetUint(uint64(0))
return nil
},
THPosNumSingle: toUint,
}
// value SHOULD NOT be a pointer
floatReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetFloat(Numeric.ByteToFloat64(byte(length), false))
return nil
},
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetFloat(0)
return nil
},
THPosNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toFloat(length, vr, false, value)
},
THNegNumSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
return toFloat(length, vr, true, value)
},
}
// value SHOULD NOT be a pointer
boolReaders = map[TypeHeader]typeReaderFunc{
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetBool(false)
return nil
},
THTrue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetBool(true)
return nil
},
}
// value SHOULD NOT be a pointer
stringReaders = map[TypeHeader]typeReaderFunc{
THSingleByte: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.SetString(string([]byte{byte(length)}))
return nil
},
THZeroValue: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
value.Set(reflect.Zero(value.Type()))
return nil
},
THStringSingle: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
buf, err := vr.ReadBytes(length, nil)
if err != nil {
return err
}
value.SetString(string(buf))
return nil
},
THStringMulti: func(length int, vr ValueReader, value reflect.Value, nesting int) error {
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
buf, err := vr.ReadBytes(int(l), nil)
if err != nil {
return err
}
value.SetString(string(buf))
return nil
},
}
)
func valueReader(r ValueReader, value reflect.Value) error {
return valueReader0(r, value, 0)
}
func valueReader0(vr ValueReader, value reflect.Value, nesting int) error {
// decode itself if the value implements encoding.Decoder interface
isDecoder, err := checkTypeOfDecoder(vr, value)
if isDecoder || err != nil {
return err
}
// if not an encoding.Decoder implementation, use default decoder
th, length, err := vr.ReadHeader()
if err != nil {
// if EndOfFile(err) {
// return nil
// }
return err
}
return valueReader1(th, length, vr, value, nesting)
}
func valueReader1(th TypeHeader, length int, vr ValueReader, value reflect.Value, nesting int) error {
if nesting > MaxNested {
return ErrNestingOverflow
}
typ := value.Type()
if matched, err := checkPriorStructsReader(th, length, vr, value, nesting); err != nil {
return err
} else if matched {
return err
}
kind := value.Kind()
switch kind {
case reflect.Array:
switch th {
case THSingleByte:
return singleByteToArray0(length, vr, value, nesting)
case THZeroValue:
value.Set(reflect.Zero(typ))
return nil
case THStringSingle:
return stringSingleToArray0(length, vr, value, nesting)
case THStringMulti:
return stringMultiToArray0(length, vr, value, nesting)
case THArraySingle:
return arraySingleToArray0(length, vr, value, nesting)
case THArrayMulti:
return arrayMultiToArray0(length, vr, value, nesting)
}
case reflect.Slice:
switch th {
case THSingleByte:
return singleByteToSlice0(length, vr, value, nesting)
case THZeroValue:
value.Set(reflect.Zero(typ))
return nil
case THEmpty:
if !value.CanSet() {
return fmt.Errorf("rtl: slice can not set to empty")
}
nslice := reflect.MakeSlice(typ, 0, 0)
value.Set(nslice)
return nil
case THStringSingle:
return stringSingleToSlice0(length, vr, value, nesting)
case THStringMulti:
return stringMultiToSlice0(length, vr, value, nesting)
case THArraySingle:
return arraySingleToSlice0(length, vr, value, nesting)
case THArrayMulti:
return arrayMultiToSlice0(length, vr, value, nesting)
}
case reflect.Map:
switch th {
case THZeroValue:
value.Set(reflect.Zero(typ))
return nil
case THEmpty:
if !value.CanSet() {
return fmt.Errorf("rtl: map can not set")
}
nmap := reflect.MakeMapWithSize(typ, 0)
value.Set(nmap)
return nil
case THArraySingle:
return arraySingleToMap0(length, vr, value, nesting)
case THArrayMulti:
return arrayMultiToMap0(length, vr, value, nesting)
}
case reflect.Struct:
return toStructs(typ, kind, th, length, vr, value, nesting)
case reflect.Ptr:
// pointer
return toPointers(typ, kind, th, length, vr, value, nesting)
case reflect.Interface:
// as interface{} type
return toInterfaces(typ, kind, th, length, vr, value, nesting)
default:
funcMap, ok := primKindTypeHeaderMap[kind]
if ok {
return typedReader0(th, length, vr, value, nesting, funcMap)
} else {
return fmt.Errorf("rtl: unsupported type1 %v (kind: %s, headerType: %s) for decoding", typ, kind, th)
}
}
return fmt.Errorf("rtl: unsupported type2 %v (kind: %s, headerType: %s) for decoding", typ, kind, th)
}
func toStructs(typ reflect.Type, kind reflect.Kind, th TypeHeader, length int, vr ValueReader,
value reflect.Value, nesting int) error {
switch th {
case THZeroValue:
value.Set(reflect.Zero(typ))
return nil
case THArraySingle:
return arraySingleToStruct0(length, vr, value, nesting)
case THArrayMulti:
return arrayMultiToStruct0(length, vr, value, nesting)
}
return fmt.Errorf("rtl: unsupported type3 %v (kind: %s, headerType: %s) for decoding", typ, kind, th)
}
func toPointers(typ reflect.Type, _ reflect.Kind, th TypeHeader, length int, vr ValueReader,
value reflect.Value, nesting int) error {
etyp := typ.Elem()
if th == THZeroValue {
// nil pointer
if !value.IsNil() {
value.Set(reflect.Zero(typ))
}
return nil
} else {
// pointer to something
// check if the type supported at first
ekind := etyp.Kind()
if !canBeDecodeTo(ekind) {
return fmt.Errorf("rtl: unsupported type4 pointer to %v (kind: %s) "+
"for decoding", etyp, ekind.String())
}
// create if nil
evalue := value
if value.IsNil() {
if !value.CanSet() {
return fmt.Errorf("rtl: cannot create new value %s", etyp.Name())
}
evalue = reflect.New(etyp)
}
err := valueReader1(th, length, vr, evalue.Elem(), nesting)
if err == nil && value.IsNil() {
value.Set(evalue)
}
return err
}
}
func toInterfaces(typ reflect.Type, kind reflect.Kind, th TypeHeader, length int, vr ValueReader,
value reflect.Value, nesting int) error {
if value.Type().NumMethod() != 0 {
return fmt.Errorf("rtl: unsupported type5 %v (kind: %s, headerType: %s) for decoding", typ, kind, th)
}
switch th {
case THSingleByte:
nv := reflect.New(typeOfUint64).Elem()
nv.SetUint(uint64(length))
value.Set(nv)
return nil
case THZeroValue:
value.Set(reflect.Zero(typeOfInterface))
return nil
case THEmpty:
value.Set(reflect.MakeSlice(typeOfInterfaceSlice, 0, 0))
return nil
case THPosNumSingle:
b, err := vr.ReadBytes(length, nil)
if err != nil {
return nil
}
nv := reflect.New(typeOfUint64).Elem()
nv.SetUint(Numeric.BytesToUint64(b))
value.Set(nv)
return nil
case THNegNumSingle:
b, err := vr.ReadBytes(length, nil)
if err != nil {
return nil
}
nv := reflect.New(typeOfInt64).Elem()
nv.SetInt(Numeric.BytesToInt64(b, true))
value.Set(nv)
return nil
case THStringSingle:
b, err := vr.ReadBytes(length, nil)
if err != nil {
return nil
}
nv := reflect.New(typeOfString).Elem()
nv.SetString(string(b))
value.Set(nv)
return nil
case THStringMulti:
b, err := vr.ReadMultiLengthBytes(length, nil)
if err != nil {
return nil
}
nv := reflect.New(typeOfString).Elem()
nv.SetString(string(b))
value.Set(nv)
return nil
case THArraySingle:
slice := reflect.MakeSlice(typeOfInterfaceSlice, length, length)
value.Set(slice)
return arraySingleToArray0(length, vr, slice, nesting)
case THArrayMulti:
l, err := vr.ReadMultiLength(length)
if err != nil {
return err
}
slice := reflect.MakeSlice(typeOfInterfaceSlice, int(l), int(l))
value.Set(slice)
return arrayMultiToArray0(int(l), vr, slice, nesting)
}
return fmt.Errorf("rtl: unsupported type6 %v (kind: %s, headerType: %s) for decoding", typ, kind, th)
}
func typedReader0(th TypeHeader, length int, vr ValueReader, value reflect.Value, nesting int,
funcMap map[TypeHeader]typeReaderFunc) error {
if funcMap == nil {
return fmt.Errorf("rtl: type mismatch error: expect %s but %s found", value.Type().Name(), th.Name())
}
f, ok := funcMap[th]
if !ok {
return fmt.Errorf("rtl: type mismatch error: expect %s but %s found", value.Type().Name(), th.Name())
}
return f(length, vr, value, nesting)
}
func canBeDecodeTo(kind reflect.Kind) bool {
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Float32:
case reflect.Float64:
case reflect.Bool:
case reflect.String:
case reflect.Array:
case reflect.Slice:
case reflect.Map:
case reflect.Struct:
case reflect.Ptr:
default:
return false
}
return true
}