-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_slice.go
90 lines (75 loc) · 2.25 KB
/
handler_slice.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
package jsonpack
import (
"reflect"
"unsafe"
ibuf "github.com/arloliu/jsonpack/buffer"
"github.com/modern-go/reflect2"
)
type sliceOp struct{}
func (p *sliceOp) decodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error {
var err error
sliceType := opNode.opType.(*reflect2.UnsafeSliceType)
itemOp := opNode.children[0]
l, _ := buf.ReadVarUint()
length := int(l)
if sliceType.UnsafeIsNil(ptr) {
// allocate new slice
sliceType.UnsafeSet(ptr, sliceType.UnsafeMakeSlice(length, length))
} else if sliceType.UnsafeLengthOf(ptr) < length {
// grow slice
sliceType.UnsafeGrow(ptr, length)
}
for i := 0; i < length; i++ {
itemPtr := sliceType.UnsafeGetIndex(ptr, i)
var newPtr unsafe.Pointer = itemPtr
if itemOp.isPtrType {
ptrType := (itemOp.opType).(*reflect2.UnsafePtrType)
// always allocate new struct instance
elemType := ptrType.Elem()
newPtr = elemType.UnsafeNew()
}
err = itemOp.handler.decodeStruct(buf, itemOp, newPtr)
if err != nil {
return err
}
if itemPtr != newPtr {
assignPtr(itemPtr, newPtr)
}
}
return nil
}
func (p *sliceOp) encodeStruct(buf *ibuf.Buffer, opNode *structOperation, ptr unsafe.Pointer) error {
var err error
sliceType := opNode.opType.(*reflect2.UnsafeSliceType)
length := sliceType.UnsafeLengthOf(ptr)
itemOp := opNode.children[0]
if sliceType.UnsafeIsNil(ptr) {
// allocate new slice
sliceType.UnsafeSet(ptr, sliceType.UnsafeMakeSlice(length, length))
} else if sliceType.UnsafeLengthOf(ptr) < length {
// grow slice
sliceType.UnsafeGrow(ptr, length)
}
buf.WriteVarUint(uint64(length))
for i := 0; i < length; i++ {
itemPtr := sliceType.UnsafeGetIndex(ptr, i)
// dereference pointer
if itemOp.isPtrType {
itemPtr = derefPtr(itemPtr)
}
err = itemOp.handler.encodeStruct(buf, itemOp, itemPtr)
if err != nil {
return err
}
}
return nil
}
func (p *sliceOp) encodeDynamic(buf *ibuf.Buffer, opNode *operation, data interface{}) error {
return encodeSliceTypeDynamic(buf, opNode, data)
}
func (p *sliceOp) decodeDynamic(buf *ibuf.Buffer, opNode *operation, v interface{}) (interface{}, error) {
if reflect2.TypeOf(v).Kind() == reflect.Array {
return _arrayOp.decodeDynamic(buf, opNode, v)
}
return decodeSliceTypeDynamic(buf, opNode, v)
}