-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct.go
233 lines (200 loc) · 5.35 KB
/
struct.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
// cache struct info to improve encoding/decoding efficiency.
package binary
import (
"fmt"
"reflect"
)
// RegStruct regist struct info to improve encoding/decoding efficiency.
// Regist by a nil pointer is aviable.
// RegStruct((*someStruct)(nil)) is recommended usage.
func RegStruct(data interface{}) error {
return _structInfoMgr.regist(reflect.TypeOf(data))
}
var _structInfoMgr structInfoMgr
func init() {
_structInfoMgr.init()
}
type structInfoMgr struct {
reg map[string]*structInfo
}
func (mgr *structInfoMgr) init() {
mgr.reg = make(map[string]*structInfo)
}
func (mgr *structInfoMgr) regist(t reflect.Type) error {
if _t, _, err := mgr.deepStructType(t, true); err == nil {
if mgr.query(_t) == nil {
p := &structInfo{}
if p.parse(_t) {
mgr.reg[p.identify] = p
}
} else {
return fmt.Errorf("binary: regist duplicate type %s", _t.String())
}
} else {
return err
}
return nil
}
func (mgr *structInfoMgr) query(t reflect.Type) *structInfo {
if _t, _ok, _ := mgr.deepStructType(t, false); _ok {
if p, ok := mgr.reg[_t.String()]; ok {
return p
}
}
return nil
}
func (mgr *structInfoMgr) deepStructType(t reflect.Type, needErr bool) (reflect.Type, bool, error) {
_t := t
for _t.Kind() == reflect.Ptr {
_t = _t.Elem()
}
if _t.Kind() != reflect.Struct {
if needErr {
return _t, false, fmt.Errorf("binary: only struct is aviable for regist, but got %s", t.String())
}
return _t, false, nil
}
return _t, true, nil
}
//informatin of a struct
type structInfo struct {
identify string //reflect.Type.String()
fields []*fieldInfo
}
func (info *structInfo) encode(encoder *Encoder, v reflect.Value) error {
//assert(v.Kind() == reflect.Struct, v.Type().String())
t := v.Type()
for i, n := 0, v.NumField(); i < n; i++ {
// see comment for corresponding code in decoder.value()
finfo := info.field(i)
if f := v.Field(i); finfo.isValid(i, t) {
if err := encoder.value(f, finfo.isPacked()); err != nil {
return err
}
}
}
return nil
}
func (info *structInfo) decode(decoder *Decoder, v reflect.Value) error {
t := v.Type()
//assert(t.Kind() == reflect.Struct, t.String())
for i, n := 0, v.NumField(); i < n; i++ {
finfo := info.field(i)
if f := v.Field(i); finfo.isValid(i, t) {
if err := decoder.value(f, false, finfo.isPacked()); err != nil {
return err
}
}
}
return nil
}
func (info *structInfo) decodeSkipByType(decoder *Decoder, t reflect.Type, packed bool) int {
//assert(t.Kind() == reflect.Struct, t.String())
sum := 0
for i, n := 0, t.NumField(); i < n; i++ {
f := info.field(i)
ft := f.Type(i, t)
s := decoder.skipByType(ft, f.isPacked())
assert(s >= 0, "skip struct field fail:"+ft.String()) //I'm sure here cannot find unsupported type
sum += s
}
return sum
}
func (info *structInfo) bitsOfValue(v reflect.Value) int {
t := v.Type()
//assert(t.Kind() == reflect.Struct,t.String())
sum := 0
for i, n := 0, v.NumField(); i < n; i++ {
if finfo := info.field(i); finfo.isValid(i, t) {
if s := bitsOfValue(v.Field(i), false, finfo.isPacked()); s >= 0 {
sum += s
} else {
return -1 //invalid field type
}
}
}
return sum
}
func (info *structInfo) sizeofNilPointer(t reflect.Type) int {
sum := 0
for i, n := 0, info.fieldNum(t); i < n; i++ {
if info.fieldValid(i, t) {
if s := sizeofNilPointer(info.field(i).Type(i, t)); s >= 0 {
sum += s
} else {
return -1 //invalid field type
}
}
}
return sum
}
//check if field i of t valid for encoding/decoding
func (info *structInfo) fieldValid(i int, t reflect.Type) bool {
return info.field(i).isValid(i, t)
}
func (info *structInfo) fieldNum(t reflect.Type) int {
if info == nil {
return t.NumField()
}
return info.numField()
}
func (info *structInfo) parse(t reflect.Type) bool {
//assert(t.Kind() == reflect.Struct, t.String())
info.identify = t.String()
for i, n := 0, t.NumField(); i < n; i++ {
f := t.Field(i)
field := &fieldInfo{}
field.field = f
tag := f.Tag.Get("binary")
field.ignore = !isExported(f.Name) || tag == "ignore"
field.packed = tag == "packed"
info.fields = append(info.fields, field)
//deep regist if field is a struct
if _t, ok, _ := _structInfoMgr.deepStructType(f.Type, false); ok {
if err := _structInfoMgr.regist(_t); err != nil {
//fmt.Printf("binary: internal regist duplicate type %s\n", _t.String())
continue
}
}
}
return true
}
func (info *structInfo) field(i int) *fieldInfo {
if nil != info && i >= 0 && i < info.numField() {
return info.fields[i]
}
return nil
}
func (info *structInfo) numField() int {
if nil != info {
return len(info.fields)
}
return 0
}
//informatin of a struct field
type fieldInfo struct {
field reflect.StructField
ignore bool //if this field is ignored
packed bool //if this ints field encode as varint/uvarint
}
func (field *fieldInfo) Type(i int, t reflect.Type) reflect.Type {
if field != nil {
return field.field.Type
}
return t.Field(i).Type
}
func (field *fieldInfo) isValid(i int, t reflect.Type) bool {
if field != nil {
return !field.ignore
}
// NOTE:
// creating the StructField info for each field is costly
// use RegStruct((*someStruct)(nil)) to aboid this path
return validField(t.Field(i)) // slow way to access field info
}
func (field *fieldInfo) isPacked() bool {
return field != nil && field.packed
}
func queryStruct(t reflect.Type) *structInfo {
return _structInfoMgr.query(t)
}