-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic.go
342 lines (301 loc) · 11 KB
/
basic.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
package y3
import (
"bytes"
"fmt"
"reflect"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
// basicEncoder is a Encoder for BasicTestData data types
type basicEncoder interface {
// Encode: encode interface to bytes
Encode(input interface{}, signals ...*signal) (buf []byte, err error)
}
// basicEncoderImpl is implementation of the basicEncoder interface
type basicEncoderImpl struct {
observe byte
root byte
forbidUserKey func(key byte) bool
allowSignalKey func(key byte) bool
}
// basicEncoderOption create basicEncoderImpl with option
type basicEncoderOption func(*basicEncoderImpl)
// basicEncoderOptionRoot set root value for creating basicEncoderImpl
func basicEncoderOptionRoot(root byte) basicEncoderOption {
return func(b *basicEncoderImpl) {
b.root = root
}
}
// basicEncoderOptionForbidUserKey set func to forbid some key
func basicEncoderOptionForbidUserKey(f func(key byte) bool) basicEncoderOption {
return func(b *basicEncoderImpl) {
b.forbidUserKey = f
}
}
// basicEncoderOptionAllowSignalKey set func to allow signal key
func basicEncoderOptionAllowSignalKey(f func(key byte) bool) basicEncoderOption {
return func(b *basicEncoderImpl) {
b.allowSignalKey = f
}
}
// newBasicEncoder create a basicEncoder interface
func newBasicEncoder(observe byte, options ...func(*basicEncoderImpl)) basicEncoder {
encoder := &basicEncoderImpl{observe: observe, root: utils.EmptyKey}
for _, option := range options {
option(encoder)
}
if encoder.forbidUserKey != nil && encoder.forbidUserKey(observe) {
panic(fmt.Errorf("prohibit the use of this key: %#x", observe))
}
return encoder
}
// Encode encode interface{} to bytes
func (e *basicEncoderImpl) Encode(input interface{}, signals ...*signal) (buf []byte, err error) {
encoders := make([]*PrimitivePacketEncoder, 0)
for _, signal := range signals {
encoders = append(encoders, signal.ToEncoder(e.allowSignalKey))
}
return e.encodeBasic(input, encoders)
}
// encodeBasic encode interface{} to bytes, and inserting signals
func (e *basicEncoderImpl) encodeBasic(input interface{}, signals []*PrimitivePacketEncoder) ([]byte, error) {
if e.observe == 0 {
panic(fmt.Errorf("observe cannot be 0"))
}
var primitiveEncoder *PrimitivePacketEncoder
value := reflect.ValueOf(input)
if value.Type() == utils.TypeOfByteSlice {
primitiveEncoder = e.encodeBytes(input)
} else {
switch value.Kind() {
case reflect.String:
primitiveEncoder = e.encodeBasicString(input)
case reflect.Int32:
primitiveEncoder = e.encodeBasicInt32(input)
case reflect.Uint32:
primitiveEncoder = e.encodeBasicUint32(input)
case reflect.Int64:
primitiveEncoder = e.encodeBasicInt64(input)
case reflect.Uint64:
primitiveEncoder = e.encodeBasicUint64(input)
case reflect.Float32:
primitiveEncoder = e.encodeBasicFloat32(input)
case reflect.Float64:
primitiveEncoder = e.encodeBasicFloat64(input)
case reflect.Bool:
primitiveEncoder = e.encodeBasicBool(input)
case reflect.Array, reflect.Slice:
//e.marshalBasicSlice(value, e.root)
return e.encodeBasicSlice(value, signals)
default:
panic(fmt.Errorf("marshal error, no matching type: %v", value.Kind()))
}
}
if primitiveEncoder == nil {
panic("PrimitivePacketEncoder is nil")
}
if !utils.IsEmptyKey(e.root) {
root := NewNodePacketEncoder(int(e.root))
for _, signal := range signals {
root.AddPrimitivePacket(signal)
}
root.AddPrimitivePacket(primitiveEncoder)
return root.Encode(), nil
}
buf := make([][]byte, 0)
for _, signal := range signals {
buf = append(buf, signal.Encode())
}
buf = append(buf, primitiveEncoder.Encode())
return bytes.Join(buf, []byte{}), nil
}
// encodeBasicSlice encode reflect.Value of slice, and inserting signals
func (e *basicEncoderImpl) encodeBasicSlice(value reflect.Value, signals []*PrimitivePacketEncoder) ([]byte, error) {
var nodeEncoder *NodePacketEncoder
switch value.Type() {
case utils.TyeOfStringSlice:
nodeEncoder = e.encodeBasicStringSlice(value)
case utils.TyeOfInt32Slice:
nodeEncoder = e.encodeBasicInt32Slice(value)
case utils.TyeOfUint32Slice:
nodeEncoder = e.encodeBasicUint32Slice(value)
case utils.TyeOfInt64Slice:
nodeEncoder = e.encodeBasicInt64Slice(value)
case utils.TyeOfUint64Slice:
nodeEncoder = e.encodeBasicUint64Slice(value)
case utils.TyeOfFloat32Slice:
nodeEncoder = e.encodeBasicFloat32Slice(value)
case utils.TyeOfFloat64Slice:
nodeEncoder = e.encodeBasicFloat64Slice(value)
case utils.TyeOfBoolSlice:
nodeEncoder = e.encodeBasicBoolSlice(value)
default:
panic(fmt.Errorf("marshal error, no matching type in SliceTestData: %v", value.Index(0).Kind()))
}
if nodeEncoder == nil {
panic("NodePacketEncoder is nil")
}
if !utils.IsEmptyKey(e.root) {
root := NewNodePacketEncoder(int(e.root))
for _, signal := range signals {
root.AddPrimitivePacket(signal)
}
root.AddNodePacket(nodeEncoder)
return root.Encode(), nil
}
buf := make([][]byte, 0)
for _, signal := range signals {
buf = append(buf, signal.Encode())
}
buf = append(buf, nodeEncoder.Encode())
return bytes.Join(buf, []byte{}), nil
}
// encodeBasicString encode string to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicString(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetStringValue(fmt.Sprintf("%v", input))
return encoder
}
// encodeBasicInt32 encode int32 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicInt32(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetInt32Value(input.(int32))
return encoder
}
// encodeBasicUint32 encode uint32 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicUint32(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetUInt32Value(input.(uint32))
return encoder
}
// encodeBasicInt64 encode int64 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicInt64(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetInt64Value(input.(int64))
return encoder
}
// encodeBasicUint64 encode uint64 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicUint64(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetUInt64Value(input.(uint64))
return encoder
}
// encodeBasicFloat32 encode float32 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicFloat32(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetFloat32Value(input.(float32))
return encoder
}
// encodeBasicFloat64 encode float64 to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicFloat64(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetFloat64Value(input.(float64))
return encoder
}
// encodeBasicBool encode bool to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBasicBool(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetBoolValue(input.(bool))
return encoder
}
// encodeBytes encode []byte to PrimitivePacketEncoder
func (e *basicEncoderImpl) encodeBytes(input interface{}) *PrimitivePacketEncoder {
var encoder = NewPrimitivePacketEncoder(int(e.observe))
encoder.SetBytesValue(input.([]byte))
return encoder
}
// encodeBasicStringSlice encode reflect.Value of []string to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToStringSlice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetStringValue(fmt.Sprintf("%v", v))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicInt32Slice encode reflect.Value of []int32 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicInt32Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToInt64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetInt32Value(int32(v.(int64)))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicUint32Slice encode reflect.Value of []uint32 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicUint32Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToUInt64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetUInt32Value(uint32(v.(uint64)))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicInt64Slice encode reflect.Value of []int64 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicInt64Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToInt64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetInt64Value(v.(int64))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicUint64Slice encode reflect.Value of []uint64 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicUint64Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToUInt64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetUInt64Value(v.(uint64))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicFloat32Slice encode reflect.Value of []float32 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicFloat32Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToUFloat64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetFloat32Value(float32(v.(float64)))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicFloat64Slice encode reflect.Value of []float64 to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicFloat64Slice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToUFloat64Slice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetFloat64Value(v.(float64))
node.AddPrimitivePacket(item)
}
}
return node
}
// encodeBasicBoolSlice encode reflect.Value of []bool to NodePacketEncoder
func (e *basicEncoderImpl) encodeBasicBoolSlice(value reflect.Value) *NodePacketEncoder {
var node = NewNodeSlicePacketEncoder(int(e.observe))
if out, ok := utils.ToBoolSlice(value.Interface()); ok {
for _, v := range out {
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
item.SetBoolValue(v.(bool))
node.AddPrimitivePacket(item)
}
}
return node
}