-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtable_unmarshal.go
2251 lines (2125 loc) · 54.1 KB
/
table_unmarshal.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2016 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto
import (
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"unicode/utf8"
)
// Unmarshal is the entry point from the generated .pb.go files.
// This function is not intended to be used by non-generated code.
// This function is not subject to any compatibility guarantee.
// msg contains a pointer to a protocol buffer struct.
// b is the data to be unmarshaled into the protocol buffer.
// a is a pointer to a place to store cached unmarshal information.
func (a *InternalMessageInfo) Unmarshal(msg Message, b []byte) error {
// Load the unmarshal information for this message type.
// The atomic load ensures memory consistency.
u := atomicLoadUnmarshalInfo(&a.unmarshal)
if u == nil {
// Slow path: find unmarshal info for msg, update a with it.
u = getUnmarshalInfo(reflect.TypeOf(msg).Elem())
atomicStoreUnmarshalInfo(&a.unmarshal, u)
}
// Then do the unmarshaling.
err := u.unmarshal(toPointer(&msg), b)
return err
}
type unmarshalInfo struct {
typ reflect.Type // type of the protobuf struct
// 0 = only typ field is initialized
// 1 = completely initialized
initialized int32
lock sync.Mutex // prevents double initialization
dense []unmarshalFieldInfo // fields indexed by tag #
sparse map[uint64]unmarshalFieldInfo // fields indexed by tag #
reqFields []string // names of required fields
reqMask uint64 // 1<<len(reqFields)-1
unrecognized field // offset of []byte to put unrecognized data (or invalidField if we should throw it away)
extensions field // offset of extensions field (of type proto.XXX_InternalExtensions), or invalidField if it does not exist
oldExtensions field // offset of old-form extensions field (of type map[int]Extension)
extensionRanges []ExtensionRange // if non-nil, implies extensions field is valid
isMessageSet bool // if true, implies extensions field is valid
bytesExtensions field // offset of XXX_extensions with type []byte
}
// An unmarshaler takes a stream of bytes and a pointer to a field of a message.
// It decodes the field, stores it at f, and returns the unused bytes.
// w is the wire encoding.
// b is the data after the tag and wire encoding have been read.
type unmarshaler func(b []byte, f pointer, w int) ([]byte, error)
type unmarshalFieldInfo struct {
// location of the field in the proto message structure.
field field
// function to unmarshal the data for the field.
unmarshal unmarshaler
// if a required field, contains a single set bit at this field's index in the required field list.
reqMask uint64
name string // name of the field, for error reporting
}
var (
unmarshalInfoMap = map[reflect.Type]*unmarshalInfo{}
unmarshalInfoLock sync.Mutex
)
// getUnmarshalInfo returns the data structure which can be
// subsequently used to unmarshal a message of the given type.
// t is the type of the message (note: not pointer to message).
func getUnmarshalInfo(t reflect.Type) *unmarshalInfo {
// It would be correct to return a new unmarshalInfo
// unconditionally. We would end up allocating one
// per occurrence of that type as a message or submessage.
// We use a cache here just to reduce memory usage.
unmarshalInfoLock.Lock()
defer unmarshalInfoLock.Unlock()
u := unmarshalInfoMap[t]
if u == nil {
u = &unmarshalInfo{typ: t}
// Note: we just set the type here. The rest of the fields
// will be initialized on first use.
unmarshalInfoMap[t] = u
}
return u
}
// unmarshal does the main work of unmarshaling a message.
// u provides type information used to unmarshal the message.
// m is a pointer to a protocol buffer message.
// b is a byte stream to unmarshal into m.
// This is top routine used when recursively unmarshaling submessages.
func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
if atomic.LoadInt32(&u.initialized) == 0 {
u.computeUnmarshalInfo()
}
if u.isMessageSet {
return unmarshalMessageSet(b, m.offset(u.extensions).toExtensions())
}
var reqMask uint64 // bitmask of required fields we've seen.
var errLater error
for len(b) > 0 {
// Read tag and wire type.
// Special case 1 and 2 byte varints.
var x uint64
if b[0] < 128 {
x = uint64(b[0])
b = b[1:]
} else if len(b) >= 2 && b[1] < 128 {
x = uint64(b[0]&0x7f) + uint64(b[1])<<7
b = b[2:]
} else {
var n int
x, n = decodeVarint(b)
if n == 0 {
return io.ErrUnexpectedEOF
}
b = b[n:]
}
tag := x >> 3
wire := int(x) & 7
// Dispatch on the tag to one of the unmarshal* functions below.
var f unmarshalFieldInfo
if tag < uint64(len(u.dense)) {
f = u.dense[tag]
} else {
f = u.sparse[tag]
}
if fn := f.unmarshal; fn != nil {
var err error
b, err = fn(b, m.offset(f.field), wire)
if err == nil {
reqMask |= f.reqMask
continue
}
if r, ok := err.(*RequiredNotSetError); ok {
// Remember this error, but keep parsing. We need to produce
// a full parse even if a required field is missing.
if errLater == nil {
errLater = r
}
reqMask |= f.reqMask
continue
}
if err != errInternalBadWireType {
if err == errInvalidUTF8 {
if errLater == nil {
fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
errLater = &invalidUTF8Error{fullName}
}
continue
}
return err
}
// Fragments with bad wire type are treated as unknown fields.
}
// Unknown tag.
if !u.unrecognized.IsValid() {
// Don't keep unrecognized data; just skip it.
var err error
b, err = skipField(b, wire)
if err != nil {
return err
}
continue
}
// Keep unrecognized data around.
// maybe in extensions, maybe in the unrecognized field.
z := m.offset(u.unrecognized).toBytes()
var emap map[int32]Extension
var e Extension
for _, r := range u.extensionRanges {
if uint64(r.Start) <= tag && tag <= uint64(r.End) {
if u.extensions.IsValid() {
mp := m.offset(u.extensions).toExtensions()
emap = mp.extensionsWrite()
e = emap[int32(tag)]
z = &e.enc
break
}
if u.oldExtensions.IsValid() {
p := m.offset(u.oldExtensions).toOldExtensions()
emap = *p
if emap == nil {
emap = map[int32]Extension{}
*p = emap
}
e = emap[int32(tag)]
z = &e.enc
break
}
if u.bytesExtensions.IsValid() {
z = m.offset(u.bytesExtensions).toBytes()
break
}
panic("no extensions field available")
}
}
// Use wire type to skip data.
var err error
b0 := b
b, err = skipField(b, wire)
if err != nil {
return err
}
*z = encodeVarint(*z, tag<<3|uint64(wire))
*z = append(*z, b0[:len(b0)-len(b)]...)
if emap != nil {
emap[int32(tag)] = e
}
}
if reqMask != u.reqMask && errLater == nil {
// A required field of this message is missing.
for _, n := range u.reqFields {
if reqMask&1 == 0 {
errLater = &RequiredNotSetError{n}
}
reqMask >>= 1
}
}
return errLater
}
// computeUnmarshalInfo fills in u with information for use
// in unmarshaling protocol buffers of type u.typ.
func (u *unmarshalInfo) computeUnmarshalInfo() {
u.lock.Lock()
defer u.lock.Unlock()
if u.initialized != 0 {
return
}
t := u.typ
n := t.NumField()
// Set up the "not found" value for the unrecognized byte buffer.
// This is the default for proto3.
u.unrecognized = invalidField
u.extensions = invalidField
u.oldExtensions = invalidField
u.bytesExtensions = invalidField
// List of the generated type and offset for each oneof field.
type oneofField struct {
ityp reflect.Type // interface type of oneof field
field field // offset in containing message
}
var oneofFields []oneofField
for i := 0; i < n; i++ {
f := t.Field(i)
if f.Name == "XXX_unrecognized" {
// The byte slice used to hold unrecognized input is special.
if f.Type != reflect.TypeOf(([]byte)(nil)) {
panic("bad type for XXX_unrecognized field: " + f.Type.Name())
}
u.unrecognized = toField(&f)
continue
}
if f.Name == "XXX_InternalExtensions" {
// Ditto here.
if f.Type != reflect.TypeOf(XXX_InternalExtensions{}) {
panic("bad type for XXX_InternalExtensions field: " + f.Type.Name())
}
u.extensions = toField(&f)
if f.Tag.Get("protobuf_messageset") == "1" {
u.isMessageSet = true
}
continue
}
if f.Name == "XXX_extensions" {
// An older form of the extensions field.
if f.Type == reflect.TypeOf((map[int32]Extension)(nil)) {
u.oldExtensions = toField(&f)
continue
} else if f.Type == reflect.TypeOf(([]byte)(nil)) {
u.bytesExtensions = toField(&f)
continue
}
panic("bad type for XXX_extensions field: " + f.Type.Name())
}
if f.Name == "XXX_NoUnkeyedLiteral" || f.Name == "XXX_sizecache" {
continue
}
oneof := f.Tag.Get("protobuf_oneof")
if oneof != "" {
oneofFields = append(oneofFields, oneofField{f.Type, toField(&f)})
// The rest of oneof processing happens below.
continue
}
tags := f.Tag.Get("protobuf")
tagArray := strings.Split(tags, ",")
if len(tagArray) < 2 {
panic("protobuf tag not enough fields in " + t.Name() + "." + f.Name + ": " + tags)
}
tag, err := strconv.Atoi(tagArray[1])
if err != nil {
panic("protobuf tag field not an integer: " + tagArray[1])
}
name := ""
for _, tag := range tagArray[3:] {
if strings.HasPrefix(tag, "name=") {
name = tag[5:]
}
}
// Extract unmarshaling function from the field (its type and tags).
unmarshal := fieldUnmarshaler(&f)
// Required field?
var reqMask uint64
if tagArray[2] == "req" {
bit := len(u.reqFields)
u.reqFields = append(u.reqFields, name)
reqMask = uint64(1) << uint(bit)
// TODO: if we have more than 64 required fields, we end up
// not verifying that all required fields are present.
// Fix this, perhaps using a count of required fields?
}
// Store the info in the correct slot in the message.
u.setTag(tag, toField(&f), unmarshal, reqMask, name)
}
// Find any types associated with oneof fields.
// gogo: len(oneofFields) > 0 is needed for embedded oneof messages, without a marshaler and unmarshaler
if len(oneofFields) > 0 {
var oneofImplementers []interface{}
switch m := reflect.Zero(reflect.PtrTo(t)).Interface().(type) {
case oneofFuncsIface:
_, _, _, oneofImplementers = m.XXX_OneofFuncs()
case oneofWrappersIface:
oneofImplementers = m.XXX_OneofWrappers()
}
for _, v := range oneofImplementers {
tptr := reflect.TypeOf(v) // *Msg_X
typ := tptr.Elem() // Msg_X
f := typ.Field(0) // oneof implementers have one field
baseUnmarshal := fieldUnmarshaler(&f)
tags := strings.Split(f.Tag.Get("protobuf"), ",")
fieldNum, err := strconv.Atoi(tags[1])
if err != nil {
panic("protobuf tag field not an integer: " + tags[1])
}
var name string
for _, tag := range tags {
if strings.HasPrefix(tag, "name=") {
name = strings.TrimPrefix(tag, "name=")
break
}
}
// Find the oneof field that this struct implements.
// Might take O(n^2) to process all of the oneofs, but who cares.
for _, of := range oneofFields {
if tptr.Implements(of.ityp) {
// We have found the corresponding interface for this struct.
// That lets us know where this struct should be stored
// when we encounter it during unmarshaling.
unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal)
u.setTag(fieldNum, of.field, unmarshal, 0, name)
}
}
}
}
// Get extension ranges, if any.
fn := reflect.Zero(reflect.PtrTo(t)).MethodByName("ExtensionRangeArray")
if fn.IsValid() {
if !u.extensions.IsValid() && !u.oldExtensions.IsValid() && !u.bytesExtensions.IsValid() {
panic("a message with extensions, but no extensions field in " + t.Name())
}
u.extensionRanges = fn.Call(nil)[0].Interface().([]ExtensionRange)
}
// Explicitly disallow tag 0. This will ensure we flag an error
// when decoding a buffer of all zeros. Without this code, we
// would decode and skip an all-zero buffer of even length.
// [0 0] is [tag=0/wiretype=varint varint-encoded-0].
u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) {
return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w)
}, 0, "")
// Set mask for required field check.
u.reqMask = uint64(1)<<uint(len(u.reqFields)) - 1
atomic.StoreInt32(&u.initialized, 1)
}
// setTag stores the unmarshal information for the given tag.
// tag = tag # for field
// field/unmarshal = unmarshal info for that field.
// reqMask = if required, bitmask for field position in required field list. 0 otherwise.
// name = short name of the field.
func (u *unmarshalInfo) setTag(tag int, field field, unmarshal unmarshaler, reqMask uint64, name string) {
i := unmarshalFieldInfo{field: field, unmarshal: unmarshal, reqMask: reqMask, name: name}
n := u.typ.NumField()
if tag >= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here?
for len(u.dense) <= tag {
u.dense = append(u.dense, unmarshalFieldInfo{})
}
u.dense[tag] = i
return
}
if u.sparse == nil {
u.sparse = map[uint64]unmarshalFieldInfo{}
}
u.sparse[uint64(tag)] = i
}
// fieldUnmarshaler returns an unmarshaler for the given field.
func fieldUnmarshaler(f *reflect.StructField) unmarshaler {
if f.Type.Kind() == reflect.Map {
return makeUnmarshalMap(f)
}
return typeUnmarshaler(f.Type, f.Tag.Get("protobuf"))
}
// typeUnmarshaler returns an unmarshaler for the given field type / field tag pair.
func typeUnmarshaler(t reflect.Type, tags string) unmarshaler {
tagArray := strings.Split(tags, ",")
encoding := tagArray[0]
name := "unknown"
ctype := false
isTime := false
isDuration := false
isWktPointer := false
proto3 := false
validateUTF8 := true
for _, tag := range tagArray[3:] {
if strings.HasPrefix(tag, "name=") {
name = tag[5:]
}
if tag == "proto3" {
proto3 = true
}
if strings.HasPrefix(tag, "customtype=") {
ctype = true
}
if tag == "stdtime" {
isTime = true
}
if tag == "stdduration" {
isDuration = true
}
if tag == "wktptr" {
isWktPointer = true
}
}
validateUTF8 = validateUTF8 && proto3
// Figure out packaging (pointer, slice, or both)
slice := false
pointer := false
if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
slice = true
t = t.Elem()
}
if t.Kind() == reflect.Ptr {
pointer = true
t = t.Elem()
}
if ctype {
if reflect.PtrTo(t).Implements(customType) {
if slice {
return makeUnmarshalCustomSlice(getUnmarshalInfo(t), name)
}
if pointer {
return makeUnmarshalCustomPtr(getUnmarshalInfo(t), name)
}
return makeUnmarshalCustom(getUnmarshalInfo(t), name)
} else {
panic(fmt.Sprintf("custom type: type: %v, does not implement the proto.custom interface", t))
}
}
if isTime {
if pointer {
if slice {
return makeUnmarshalTimePtrSlice(getUnmarshalInfo(t), name)
}
return makeUnmarshalTimePtr(getUnmarshalInfo(t), name)
}
if slice {
return makeUnmarshalTimeSlice(getUnmarshalInfo(t), name)
}
return makeUnmarshalTime(getUnmarshalInfo(t), name)
}
if isDuration {
if pointer {
if slice {
return makeUnmarshalDurationPtrSlice(getUnmarshalInfo(t), name)
}
return makeUnmarshalDurationPtr(getUnmarshalInfo(t), name)
}
if slice {
return makeUnmarshalDurationSlice(getUnmarshalInfo(t), name)
}
return makeUnmarshalDuration(getUnmarshalInfo(t), name)
}
if isWktPointer {
switch t.Kind() {
case reflect.Float64:
if pointer {
if slice {
return makeStdDoubleValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdDoubleValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdDoubleValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdDoubleValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Float32:
if pointer {
if slice {
return makeStdFloatValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdFloatValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdFloatValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdFloatValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Int64:
if pointer {
if slice {
return makeStdInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Uint64:
if pointer {
if slice {
return makeStdUInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdUInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdUInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdUInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Int32:
if pointer {
if slice {
return makeStdInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Uint32:
if pointer {
if slice {
return makeStdUInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdUInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdUInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdUInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.Bool:
if pointer {
if slice {
return makeStdBoolValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdBoolValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdBoolValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdBoolValueUnmarshaler(getUnmarshalInfo(t), name)
case reflect.String:
if pointer {
if slice {
return makeStdStringValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdStringValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdStringValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdStringValueUnmarshaler(getUnmarshalInfo(t), name)
case uint8SliceType:
if pointer {
if slice {
return makeStdBytesValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdBytesValuePtrUnmarshaler(getUnmarshalInfo(t), name)
}
if slice {
return makeStdBytesValueSliceUnmarshaler(getUnmarshalInfo(t), name)
}
return makeStdBytesValueUnmarshaler(getUnmarshalInfo(t), name)
default:
panic(fmt.Sprintf("unknown wktpointer type %#v", t))
}
}
// We'll never have both pointer and slice for basic types.
if pointer && slice && t.Kind() != reflect.Struct {
panic("both pointer and slice for basic type in " + t.Name())
}
switch t.Kind() {
case reflect.Bool:
if pointer {
return unmarshalBoolPtr
}
if slice {
return unmarshalBoolSlice
}
return unmarshalBoolValue
case reflect.Int32:
switch encoding {
case "fixed32":
if pointer {
return unmarshalFixedS32Ptr
}
if slice {
return unmarshalFixedS32Slice
}
return unmarshalFixedS32Value
case "varint":
// this could be int32 or enum
if pointer {
return unmarshalInt32Ptr
}
if slice {
return unmarshalInt32Slice
}
return unmarshalInt32Value
case "zigzag32":
if pointer {
return unmarshalSint32Ptr
}
if slice {
return unmarshalSint32Slice
}
return unmarshalSint32Value
}
case reflect.Int64:
switch encoding {
case "fixed64":
if pointer {
return unmarshalFixedS64Ptr
}
if slice {
return unmarshalFixedS64Slice
}
return unmarshalFixedS64Value
case "varint":
if pointer {
return unmarshalInt64Ptr
}
if slice {
return unmarshalInt64Slice
}
return unmarshalInt64Value
case "zigzag64":
if pointer {
return unmarshalSint64Ptr
}
if slice {
return unmarshalSint64Slice
}
return unmarshalSint64Value
}
case reflect.Uint32:
switch encoding {
case "fixed32":
if pointer {
return unmarshalFixed32Ptr
}
if slice {
return unmarshalFixed32Slice
}
return unmarshalFixed32Value
case "varint":
if pointer {
return unmarshalUint32Ptr
}
if slice {
return unmarshalUint32Slice
}
return unmarshalUint32Value
}
case reflect.Uint64:
switch encoding {
case "fixed64":
if pointer {
return unmarshalFixed64Ptr
}
if slice {
return unmarshalFixed64Slice
}
return unmarshalFixed64Value
case "varint":
if pointer {
return unmarshalUint64Ptr
}
if slice {
return unmarshalUint64Slice
}
return unmarshalUint64Value
}
case reflect.Float32:
if pointer {
return unmarshalFloat32Ptr
}
if slice {
return unmarshalFloat32Slice
}
return unmarshalFloat32Value
case reflect.Float64:
if pointer {
return unmarshalFloat64Ptr
}
if slice {
return unmarshalFloat64Slice
}
return unmarshalFloat64Value
case reflect.Map:
panic("map type in typeUnmarshaler in " + t.Name())
case reflect.Slice:
if pointer {
panic("bad pointer in slice case in " + t.Name())
}
if slice {
return unmarshalBytesSlice
}
return unmarshalBytesValue
case reflect.String:
if validateUTF8 {
if pointer {
return unmarshalUTF8StringPtr
}
if slice {
return unmarshalUTF8StringSlice
}
return unmarshalUTF8StringValue
}
if pointer {
return unmarshalStringPtr
}
if slice {
return unmarshalStringSlice
}
return unmarshalStringValue
case reflect.Struct:
// message or group field
if !pointer {
switch encoding {
case "bytes":
if slice {
return makeUnmarshalMessageSlice(getUnmarshalInfo(t), name)
}
return makeUnmarshalMessage(getUnmarshalInfo(t), name)
}
}
switch encoding {
case "bytes":
if slice {
return makeUnmarshalMessageSlicePtr(getUnmarshalInfo(t), name)
}
return makeUnmarshalMessagePtr(getUnmarshalInfo(t), name)
case "group":
if slice {
return makeUnmarshalGroupSlicePtr(getUnmarshalInfo(t), name)
}
return makeUnmarshalGroupPtr(getUnmarshalInfo(t), name)
}
}
panic(fmt.Sprintf("unmarshaler not found type:%s encoding:%s", t, encoding))
}
// Below are all the unmarshalers for individual fields of various types.
func unmarshalInt64Value(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x)
*f.toInt64() = v
return b, nil
}
func unmarshalInt64Ptr(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x)
*f.toInt64Ptr() = &v
return b, nil
}
func unmarshalInt64Slice(b []byte, f pointer, w int) ([]byte, error) {
if w == WireBytes { // packed
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
if x > uint64(len(b)) {
return nil, io.ErrUnexpectedEOF
}
res := b[x:]
b = b[:x]
for len(b) > 0 {
x, n = decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x)
s := f.toInt64Slice()
*s = append(*s, v)
}
return res, nil
}
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x)
s := f.toInt64Slice()
*s = append(*s, v)
return b, nil
}
func unmarshalSint64Value(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x>>1) ^ int64(x)<<63>>63
*f.toInt64() = v
return b, nil
}
func unmarshalSint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x>>1) ^ int64(x)<<63>>63
*f.toInt64Ptr() = &v
return b, nil
}
func unmarshalSint64Slice(b []byte, f pointer, w int) ([]byte, error) {
if w == WireBytes { // packed
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
if x > uint64(len(b)) {
return nil, io.ErrUnexpectedEOF
}
res := b[x:]
b = b[:x]
for len(b) > 0 {
x, n = decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x>>1) ^ int64(x)<<63>>63
s := f.toInt64Slice()
*s = append(*s, v)
}
return res, nil
}
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := int64(x>>1) ^ int64(x)<<63>>63
s := f.toInt64Slice()
*s = append(*s, v)
return b, nil
}
func unmarshalUint64Value(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := uint64(x)
*f.toUint64() = v
return b, nil
}
func unmarshalUint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
if w != WireVarint {
return b, errInternalBadWireType
}
x, n := decodeVarint(b)
if n == 0 {
return nil, io.ErrUnexpectedEOF
}
b = b[n:]
v := uint64(x)
*f.toUint64Ptr() = &v
return b, nil
}
func unmarshalUint64Slice(b []byte, f pointer, w int) ([]byte, error) {
if w == WireBytes { // packed