-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.go
540 lines (487 loc) · 12.7 KB
/
encoder.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
package binary
import (
"fmt"
"math"
"reflect"
)
// NewEncoder make a new Encoder object with buffer size.
func NewEncoder(size int) *Encoder {
return NewEncoderEndian(size, DefaultEndian)
}
// NewEncoderBuffer make a new Encoder object with buffer.
func NewEncoderBuffer(buffer []byte) *Encoder {
p := &Encoder{}
//assert(buffer != nil, "nil buffer")
p.buff = buffer
p.endian = DefaultEndian
p.pos = 0
return p
}
// NewEncoderEndian make a new Encoder object with buffer size and endian.
func NewEncoderEndian(size int, endian Endian) *Encoder {
p := &Encoder{}
p.Init(size, endian)
return p
}
// Encoder is used to encode go data to byte array.
type Encoder struct {
coder
}
// Init initialize Encoder with buffer size and endian.
func (encoder *Encoder) Init(size int, endian Endian) {
encoder.buff = make([]byte, size)
encoder.pos = 0
encoder.endian = endian
}
// ResizeBuffer confirm that len(buffer) >= size and alloc larger buffer if necessary
// It will call Reset to initial encoder state of buffer
func (encoder *Encoder) ResizeBuffer(size int) bool {
ok := len(encoder.buff) < size
if ok {
encoder.buff = make([]byte, size)
}
encoder.Reset()
return ok
}
// Bool encode a bool value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Bool(x bool) {
if encoder.boolBit == 0 {
b := encoder.reserve(1)
b[0] = 0
encoder.boolPos = encoder.pos - 1
}
if mask := byte(1 << encoder.boolBit); x {
encoder.buff[encoder.boolPos] |= mask
}
encoder.boolBit = (encoder.boolBit + 1) % 8
}
// Int8 encode an int8 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Int8(x int8) {
encoder.Uint8(uint8(x))
}
// Uint8 encode a uint8 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Uint8(x uint8) {
b := encoder.reserve(1)
b[0] = x
}
// Int16 encode an int16 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Int16(x int16, packed bool) {
if packed {
encoder.Varint(int64(x))
} else {
encoder.Uint16(uint16(x), false)
}
}
// Uint16 encode a uint16 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Uint16(x uint16, packed bool) {
if packed {
encoder.Uvarint(uint64(x))
} else {
b := encoder.reserve(2)
encoder.endian.PutUint16(b, x)
}
}
// Int32 encode an int32 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Int32(x int32, packed bool) {
if packed {
encoder.Varint(int64(x))
} else {
encoder.Uint32(uint32(x), false)
}
}
// Uint32 encode a uint32 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Uint32(x uint32, packed bool) {
if packed {
encoder.Uvarint(uint64(x))
} else {
b := encoder.reserve(4)
encoder.endian.PutUint32(b, x)
}
}
// Int64 encode an int64 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Int64(x int64, packed bool) {
if packed {
encoder.Varint(x)
} else {
encoder.Uint64(uint64(x), false)
}
}
// Uint64 encode a uint64 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Uint64(x uint64, packed bool) {
if packed {
encoder.Uvarint(x)
} else {
b := encoder.reserve(8)
encoder.endian.PutUint64(b, x)
}
}
// Float32 encode a float32 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Float32(x float32) {
encoder.Uint32(math.Float32bits(x), false)
}
// Float64 encode a float64 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Float64(x float64) {
encoder.Uint64(math.Float64bits(x), false)
}
// Complex64 encode a complex64 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Complex64(x complex64) {
encoder.Uint32(math.Float32bits(real(x)), false)
encoder.Uint32(math.Float32bits(imag(x)), false)
}
// Complex128 encode a complex128 value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) Complex128(x complex128) {
encoder.Uint64(math.Float64bits(real(x)), false)
encoder.Uint64(math.Float64bits(imag(x)), false)
}
// String encode a string value to Encoder buffer.
// It will panic if buffer is not enough.
func (encoder *Encoder) String(x string) {
_b := []byte(x)
size := len(_b)
encoder.Uvarint(uint64(size))
buff := encoder.reserve(size)
copy(buff, _b)
}
// Int encode an int value to Encoder buffer.
// It will panic if buffer is not enough.
// It use Varint() to encode as varint(1~10 bytes)
func (encoder *Encoder) Int(x int) {
encoder.Varint(int64(x))
}
// Uint encode a uint value to Encoder buffer.
// It will panic if buffer is not enough.
// It use Uvarint() to encode as uvarint(1~10 bytes)
func (encoder *Encoder) Uint(x uint) {
encoder.Uvarint(uint64(x))
}
// Varint encode an int64 value to Encoder buffer with varint(1~10 bytes).
// It will panic if buffer is not enough.
func (encoder *Encoder) Varint(x int64) int {
return encoder.Uvarint(ToUvarint(x))
}
// Uvarint encode a uint64 value to Encoder buffer with varint(1~10 bytes).
// It will panic if buffer is not enough.
func (encoder *Encoder) Uvarint(x uint64) int {
i, _x := 0, x
for ; _x >= 0x80; _x >>= 7 {
encoder.Uint8(byte(_x) | 0x80)
i++
}
encoder.Uint8(byte(_x))
return i + 1
}
// Value encode an interface value to Encoder buffer.
// It will return none-nil error if x contains unsupported types
// or buffer is not enough.
// It will check if x implements interface BinaryEncoder and use x.Encode first.
func (encoder *Encoder) Value(x interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
err = e.(error)
}
}()
encoder.resetBoolCoder() //reset bool writer
if encoder.fastValue(x) { //fast value path
return nil
}
v := reflect.ValueOf(x)
if p, ok := x.(BinaryEncoder); ok {
if _, _ok := x.(BinarySizer); !_ok { //interface verification
panic(fmt.Errorf("expect but not BinarySizer: %s", v.Type().String()))
}
r, err := p.Encode(encoder.buff[encoder.pos:])
if err == nil {
encoder.reserve(len(r))
}
return err
}
if _, _ok := x.(BinarySizer); _ok { //interface verification
panic(fmt.Errorf("unexpected BinarySizer: %s", v.Type().String()))
}
return encoder.value(reflect.Indirect(v), false)
}
func (encoder *Encoder) fastValue(x interface{}) bool {
switch d := x.(type) {
case int:
encoder.Int(d)
case uint:
encoder.Uint(d)
case bool:
encoder.Bool(d)
case int8:
encoder.Int8(d)
case uint8:
encoder.Uint8(d)
case int16:
encoder.Int16(d, false)
case uint16:
encoder.Uint16(d, false)
case int32:
encoder.Int32(d, false)
case uint32:
encoder.Uint32(d, false)
case float32:
encoder.Float32(d)
case int64:
encoder.Int64(d, false)
case uint64:
encoder.Uint64(d, false)
case float64:
encoder.Float64(d)
case complex64:
encoder.Complex64(d)
case complex128:
encoder.Complex128(d)
case string:
encoder.String(d)
case []bool:
l := len(d)
encoder.Uvarint(uint64(l))
var b []byte
for i := 0; i < l; i++ {
bit := i % 8
mask := byte(1 << uint(bit))
if bit == 0 {
b = encoder.reserve(1)
b[0] = 0
}
if x := d[i]; x {
b[0] |= mask
}
}
case []int8:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Int8(d[i])
}
case []uint8:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Uint8(d[i])
}
case []int16:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Int16(d[i], false)
}
case []uint16:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Uint16(d[i], false)
}
case []int32:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Int32(d[i], false)
}
case []uint32:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Uint32(d[i], false)
}
case []int64:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Int64(d[i], false)
}
case []uint64:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Uint64(d[i], false)
}
case []float32:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Float32(d[i])
}
case []float64:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Float64(d[i])
}
case []complex64:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Complex64(d[i])
}
case []complex128:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Complex128(d[i])
}
case []string:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.String(d[i])
}
case []int:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Int(d[i])
}
case []uint:
l := len(d)
encoder.Uvarint(uint64(len(d)))
for i := 0; i < l; i++ {
encoder.Uint(d[i])
}
default:
return false
}
return true
}
func (encoder *Encoder) value(v reflect.Value, packed bool) error {
// check Packer interface for every value is perfect
// but encoder is too costly
//
// if t := v.Type(); t.Implements(tPacker) {
// if !t.Implements(tSizer) { //interface verification
// panic(fmt.Errorf("pected but not Sizer: %s", t.String()))
// }
// packer := v.Interface().(Packer)
// reault, err := packer.Pack(encoder.buff[encoder.pos:])
// if err == nil {
// encoder.reserve(len(reault))
// }
// return err
// } else {
// if t.Implements(tSizer) { //interface verification
// panic(fmt.Errorf("unexpected Sizer: %s", v.Type().String()))
// }
// }
switch k := v.Kind(); k {
case reflect.Int:
encoder.Int(int(v.Int()))
case reflect.Uint:
encoder.Uint(uint(v.Uint()))
case reflect.Bool:
encoder.Bool(v.Bool())
case reflect.Int8:
encoder.Int8(int8(v.Int()))
case reflect.Int16:
encoder.Int16(int16(v.Int()), packed)
case reflect.Int32:
encoder.Int32(int32(v.Int()), packed)
case reflect.Int64:
encoder.Int64(v.Int(), packed)
case reflect.Uint8:
encoder.Uint8(uint8(v.Uint()))
case reflect.Uint16:
encoder.Uint16(uint16(v.Uint()), packed)
case reflect.Uint32:
encoder.Uint32(uint32(v.Uint()), packed)
case reflect.Uint64:
encoder.Uint64(v.Uint(), packed)
case reflect.Float32:
encoder.Float32(float32(v.Float()))
case reflect.Float64:
encoder.Float64(v.Float())
case reflect.Complex64:
x := v.Complex()
encoder.Complex64(complex64(x))
case reflect.Complex128:
x := v.Complex()
encoder.Complex128(x)
case reflect.String:
encoder.String(v.String())
case reflect.Slice, reflect.Array:
if !validUserType(v.Type().Elem()) { //verify array element is valid
return fmt.Errorf("binary.Encoder.Value: unsupported type %s", v.Type().String())
}
if encoder.boolArray(v) < 0 { //deal with bool array first
l := v.Len()
encoder.Uvarint(uint64(l))
for i := 0; i < l; i++ {
assert(encoder.value(v.Index(i), packed) == nil, "")
}
}
case reflect.Map:
t := v.Type()
kt := t.Key()
vt := t.Elem()
if !validUserType(kt) ||
!validUserType(vt) { //verify map key and value type are both valid
return fmt.Errorf("binary.Decoder.Value: unsupported type %s", v.Type().String())
}
keys := v.MapKeys()
l := len(keys)
encoder.Uvarint(uint64(l))
for i := 0; i < l; i++ {
key := keys[i]
assert(encoder.value(key, packed) == nil, "")
assert(encoder.value(v.MapIndex(key), packed) == nil, "")
}
case reflect.Struct:
return queryStruct(v.Type()).encode(encoder, v)
case reflect.Ptr:
if !validUserType(v.Type()) {
return fmt.Errorf("binary.Encoder.Value: unsupported type %s", v.Type().String())
}
if !v.IsNil() {
encoder.Bool(true)
if e := v.Elem(); e.Kind() != reflect.Ptr {
return encoder.value(e, packed)
}
} else {
encoder.Bool(false)
// if encoder.nilPointer(v.Type()) < 0 {
// return fmt.Errorf("binary.Encoder.Value: unsupported type [%s]", v.Type().String())
// }
}
// case reflect.Invalid://BUG: it will panic to get zero.Type
// return fmt.Errorf("binary.Encoder.Value: unsupported type [%s]", v.Kind().String())
default:
return fmt.Errorf("binary.Encoder.Value: unsupported type [%s]", v.Type().String())
}
return nil
}
// encode bool array
func (encoder *Encoder) boolArray(v reflect.Value) int {
if k := v.Kind(); k == reflect.Slice || k == reflect.Array {
if v.Type().Elem().Kind() == reflect.Bool {
l := v.Len()
encoder.Uvarint(uint64(l))
var b []byte
for i := 0; i < l; i++ {
bit := i % 8
mask := byte(1 << uint(bit))
if bit == 0 {
b = encoder.reserve(1)
b[0] = 0
}
if x := v.Index(i).Bool(); x {
b[0] |= mask
}
}
return sizeofBoolArray(l)
}
}
return -1
}