-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproperties.go
222 lines (206 loc) · 6.42 KB
/
properties.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
package cstruct
import (
"reflect"
"sync"
)
// StructProperties
type StructProperties struct {
Prop []*Properties
fixedSize int
}
var (
propertiesMu sync.RWMutex
propertiesMap = make(map[reflect.Type]*StructProperties)
)
func GetProperties(t reflect.Type) *StructProperties {
if t.Kind() != reflect.Struct {
panic("cstruct: type must have kind struct")
}
propertiesMu.RLock()
sprop, ok := propertiesMap[t]
propertiesMu.RUnlock()
if ok {
return sprop
}
propertiesMu.Lock()
defer propertiesMu.Unlock()
sprop = getPropertiesLocked(t)
return sprop
}
func getPropertiesLocked(t reflect.Type) *StructProperties {
if prop, ok := propertiesMap[t]; ok {
return prop
}
prop := new(StructProperties)
propertiesMap[t] = prop
prop.Prop = make([]*Properties, t.NumField())
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
p := new(Properties)
name := f.Name
p.init(f.Type, name, "", &f, &prop.fixedSize)
prop.Prop[i] = p
}
return prop
}
// Properties
type encoder func(p *Buffer, prop *Properties, base structPointer) error
type decoder func(p *Buffer, prop *Properties, base structPointer) error
type sizer func(p *Buffer, prop *Properties, base structPointer) int
type Properties struct {
Name string
field field
enc encoder
dec decoder
siz sizer
t reflect.Type
stype reflect.Type
sprop *StructProperties
}
func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, fixedSize *int) {
p.Name = name
if f != nil {
p.field = toField(f)
}
p.setEncAndDec(typ, f, fixedSize)
}
func (p *Properties) setEncAndDec(typ reflect.Type, f *reflect.StructField, fixedSize *int) {
p.enc = nil
p.dec = nil
p.siz = nil
p.t = typ
switch typ.Kind() {
case reflect.Bool: // bool
p.enc = (*Buffer).enc_bool
p.dec = (*Buffer).dec_bool
*fixedSize += 1
case reflect.Int8, reflect.Uint8: // int8 uint8
p.enc = (*Buffer).enc_uint8
p.dec = (*Buffer).dec_uint8
*fixedSize += 1
case reflect.Int16, reflect.Uint16: // int16 uint16
p.enc = (*Buffer).enc_uint16
p.dec = (*Buffer).dec_uint16
*fixedSize += 2
case reflect.Int32, reflect.Uint32, reflect.Float32: // int32 uint32 float32
p.enc = (*Buffer).enc_uint32
p.dec = (*Buffer).dec_uint32
*fixedSize += 4
case reflect.Int64, reflect.Uint64, reflect.Float64: // int64 uint64 float64
p.enc = (*Buffer).enc_uint64
p.dec = (*Buffer).dec_uint64
*fixedSize += 8
case reflect.String: // string
p.enc = (*Buffer).enc_string
p.dec = (*Buffer).dec_string
p.siz = (*Buffer).size_string
*fixedSize += 2
case reflect.Ptr: // struct ptr
if t2 := typ.Elem(); t2.Kind() == reflect.Struct {
p.stype = t2
p.sprop = getPropertiesLocked(p.stype)
p.enc = (*Buffer).enc_substruct_ptr
p.dec = (*Buffer).dec_substruct_ptr
p.siz = (*Buffer).size_substruct_ptr
} else {
panic("cstruct: unknow type. field name =" + f.Name)
}
case reflect.Struct: // struct
p.stype = typ
p.sprop = getPropertiesLocked(p.stype)
p.enc = (*Buffer).enc_substruct
p.dec = (*Buffer).dec_substruct
p.siz = (*Buffer).size_substruct
case reflect.Slice:
*fixedSize += 2
switch t2 := typ.Elem(); t2.Kind() {
case reflect.Uint8, reflect.Int8: // []byte []uint8 []int8
p.enc = (*Buffer).enc_slice_byte
p.dec = (*Buffer).dec_slice_byte
p.siz = (*Buffer).size_slice_byte
case reflect.Bool: // []bool
p.enc = (*Buffer).enc_slice_bool
p.dec = (*Buffer).dec_slice_bool
p.siz = (*Buffer).size_slice_bool
case reflect.Uint16, reflect.Int16: // []uint16 []int16
p.enc = (*Buffer).enc_slice_uint16
p.dec = (*Buffer).dec_slice_uint16
p.siz = (*Buffer).size_slice_uint16
case reflect.Uint32, reflect.Int32, reflect.Float32: // []uint32 []int32 []float32
p.enc = (*Buffer).enc_slice_uint32
p.dec = (*Buffer).dec_slice_uint32
p.siz = (*Buffer).size_slice_uint32
case reflect.Uint64, reflect.Int64, reflect.Float64: // []uint64 []int64 []float64
p.enc = (*Buffer).enc_slice_uint64
p.dec = (*Buffer).dec_slice_uint64
p.siz = (*Buffer).size_slice_uint64
case reflect.String: // []string
p.enc = (*Buffer).enc_slice_string
p.dec = (*Buffer).dec_slice_string
p.siz = (*Buffer).size_slice_string
case reflect.Struct: // [] struct
p.stype = t2
p.sprop = getPropertiesLocked(p.stype)
p.enc = (*Buffer).enc_slice_substruct
p.dec = (*Buffer).dec_slice_substruct
p.siz = (*Buffer).size_slice_substruct
case reflect.Ptr: // []*struct
switch t3 := t2.Elem(); t3.Kind() {
case reflect.Struct:
p.stype = t3
p.sprop = getPropertiesLocked(p.stype)
if OptionSliceIgnoreNil == false {
p.enc = (*Buffer).enc_slice_substruct_ptr
p.dec = (*Buffer).dec_slice_substruct_ptr
p.siz = (*Buffer).size_slice_substruct_ptr
} else {
p.enc = (*Buffer).enc_slice_substruct_ptr_ignore_nil
p.dec = (*Buffer).dec_slice_substruct_ptr_ignore_nil
p.siz = (*Buffer).size_slice_substruct_ptr_ignore_nil
}
default:
panic("cstruct: unknow type. field name = " + f.Name)
}
case reflect.Slice:
switch t2.Elem().Kind() {
case reflect.Uint8:
p.enc = (*Buffer).enc_slice_slice_byte
p.dec = (*Buffer).dec_slice_slice_byte
p.siz = (*Buffer).size_slice_slice_byte
default:
panic("cstruct: unknow type. field name = " + f.Name)
}
default:
panic("cstruct: unknow type. field name = " + f.Name)
}
case reflect.Array:
switch t2 := typ.Elem(); t2.Kind() {
case reflect.Uint8, reflect.Int8, reflect.Bool: // [n]byte [n]uint8 [n]int8 [n]bool
p.enc = (*Buffer).enc_array_byte
p.dec = (*Buffer).dec_array_byte
p.siz = (*Buffer).size_array_byte
case reflect.Uint16, reflect.Int16: // [n]uint16 [n]int16
p.enc = (*Buffer).enc_array_uint16
p.dec = (*Buffer).dec_array_uint16
p.siz = (*Buffer).size_array_uint16
case reflect.Uint32, reflect.Int32, reflect.Float32: // [n]uint32 [n]int32 [n]float32
p.enc = (*Buffer).enc_array_uint32
p.dec = (*Buffer).dec_array_uint32
p.siz = (*Buffer).size_array_uint32
case reflect.Uint64, reflect.Int64, reflect.Float64: // [n]uint64 [n]int64 [n]float64
p.enc = (*Buffer).enc_array_uint64
p.dec = (*Buffer).dec_array_uint64
p.siz = (*Buffer).size_array_uint64
case reflect.Struct: // [n]struct
p.stype = t2
p.sprop = getPropertiesLocked(p.stype)
p.enc = (*Buffer).enc_array_substruct
p.dec = (*Buffer).dec_array_substruct
p.siz = (*Buffer).size_array_substruct
default:
panic("cstruct: unknow type. field name = " + f.Name)
}
default:
panic("cstruct: unknow type. field name = " + f.Name)
}
}