-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
339 lines (300 loc) · 8.49 KB
/
handler.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
package jsonpack
import (
"fmt"
"reflect"
"unsafe"
ibuf "github.com/arloliu/jsonpack/buffer"
"github.com/pkg/errors"
"github.com/modern-go/reflect2"
)
type opHandler interface {
encodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error
decodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error
// write(buf *ibuf.Buffer, data interface{}) error
encodeDynamic(buf *ibuf.Buffer, opNode *operation, data interface{}) error
decodeDynamic(buf *ibuf.Buffer, opNode *operation, v interface{}) (interface{}, error)
}
type opHandlerType uint8
const (
nullOpType opHandlerType = iota
objectOpType
structOpType
sliceOpType
arrayOpType
booleanOpType
stringOpType
int8OpType
int16LEOpType
int16BEOpType
int32LEOpType
int32BEOpType
int64LEOpType
int64BEOpType
uint8OpType
uint16LEOpType
uint16BEOpType
uint32LEOpType
uint32BEOpType
uint64LEOpType
uint64BEOpType
float32LEOpType
float32BEOpType
float64LEOpType
float64BEOpType
)
// Null type
type nullOp struct{}
func (p *nullOp) decodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error {
return errors.WithStack(&NotImplementedError{"nullOp.decodeStruct"})
}
func (p *nullOp) encodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error {
return errors.WithStack(&NotImplementedError{"nullOp.encodeStruct"})
}
func (p *nullOp) encodeDynamic(buf *ibuf.Buffer, opNode *operation, data interface{}) error {
return errors.WithStack(&NotImplementedError{"nullOp.encodeDynamic"})
}
func (p *nullOp) decodeDynamic(buf *ibuf.Buffer, opNode *operation, v interface{}) (interface{}, error) {
return nil, errors.WithStack(&NotImplementedError{"nullOp.decodeDynamic"})
}
// declare operation variables for type alias
var (
_nullOp = &nullOp{}
_structOp = &structOp{}
// _objectOp = &objectOp{}
_sliceOp = &sliceOp{}
_arrayOp = &arrayOp{}
_booleanOp = &booleanOp{}
_float32LEOp = &float32LEOp{}
_float32BEOp = &float32BEOp{}
_float64LEOp = &float64LEOp{}
_float64BEOp = &float64BEOp{}
)
var builtinTypes = map[string]opHandler{
"bool": _booleanOp,
"boolean": _booleanOp,
"string": &stringOp{},
"int8": &int8Op{},
"int16be": &int16BEOp{},
"int16le": &int16LEOp{},
"int32be": &int32BEOp{},
"int32le": &int32LEOp{},
"int64be": &int64BEOp{},
"int64le": &int64LEOp{},
"uint8": &uint8Op{},
"uint16be": &uint16BEOp{},
"uint16le": &uint16LEOp{},
"uint32be": &uint32BEOp{},
"uint32le": &uint32LEOp{},
"uint64be": &uint64BEOp{},
"uint64le": &uint64LEOp{},
"floatbe": _float32BEOp,
"floatle": _float32LEOp,
"float32be": _float32BEOp,
"float32le": _float32LEOp,
"doublebe": _float64BEOp,
"doublele": _float64LEOp,
"float64be": _float64BEOp,
"float64le": _float64LEOp,
}
var builtinOpHandlerTypes = map[string]opHandlerType{
"bool": booleanOpType,
"boolean": booleanOpType,
"string": stringOpType,
"int8": int8OpType,
"int16be": int16BEOpType,
"int16le": int16LEOpType,
"int32be": int32BEOpType,
"int32le": int32LEOpType,
"int64be": int64BEOpType,
"int64le": int64LEOpType,
"uint8": uint8OpType,
"uint16be": uint16BEOpType,
"uint16le": uint16LEOpType,
"uint32be": uint32BEOpType,
"uint32le": uint32LEOpType,
"uint64be": uint64BEOpType,
"uint64le": uint64LEOpType,
"floatbe": float32BEOpType,
"floatle": float32LEOpType,
"float32be": float32BEOpType,
"float32le": float32LEOpType,
"doublebe": float64BEOpType,
"doublele": float64LEOpType,
"float64be": float64BEOpType,
"float64le": float64LEOpType,
}
func isBuiltinType(propType *string) bool {
_, ok := builtinTypes[*propType]
return ok
}
func getPropData(opNode *operation, d interface{}) interface{} {
if len(opNode.propName) == 0 {
return d
}
switch d := d.(type) {
case map[string]interface{}:
fVal, ok := d[opNode.propName]
if !ok {
return nil
}
return fVal
case *map[string]interface{}:
return getPropData(opNode, *d)
case *interface{}:
return getPropData(opNode, *d)
default:
dType := reflect2.TypeOf(d)
switch dType.Kind() {
case reflect.Ptr:
ptrType := dType.(*reflect2.UnsafePtrType)
return getPropData(opNode, ptrType.Elem().Indirect(d))
default:
}
}
return nil
}
func derefPtr(ptr unsafe.Pointer) unsafe.Pointer {
return *(*unsafe.Pointer)(ptr)
}
func assignPtr(dst, src unsafe.Pointer) {
if dst != src {
*(*unsafe.Pointer)(dst) = src
}
}
func encodeSliceTypeDynamic(buf *ibuf.Buffer, opNode *operation, data interface{}) error {
var err error
switch items := data.(type) {
// slice type fast path
case []interface{}:
// logger.Tracef("arrayOp.encodeDynamic FASTPATH, type: %T, data: %+v", data, data)
itemLen := len(items)
itemOpNode := opNode.children[0]
buf.WriteVarUint((uint64(itemLen)))
for i := 0; i < itemLen; i++ {
err = itemOpNode.handler.encodeDynamic(buf, itemOpNode, items[i])
if err != nil {
return err
}
}
case []map[string]interface{}:
// logger.Tracef("arrayOp.encodeDynamic FASTPATH, type: %T, data: %+v", data, data)
itemLen := len(items)
itemOpNode := opNode.children[0]
buf.WriteVarUint((uint64(itemLen)))
for i := 0; i < itemLen; i++ {
err = itemOpNode.handler.encodeDynamic(buf, itemOpNode, items[i])
if err != nil {
return err
}
}
default:
// logger.Tracef("arrayOp.encodeDynamic SLOWPATH, type: %T, data: %+v", data, data)
fVal := reflect.ValueOf(data)
fKind := fVal.Type().Kind()
if fKind == reflect.Slice || fKind == reflect.Array {
itemLen := fVal.Len()
itemOpNode := opNode.children[0]
buf.WriteVarUint((uint64(itemLen)))
for i := 0; i < itemLen; i++ {
err = itemOpNode.handler.encodeDynamic(buf, itemOpNode, fVal.Index(i).Interface())
if err != nil {
return err
}
}
} else {
return errors.WithStack(&WrongTypeError{fmt.Sprintf("%T", data)})
}
}
return nil
}
func decodeSliceAnyDynamic(buf *ibuf.Buffer, opNode *operation, ptr *[]interface{}) (interface{}, error) {
var err error
var m []interface{} = *ptr
size, _ := buf.ReadVarUint()
if cap(m) < int(size) {
m = make([]interface{}, size)
} else {
m = m[:size]
}
childNode := opNode.children[0]
for i := uint64(0); i < size; i++ {
childProp := _createNewData(buf, childNode)
m[i], err = childNode.handler.decodeDynamic(buf, childNode, childProp)
if err != nil {
return nil, err
}
}
*ptr = m
return m, nil
}
func decodeSliceMapDynamic(buf *ibuf.Buffer, opNode *operation, ptr *[]map[string]interface{}) (interface{}, error) {
var m []map[string]interface{} = *ptr
var ok bool
size, _ := buf.ReadVarUint()
if cap(m) < int(size) {
m = make([]map[string]interface{}, size)
} else {
m = m[:size]
}
childNode := opNode.children[0]
for i := uint64(0); i < size; i++ {
childProp := _createNewData(buf, childNode)
result, err := childNode.handler.decodeDynamic(buf, childNode, childProp)
if err != nil {
return nil, err
}
m[i], ok = result.(map[string]interface{})
if !ok {
return nil, errors.WithStack(&TypeAssertionError{Data: result, ExpectedType: "map[string]interface{}"})
}
}
*ptr = m
return m, nil
}
func decodeSliceTypeDynamic(buf *ibuf.Buffer, opNode *operation, v interface{}) (interface{}, error) {
switch ptr := v.(type) {
case *[]interface{}:
return decodeSliceAnyDynamic(buf, opNode, ptr)
case []interface{}:
return decodeSliceAnyDynamic(buf, opNode, &ptr)
case *[]map[string]interface{}:
return decodeSliceMapDynamic(buf, opNode, ptr)
case []map[string]interface{}:
return decodeSliceMapDynamic(buf, opNode, &ptr)
}
return nil, errors.WithStack(&WrongTypeError{reflect.TypeOf(v).String()})
}
func decodeArrayTypeDynamic(buf *ibuf.Buffer, opNode *operation, v interface{}) (interface{}, error) {
var err error
var m []interface{}
var ptr *[]interface{}
size, _ := buf.ReadVarUint()
val := reflect.ValueOf(v).Elem()
if val.Len() < int(size) {
return nil, errors.Errorf("the capacity of array of decode target less than data")
}
m = val.Slice(0, int(size)).Interface().([]interface{})
ptr = &m
childNode := opNode.children[0]
for i := 0; i < int(size); i++ {
childProp := _createNewData(buf, childNode)
m[i], err = childNode.handler.decodeDynamic(buf, childNode, childProp)
if err != nil {
return nil, err
}
}
*ptr = m
return v, nil
}
func _createNewData(buf *ibuf.Buffer, opNode *operation) interface{} {
switch opNode.handlerType {
case objectOpType:
return make(map[string]interface{})
case arrayOpType, sliceOpType:
size, n := buf.ReadVarUint()
buf.Seek(-int64(n), true)
return make([]interface{}, size)
default:
return nil
}
}