/
value_decoder.go
510 lines (469 loc) · 12.2 KB
/
value_decoder.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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vdl
import (
"errors"
"fmt"
"math"
)
var (
errEmptyDecoderStack = errors.New("vdl: empty decoder stack")
)
// Decoder returns a decoder that traverses vv.
func (vv *Value) Decoder() Decoder {
return &valueDecoder{initial: vv}
}
// ValueDecoder is an implementation of Decoder for vdl Value.
type valueDecoder struct {
initial *Value
ignoreNext bool
stack []vdStackEntry
}
type vdStackEntry struct {
Value *Value // the vdl Value
Index int // next index or field (index into Keys for map/set)
NumStarted int // hold state for multiple StartValue() calls
IsAny bool // true iff this value is within an any value
IsOptional bool // true iff this value is within an optional value
Keys []*Value // keys for set/map
}
func (d *valueDecoder) StartValue(want *Type) error { //nolint:gocyclo
if d.ignoreNext {
d.ignoreNext = false
return nil
}
var vv *Value
if top := d.top(); top == nil {
vv = d.initial
} else {
switch top.Value.Kind() {
case Array, List:
vv = top.Value.Index(top.Index)
case Set:
vv = top.Keys[top.Index]
case Map:
switch top.NumStarted % 2 {
case 0:
vv = top.Keys[top.Index]
case 1:
vv = top.Value.MapIndex(top.Keys[top.Index])
}
case Struct:
vv = top.Value.StructField(top.Index)
case Union:
_, vv = top.Value.UnionField()
default:
return fmt.Errorf("vdl: can't StartValue on %v", top.Value.Type())
}
top.NumStarted++
}
var isAny, isOptional bool
if vv.Kind() == Any {
isAny = true
if !vv.IsNil() {
vv = vv.Elem()
}
}
if vv.Kind() == Optional {
isOptional = true
if !vv.IsNil() {
vv = vv.Elem()
}
}
// Check compatibility between the actual type and the want type. Since
// compatibility applies to the entire static type, we only need to perform
// this check for top-level decoded values, and subsequently for decoded any
// values. We skip checking non-composite want types, since those will be
// naturally caught by the Decode* calls anyways.
if len(d.stack) == 0 || isAny {
switch want.Kind() {
case Optional, Array, List, Set, Map, Struct, Union:
if !Compatible(vv.Type(), want) {
return fmt.Errorf("vdl: incompatible decode from %v into %v", vv.Type(), want)
}
}
}
entry := vdStackEntry{
Value: vv,
IsAny: isAny,
IsOptional: isOptional,
Index: -1,
}
if vv.Kind() == Map || vv.Kind() == Set {
entry.Keys = vv.Keys()
}
d.stack = append(d.stack, entry)
return nil
}
func (d *valueDecoder) IgnoreNextStartValue() {
d.ignoreNext = true
}
func (d *valueDecoder) FinishValue() error {
if len(d.stack) == 0 {
return errEmptyDecoderStack
}
d.stack = d.stack[:len(d.stack)-1]
d.ignoreNext = false
return nil
}
func (d *valueDecoder) SkipValue() error {
d.ignoreNext = false
return nil
}
func (d *valueDecoder) NextEntry() (bool, error) {
top := d.top()
if top == nil {
return false, errEmptyDecoderStack
}
top.Index++
switch top.Value.Kind() {
case Array, List, Set, Map:
switch {
case top.Index == top.Value.Len():
return true, nil
case top.Index > top.Value.Len():
return false, fmt.Errorf("vdl: NextEntry called after done, stack: %+v", d.stack)
}
}
return false, nil
}
func (d *valueDecoder) NextField() (int, error) {
top := d.top()
if top == nil {
return -1, errEmptyDecoderStack
}
switch top.Value.Kind() {
case Union:
if top.Index != -1 {
return -1, nil
}
top.Index, _ = top.Value.UnionField()
case Struct:
top.Index++
if top.Index >= top.Value.Type().NumField() {
return -1, nil
}
}
return top.Index, nil
}
func (d *valueDecoder) topValue() *Value {
if top := d.top(); top != nil {
return top.Value
}
return nil
}
func (d *valueDecoder) Type() *Type {
if top := d.top(); top != nil {
return top.Value.Type()
}
return nil
}
func (d *valueDecoder) IsAny() bool {
if top := d.top(); top != nil {
return top.IsAny
}
return false
}
func (d *valueDecoder) IsOptional() bool {
if top := d.top(); top != nil {
return top.IsOptional
}
return false
}
func (d *valueDecoder) IsNil() bool {
if top := d.top(); top != nil {
return top.Value.IsNil()
}
return false
}
func (d *valueDecoder) Index() int {
if top := d.top(); top != nil {
return top.Index
}
return -1
}
func (d *valueDecoder) LenHint() int {
if top := d.top(); top != nil {
switch top.Value.Kind() {
case Array, List, Set, Map:
return top.Value.Len()
}
}
return -1
}
func (d *valueDecoder) DecodeBool() (bool, error) {
topV := d.topValue()
if topV == nil {
return false, errEmptyDecoderStack
}
if topV.Kind() == Bool {
return topV.Bool(), nil
}
return false, fmt.Errorf("vdl: incompatible decode from %v into bool", topV.Type())
}
func (d *valueDecoder) DecodeUint(bitlen int) (uint64, error) {
const errFmt = "vdl: conversion from %v into uint%d loses precision: %v"
topV, ubitlen := d.topValue(), uint(bitlen)
if topV == nil {
return 0, errEmptyDecoderStack
}
switch topV.Kind() {
case Byte, Uint16, Uint32, Uint64:
x := topV.Uint()
if shift := 64 - ubitlen; x != (x<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return x, nil
case Int8, Int16, Int32, Int64:
x := topV.Int()
ux := uint64(x)
if shift := 64 - ubitlen; x < 0 || ux != (ux<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return ux, nil
case Float32, Float64:
x := topV.Float()
ux := uint64(x)
if shift := 64 - ubitlen; x != float64(ux) || ux != (ux<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return ux, nil
default:
return 0, fmt.Errorf("vdl: incompatible decode from %v into uint%d", topV.Type(), bitlen)
}
}
func (d *valueDecoder) DecodeInt(bitlen int) (int64, error) {
const errFmt = "vdl: conversion from %v into int%d loses precision: %v"
topV, ubitlen := d.topValue(), uint(bitlen)
if topV == nil {
return 0, errEmptyDecoderStack
}
switch topV.Kind() {
case Byte, Uint16, Uint32, Uint64:
x := topV.Uint()
ix := int64(x)
// The shift uses 65 since the topmost bit is the sign bit. I.e. 32 bit
// numbers should be shifted by 33 rather than 32.
if shift := 65 - ubitlen; ix < 0 || x != (x<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return ix, nil
case Int8, Int16, Int32, Int64:
x := topV.Int()
if shift := 64 - ubitlen; x != (x<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return x, nil
case Float32, Float64:
x := topV.Float()
ix := int64(x)
if shift := 64 - ubitlen; x != float64(ix) || ix != (ix<<shift)>>shift {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return ix, nil
default:
return 0, fmt.Errorf("vdl: incompatible decode from %v into int%d", topV.Type(), bitlen)
}
}
func (d *valueDecoder) DecodeFloat(bitlen int) (float64, error) {
const errFmt = "vdl: conversion from %v into float%d loses precision: %v"
topV := d.topValue()
if topV == nil {
return 0, errEmptyDecoderStack
}
switch topV.Kind() {
case Byte, Uint16, Uint32, Uint64:
x := topV.Uint()
var max uint64
if bitlen > 32 {
max = float64MaxInt
} else {
max = float32MaxInt
}
if x > max {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return float64(x), nil
case Int8, Int16, Int32, Int64:
x := topV.Int()
var min, max int64
if bitlen > 32 {
min, max = float64MinInt, float64MaxInt
} else {
min, max = float32MinInt, float32MaxInt
}
if x < min || x > max {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return float64(x), nil
case Float32, Float64:
x := topV.Float()
if bitlen <= 32 && (x < -math.MaxFloat32 || x > math.MaxFloat32) {
return 0, fmt.Errorf(errFmt, topV.Type(), bitlen, x)
}
return x, nil
default:
return 0, fmt.Errorf("vdl: incompatible decode from %v into float%d", topV.Type(), bitlen)
}
}
func (d *valueDecoder) DecodeBytes(fixedlen int, v *[]byte) error {
topV := d.topValue()
if topV == nil {
return errEmptyDecoderStack
}
if !topV.Type().IsBytes() {
return DecodeConvertedBytes(d, fixedlen, v)
}
if fixedlen >= 0 && fixedlen != topV.Len() {
return fmt.Errorf("vdl: %v got %d bytes, want fixed len %d", topV.Type(), topV.Len(), fixedlen)
}
if cap(*v) >= topV.Len() {
*v = (*v)[:topV.Len()]
} else {
*v = make([]byte, topV.Len())
}
copy(*v, topV.Bytes())
return nil
}
func (d *valueDecoder) DecodeString() (string, error) {
topV := d.topValue()
if topV == nil {
return "", errEmptyDecoderStack
}
switch topV.Kind() {
case String:
return topV.RawString(), nil
case Enum:
return topV.EnumLabel(), nil
}
return "", fmt.Errorf("vdl: incompatible decode from %v into string", topV.Type())
}
func (d *valueDecoder) DecodeTypeObject() (*Type, error) {
topV := d.topValue()
if topV == nil {
return nil, errEmptyDecoderStack
}
if topV.Type() == TypeObjectType {
return topV.TypeObject(), nil
}
return nil, fmt.Errorf("vdl: incompatible decode from %v into typeobject", topV.Type())
}
func (d *valueDecoder) top() *vdStackEntry {
if len(d.stack) > 0 {
return &d.stack[len(d.stack)-1]
}
return nil
}
// The ReadValue* and NextEntryValue* methods just call methods in sequence.
func (d *valueDecoder) ReadValueBool() (bool, error) {
if err := d.StartValue(BoolType); err != nil {
return false, err
}
value, err := d.DecodeBool()
if err != nil {
return false, err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueString() (string, error) {
if err := d.StartValue(StringType); err != nil {
return "", err
}
value, err := d.DecodeString()
if err != nil {
return "", err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueUint(bitlen int) (uint64, error) {
if err := d.StartValue(Uint64Type); err != nil {
return 0, err
}
value, err := d.DecodeUint(bitlen)
if err != nil {
return 0, err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueInt(bitlen int) (int64, error) {
if err := d.StartValue(Int64Type); err != nil {
return 0, err
}
value, err := d.DecodeInt(bitlen)
if err != nil {
return 0, err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueFloat(bitlen int) (float64, error) {
if err := d.StartValue(Float64Type); err != nil {
return 0, err
}
value, err := d.DecodeFloat(bitlen)
if err != nil {
return 0, err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueTypeObject() (*Type, error) {
if err := d.StartValue(TypeObjectType); err != nil {
return nil, err
}
value, err := d.DecodeTypeObject()
if err != nil {
return nil, err
}
return value, d.FinishValue()
}
func (d *valueDecoder) ReadValueBytes(fixedLen int, x *[]byte) error {
if err := d.StartValue(ttByteList); err != nil {
return err
}
if err := d.DecodeBytes(fixedLen, x); err != nil {
return err
}
return d.FinishValue()
}
func (d *valueDecoder) NextEntryValueBool() (done bool, _ bool, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, false, err
}
value, err := d.ReadValueBool()
return false, value, err
}
func (d *valueDecoder) NextEntryValueString() (done bool, _ string, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, "", err
}
value, err := d.ReadValueString()
return false, value, err
}
func (d *valueDecoder) NextEntryValueUint(bitlen int) (done bool, _ uint64, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, 0, err
}
value, err := d.ReadValueUint(bitlen)
return false, value, err
}
func (d *valueDecoder) NextEntryValueInt(bitlen int) (done bool, _ int64, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, 0, err
}
value, err := d.ReadValueInt(bitlen)
return false, value, err
}
func (d *valueDecoder) NextEntryValueFloat(bitlen int) (done bool, _ float64, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, 0, err
}
value, err := d.ReadValueFloat(bitlen)
return false, value, err
}
func (d *valueDecoder) NextEntryValueTypeObject() (done bool, _ *Type, _ error) {
if done, err := d.NextEntry(); done || err != nil {
return done, nil, err
}
value, err := d.ReadValueTypeObject()
return false, value, err
}