-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
decode_map.go
356 lines (296 loc) · 6.54 KB
/
decode_map.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
package msgpack
import (
"errors"
"fmt"
"reflect"
"github.com/vmihailenco/msgpack/v5/msgpcode"
)
var errArrayStruct = errors.New("msgpack: number of fields in array-encoded struct has changed")
var (
mapStringStringPtrType = reflect.TypeOf((*map[string]string)(nil))
mapStringStringType = mapStringStringPtrType.Elem()
mapStringBoolPtrType = reflect.TypeOf((*map[string]bool)(nil))
mapStringBoolType = mapStringBoolPtrType.Elem()
)
var (
mapStringInterfacePtrType = reflect.TypeOf((*map[string]interface{})(nil))
mapStringInterfaceType = mapStringInterfacePtrType.Elem()
)
func decodeMapValue(d *Decoder, v reflect.Value) error {
n, err := d.DecodeMapLen()
if err != nil {
return err
}
typ := v.Type()
if n == -1 {
v.Set(reflect.Zero(typ))
return nil
}
if v.IsNil() {
ln := n
if d.flags&disableAllocLimitFlag == 0 {
ln = min(ln, maxMapSize)
}
v.Set(reflect.MakeMapWithSize(typ, ln))
}
if n == 0 {
return nil
}
return d.decodeTypedMapValue(v, n)
}
func (d *Decoder) decodeMapDefault() (interface{}, error) {
if d.mapDecoder != nil {
return d.mapDecoder(d)
}
return d.DecodeMap()
}
// DecodeMapLen decodes map length. Length is -1 when map is nil.
func (d *Decoder) DecodeMapLen() (int, error) {
c, err := d.readCode()
if err != nil {
return 0, err
}
if msgpcode.IsExt(c) {
if err = d.skipExtHeader(c); err != nil {
return 0, err
}
c, err = d.readCode()
if err != nil {
return 0, err
}
}
return d.mapLen(c)
}
func (d *Decoder) mapLen(c byte) (int, error) {
if c == msgpcode.Nil {
return -1, nil
}
if c >= msgpcode.FixedMapLow && c <= msgpcode.FixedMapHigh {
return int(c & msgpcode.FixedMapMask), nil
}
if c == msgpcode.Map16 {
size, err := d.uint16()
return int(size), err
}
if c == msgpcode.Map32 {
size, err := d.uint32()
return int(size), err
}
return 0, unexpectedCodeError{code: c, hint: "map length"}
}
func decodeMapStringStringValue(d *Decoder, v reflect.Value) error {
mptr := v.Addr().Convert(mapStringStringPtrType).Interface().(*map[string]string)
return d.decodeMapStringStringPtr(mptr)
}
func (d *Decoder) decodeMapStringStringPtr(ptr *map[string]string) error {
size, err := d.DecodeMapLen()
if err != nil {
return err
}
if size == -1 {
*ptr = nil
return nil
}
m := *ptr
if m == nil {
ln := size
if d.flags&disableAllocLimitFlag == 0 {
ln = min(size, maxMapSize)
}
*ptr = make(map[string]string, ln)
m = *ptr
}
for i := 0; i < size; i++ {
mk, err := d.DecodeString()
if err != nil {
return err
}
mv, err := d.DecodeString()
if err != nil {
return err
}
m[mk] = mv
}
return nil
}
func decodeMapStringInterfaceValue(d *Decoder, v reflect.Value) error {
ptr := v.Addr().Convert(mapStringInterfacePtrType).Interface().(*map[string]interface{})
return d.decodeMapStringInterfacePtr(ptr)
}
func (d *Decoder) decodeMapStringInterfacePtr(ptr *map[string]interface{}) error {
m, err := d.DecodeMap()
if err != nil {
return err
}
*ptr = m
return nil
}
func (d *Decoder) DecodeMap() (map[string]interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
m := make(map[string]interface{}, n)
for i := 0; i < n; i++ {
mk, err := d.DecodeString()
if err != nil {
return nil, err
}
mv, err := d.decodeInterfaceCond()
if err != nil {
return nil, err
}
m[mk] = mv
}
return m, nil
}
func (d *Decoder) DecodeUntypedMap() (map[interface{}]interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
m := make(map[interface{}]interface{}, n)
for i := 0; i < n; i++ {
mk, err := d.decodeInterfaceCond()
if err != nil {
return nil, err
}
mv, err := d.decodeInterfaceCond()
if err != nil {
return nil, err
}
m[mk] = mv
}
return m, nil
}
// DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values.
// Key and value types may be different.
func (d *Decoder) DecodeTypedMap() (interface{}, error) {
n, err := d.DecodeMapLen()
if err != nil {
return nil, err
}
if n <= 0 {
return nil, nil
}
key, err := d.decodeInterfaceCond()
if err != nil {
return nil, err
}
value, err := d.decodeInterfaceCond()
if err != nil {
return nil, err
}
keyType := reflect.TypeOf(key)
valueType := reflect.TypeOf(value)
if !keyType.Comparable() {
return nil, fmt.Errorf("msgpack: unsupported map key: %s", keyType.String())
}
mapType := reflect.MapOf(keyType, valueType)
ln := n
if d.flags&disableAllocLimitFlag == 0 {
ln = min(ln, maxMapSize)
}
mapValue := reflect.MakeMapWithSize(mapType, ln)
mapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
n--
if err := d.decodeTypedMapValue(mapValue, n); err != nil {
return nil, err
}
return mapValue.Interface(), nil
}
func (d *Decoder) decodeTypedMapValue(v reflect.Value, n int) error {
var (
typ = v.Type()
keyType = typ.Key()
valueType = typ.Elem()
)
for i := 0; i < n; i++ {
mk := d.newValue(keyType).Elem()
if err := d.DecodeValue(mk); err != nil {
return err
}
mv := d.newValue(valueType).Elem()
if err := d.DecodeValue(mv); err != nil {
return err
}
v.SetMapIndex(mk, mv)
}
return nil
}
func (d *Decoder) skipMap(c byte) error {
n, err := d.mapLen(c)
if err != nil {
return err
}
for i := 0; i < n; i++ {
if err := d.Skip(); err != nil {
return err
}
if err := d.Skip(); err != nil {
return err
}
}
return nil
}
func decodeStructValue(d *Decoder, v reflect.Value) error {
c, err := d.readCode()
if err != nil {
return err
}
n, err := d.mapLen(c)
if err == nil {
return d.decodeStruct(v, n)
}
var err2 error
n, err2 = d.arrayLen(c)
if err2 != nil {
return err
}
if n <= 0 {
v.Set(reflect.Zero(v.Type()))
return nil
}
fields := structs.Fields(v.Type(), d.structTag)
if n != len(fields.List) {
return errArrayStruct
}
for _, f := range fields.List {
if err := f.DecodeValue(d, v); err != nil {
return err
}
}
return nil
}
func (d *Decoder) decodeStruct(v reflect.Value, n int) error {
if n == -1 {
v.Set(reflect.Zero(v.Type()))
return nil
}
fields := structs.Fields(v.Type(), d.structTag)
for i := 0; i < n; i++ {
name, err := d.decodeStringTemp()
if err != nil {
return err
}
if f := fields.Map[name]; f != nil {
if err := f.DecodeValue(d, v); err != nil {
return err
}
continue
}
if d.flags&disallowUnknownFieldsFlag != 0 {
return fmt.Errorf("msgpack: unknown field %q", name)
}
if err := d.Skip(); err != nil {
return err
}
}
return nil
}