-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
210 lines (188 loc) · 4.07 KB
/
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
package bx
import (
"errors"
"fmt"
"time"
)
var (
// ErrNotEnoughData .
ErrNotEnoughData = errors.New("not enough byte data")
// ErrInvalidData .
ErrInvalidData = errors.New("invalid data")
)
// D is the buffer for decoding values
type D struct {
raw []byte
}
// Decoder creates a new decoder
func Decoder(raw []byte) *D {
d := D{
raw: []byte{},
}
d.raw = raw
return &d
}
func (d *D) Size() int {
return len(d.raw)
}
// Read reads any value which implements ValueType from the decoder
func (d *D) Read(vt ValueType) error {
n, err := vt.Decode(d.raw)
if err != nil {
return fmt.Errorf("can't decode value type: %w", err)
}
if len(d.raw) < n {
return ErrNotEnoughData
}
d.raw = d.raw[n:]
return nil
}
// ReadSlice .
func (d *D) ReadSlice(instanceFunc func() ValueType, applyFunc func(valueType ValueType) error) error {
size, err := d.Int64()
if err != nil {
return err
}
for i := int64(0); i < size; i++ {
lvt := instanceFunc()
if err := d.Read(lvt); err != nil {
return err
}
if err := applyFunc(lvt); err != nil {
return err
}
}
return nil
}
// Int8 reads a int8 value from the decoder
func (d *D) Int8() (int8, error) {
var val int8
vt := Number{size: 1, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Int16 reads a int16 value from the decoder
func (d *D) Int16() (int16, error) {
var val int16
vt := Number{size: 2, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Int32 reads a int32 value from the decoder
func (d *D) Int32() (int32, error) {
var val int32
vt := Number{size: 4, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Int64 reads a int64 value from the decoder
func (d *D) Int64() (int64, error) {
var val int64
vt := Number{size: 8, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Uint8 reads a uint8 value from the decoder
func (d *D) Uint8() (uint8, error) {
var val uint8
vt := Number{size: 1, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Uint16 reads a uint16 value from the decoder
func (d *D) Uint16() (uint16, error) {
var val uint16
vt := Number{size: 2, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Uint32 reads a uint32 value from the decoder
func (d *D) Uint32() (uint32, error) {
var val uint32
vt := Number{size: 4, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Uint64 reads a uint64 value from the decoder
func (d *D) Uint64() (uint64, error) {
var val uint64
vt := Number{size: 8, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Float32 reads a float32 value from the decoder
func (d *D) Float32() (float32, error) {
var val float32
vt := Number{size: 4, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// Float64 reads a float64 value from the decoder
func (d *D) Float64() (float64, error) {
var val float64
vt := Number{size: 8, value: &val}
if err := d.Read(&vt); err != nil {
return 0, err
}
return val, nil
}
// String reads a string value from the decoder
func (d *D) String() (string, error) {
vt := Bytes{}
if err := d.Read(&vt); err != nil {
return "", err
}
return vt.String(), nil
}
// Bytes reads a byte slice value from the decoder
func (d *D) Bytes() ([]byte, error) {
vt := Bytes{}
if err := d.Read(&vt); err != nil {
return nil, err
}
return vt.value, nil
}
// Strings reads a string slice value from the decoder
func (d *D) Strings() ([]string, error) {
n, err := d.Uint64()
if err != nil {
return nil, err
}
values := make([]string, 0, n)
for i := uint64(0); i < n; i++ {
val, err := d.String()
if err != nil {
return nil, err
}
values = append(values, val)
}
return values, nil
}
// Time read a time value from the decoder in UTC
func (d *D) Time() (time.Time, error) {
nano, err := d.Int64()
if err != nil {
return time.Time{}, err
}
t := time.Unix(0, nano)
t = t.UTC()
return t, nil
}