-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdecoder_bin.go
352 lines (318 loc) · 8.44 KB
/
decoder_bin.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
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// 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 bin
import (
"encoding/binary"
"fmt"
"io"
"reflect"
"go.uber.org/zap"
)
func (dec *Decoder) decodeWithOptionBin(v interface{}, option *option) (err error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return &InvalidDecoderError{reflect.TypeOf(v)}
}
// We decode rv not rv.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
err = dec.decodeBin(rv, option)
if err != nil {
return err
}
return nil
}
func (dec *Decoder) decodeBin(rv reflect.Value, opt *option) (err error) {
if opt == nil {
opt = newDefaultOption()
}
dec.currentFieldOpt = opt
unmarshaler, rv := indirect(rv, opt.is_Optional())
if traceEnabled {
zlog.Debug("decode: type",
zap.Stringer("value_kind", rv.Kind()),
zap.Bool("has_unmarshaler", (unmarshaler != nil)),
zap.Reflect("options", opt),
)
}
if opt.is_Optional() {
isPresent, e := dec.ReadUint32(binary.LittleEndian)
if e != nil {
err = fmt.Errorf("decode: %s isPresent, %s", rv.Type().String(), e)
return
}
if isPresent == 0 {
if traceEnabled {
zlog.Debug("decode: skipping optional value", zap.Stringer("type", rv.Kind()))
}
rv.Set(reflect.Zero(rv.Type()))
return
}
// we have ptr here we should not go get the element
unmarshaler, rv = indirect(rv, false)
}
if unmarshaler != nil {
if traceEnabled {
zlog.Debug("decode: using UnmarshalWithDecoder method to decode type")
}
return unmarshaler.UnmarshalWithDecoder(dec)
}
rt := rv.Type()
switch rv.Kind() {
case reflect.String:
s, e := dec.ReadRustString()
if e != nil {
err = e
return
}
rv.SetString(s)
return
case reflect.Uint8:
var n byte
n, err = dec.ReadByte()
rv.SetUint(uint64(n))
return
case reflect.Int8:
var n int8
n, err = dec.ReadInt8()
rv.SetInt(int64(n))
return
case reflect.Int16:
var n int16
n, err = dec.ReadInt16(opt.Order)
rv.SetInt(int64(n))
return
case reflect.Int32:
var n int32
n, err = dec.ReadInt32(opt.Order)
rv.SetInt(int64(n))
return
case reflect.Int64:
var n int64
n, err = dec.ReadInt64(opt.Order)
rv.SetInt(int64(n))
return
case reflect.Uint16:
var n uint16
n, err = dec.ReadUint16(opt.Order)
rv.SetUint(uint64(n))
return
case reflect.Uint32:
var n uint32
n, err = dec.ReadUint32(opt.Order)
rv.SetUint(uint64(n))
return
case reflect.Uint64:
var n uint64
n, err = dec.ReadUint64(opt.Order)
rv.SetUint(n)
return
case reflect.Float32:
var n float32
n, err = dec.ReadFloat32(opt.Order)
rv.SetFloat(float64(n))
return
case reflect.Float64:
var n float64
n, err = dec.ReadFloat64(opt.Order)
rv.SetFloat(n)
return
case reflect.Bool:
var r bool
r, err = dec.ReadBool()
rv.SetBool(r)
return
case reflect.Interface:
// skip
return nil
}
switch rt.Kind() {
case reflect.Array:
l := rt.Len()
if traceEnabled {
zlog.Debug("decoding: reading array", zap.Int("length", l))
}
switch k := rv.Type().Elem().Kind(); k {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := reflect_readArrayOfUint_(dec, l, k, rv, LE); err != nil {
return err
}
default:
for i := 0; i < l; i++ {
if err = dec.decodeBin(rv.Index(i), nil); err != nil {
return
}
}
}
return
case reflect.Slice:
var l int
if opt.hasSizeOfSlice() {
l = opt.getSizeOfSlice()
} else {
length, err := dec.ReadLength()
if err != nil {
return err
}
l = length
}
if traceEnabled {
zlog.Debug("reading slice", zap.Int("len", l), typeField("type", rv))
}
if l > dec.Remaining() {
return io.ErrUnexpectedEOF
}
switch k := rv.Type().Elem().Kind(); k {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := reflect_readArrayOfUint_(dec, l, k, rv, LE); err != nil {
return err
}
default:
rv.Set(reflect.MakeSlice(rt, 0, 0))
for i := 0; i < l; i++ {
// create new element of type rt:
element := reflect.New(rt.Elem())
// decode into element:
if err = dec.decodeBin(element, nil); err != nil {
return
}
// append to slice:
rv.Set(reflect.Append(rv, element.Elem()))
}
}
case reflect.Struct:
if err = dec.decodeStructBin(rt, rv); err != nil {
return
}
case reflect.Map:
l, err := dec.ReadLength()
if err != nil {
return err
}
if l == 0 {
// If the map has no content, keep it nil.
return nil
}
rv.Set(reflect.MakeMap(rt))
for i := 0; i < int(l); i++ {
key := reflect.New(rt.Key())
err := dec.decodeBin(key.Elem(), nil)
if err != nil {
return err
}
val := reflect.New(rt.Elem())
err = dec.decodeBin(val.Elem(), nil)
if err != nil {
return err
}
rv.SetMapIndex(key.Elem(), val.Elem())
}
return nil
default:
return fmt.Errorf("decode: unsupported type %q", rt)
}
return
}
func (dec *Decoder) decodeStructBin(rt reflect.Type, rv reflect.Value) (err error) {
l := rv.NumField()
if traceEnabled {
zlog.Debug("decode: struct", zap.Int("fields", l), zap.Stringer("type", rv.Kind()))
}
sizeOfMap := map[string]int{}
seenBinaryExtensionField := false
for i := 0; i < l; i++ {
structField := rt.Field(i)
fieldTag := parseFieldTag(structField.Tag)
if fieldTag.Skip {
if traceEnabled {
zlog.Debug("decode: skipping struct field with skip flag",
zap.String("struct_field_name", structField.Name),
)
}
continue
}
if !fieldTag.BinaryExtension && seenBinaryExtensionField {
panic(fmt.Sprintf("the `bin:\"binary_extension\"` tags must be packed together at the end of struct fields, problematic field %q", structField.Name))
}
if fieldTag.BinaryExtension {
seenBinaryExtensionField = true
// FIXME: This works only if what is in `d.data` is the actual full data buffer that
// needs to be decoded. If there is for example two structs in the buffer, this
// will not work as we would continue into the next struct.
//
// But at the same time, does it make sense otherwise? What would be the inference
// rule in the case of extra bytes available? Continue decoding and revert if it's
// not working? But how to detect valid errors?
if len(dec.data[dec.pos:]) <= 0 {
continue
}
}
v := rv.Field(i)
if !v.CanSet() {
// This means that the field cannot be set, to fix this
// we need to create a pointer to said field
if !v.CanAddr() {
// we cannot create a point to field skipping
if traceEnabled {
zlog.Debug("skipping struct field that cannot be addressed",
zap.String("struct_field_name", structField.Name),
zap.Stringer("struct_value_type", v.Kind()),
)
}
return fmt.Errorf("unable to decode a none setup struc field %q with type %q", structField.Name, v.Kind())
}
v = v.Addr()
}
if !v.CanSet() {
if traceEnabled {
zlog.Debug("skipping struct field that cannot be addressed",
zap.String("struct_field_name", structField.Name),
zap.Stringer("struct_value_type", v.Kind()),
)
}
continue
}
option := &option{
is_OptionalField: fieldTag.Option,
Order: fieldTag.Order,
}
if s, ok := sizeOfMap[structField.Name]; ok {
option.setSizeOfSlice(s)
}
if traceEnabled {
zlog.Debug("decode: struct field",
zap.Stringer("struct_field_value_type", v.Kind()),
zap.String("struct_field_name", structField.Name),
zap.Reflect("struct_field_tags", fieldTag),
zap.Reflect("struct_field_option", option),
)
}
if err = dec.decodeBin(v, option); err != nil {
return fmt.Errorf("error while decoding %q field: %w", structField.Name, err)
}
if fieldTag.SizeOf != "" {
size := sizeof(structField.Type, v)
if traceEnabled {
zlog.Debug("setting size of field",
zap.String("field_name", fieldTag.SizeOf),
zap.Int("size", size),
)
}
sizeOfMap[fieldTag.SizeOf] = size
}
}
return
}