-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfield.go
399 lines (341 loc) · 8.81 KB
/
field.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package restruct
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"sync"
"github.com/go-restruct/restruct/expr"
)
// ErrInvalidSize is returned when sizefrom is used on an invalid type.
var ErrInvalidSize = errors.New("size specified on fixed size type")
// ErrInvalidSizeOf is returned when sizefrom is used on an invalid type.
var ErrInvalidSizeOf = errors.New("sizeof specified on fixed size type")
// ErrInvalidSizeFrom is returned when sizefrom is used on an invalid type.
var ErrInvalidSizeFrom = errors.New("sizefrom specified on fixed size type")
// ErrInvalidBits is returned when bits is used on an invalid type.
var ErrInvalidBits = errors.New("bits specified on non-bitwise type")
// FieldFlags is a type for flags that can be applied to fields individually.
type FieldFlags uint64
const (
// VariantBoolFlag causes the true value of a boolean to be ~0 instead of
// just 1 (all bits are set.) This emulates the behavior of VARIANT_BOOL.
VariantBoolFlag FieldFlags = 1 << iota
// InvertedBoolFlag causes the true and false states of a boolean to be
// flipped in binary.
InvertedBoolFlag
// RootFlag is set when the field points to the root struct.
RootFlag
// ParentFlag is set when the field points to the parent struct.
ParentFlag
// DefaultFlag is set when the field is designated as a switch case default.
DefaultFlag
)
// Sizer is a type which has a defined size in binary. The SizeOf function
// returns how many bytes the type will consume in memory. This is used during
// encoding for allocation and therefore must equal the exact number of bytes
// the encoded form needs. You may use a pointer receiver even if the type is
// used by value.
type Sizer interface {
SizeOf() int
}
// BitSizer is an interface for types that need to specify their own size in
// bit-level granularity. It has the same effect as Sizer.
type BitSizer interface {
BitSize() int
}
// field represents a structure field, similar to reflect.StructField.
type field struct {
Name string
Index int
BinaryType reflect.Type
NativeType reflect.Type
Order binary.ByteOrder
SIndex int // Index of size field for a slice/string.
TIndex int // Index of target of sizeof field.
Skip int
Trivial bool
BitSize uint8
Flags FieldFlags
IsRoot bool
IsParent bool
IfExpr *expr.Program
SizeExpr *expr.Program
BitsExpr *expr.Program
InExpr *expr.Program
OutExpr *expr.Program
WhileExpr *expr.Program
SwitchExpr *expr.Program
CaseExpr *expr.Program
}
// fields represents a structure.
type fields []field
var fieldCache = map[reflect.Type][]field{}
var cacheMutex = sync.RWMutex{}
// Elem constructs a transient field representing an element of an array, slice,
// or pointer.
func (f *field) Elem() field {
// Special cases for string types, grumble grumble.
t := f.BinaryType
if t.Kind() == reflect.String {
t = reflect.TypeOf([]byte{})
}
dt := f.NativeType
if dt.Kind() == reflect.String {
dt = reflect.TypeOf([]byte{})
}
return field{
Name: "*" + f.Name,
Index: -1,
BinaryType: t.Elem(),
NativeType: dt.Elem(),
Order: f.Order,
TIndex: -1,
SIndex: -1,
Skip: 0,
Trivial: isTypeTrivial(t.Elem()),
}
}
// fieldFromType returns a field from a reflected type.
func fieldFromType(typ reflect.Type) field {
return field{
Index: -1,
BinaryType: typ,
NativeType: typ,
Order: nil,
TIndex: -1,
SIndex: -1,
Skip: 0,
Trivial: isTypeTrivial(typ),
}
}
func validBitType(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32,
reflect.Complex64, reflect.Complex128:
return true
default:
return false
}
}
func validSizeType(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Slice, reflect.String:
return true
default:
return false
}
}
func parseExpr(sources ...string) *expr.Program {
for _, s := range sources {
if s != "" {
return expr.ParseString(s)
}
}
return nil
}
// fieldsFromStruct returns a slice of fields for binary packing and unpacking.
func fieldsFromStruct(typ reflect.Type) (result fields) {
if typ.Kind() != reflect.Struct {
panic(fmt.Errorf("tried to get fields from non-struct type %s", typ.Kind().String()))
}
count := typ.NumField()
sizeOfMap := map[string]int{}
for i := 0; i < count; i++ {
val := typ.Field(i)
// Skip unexported names (except _)
if val.PkgPath != "" && val.Name != "_" {
continue
}
// Parse struct tag
opts := mustParseTag(val.Tag.Get("struct"))
if opts.Ignore {
continue
}
if opts.RootFlag {
result = append(result, field{
Name: val.Name,
Index: i,
Flags: RootFlag,
})
continue
}
if opts.ParentFlag {
result = append(result, field{
Name: val.Name,
Index: i,
Flags: ParentFlag,
})
continue
}
// Derive type
ftyp := val.Type
if opts.Type != nil {
ftyp = opts.Type
}
// SizeOf
sindex := -1
tindex := -1
if j, ok := sizeOfMap[val.Name]; ok {
if !validSizeType(val.Type) {
panic(ErrInvalidSizeOf)
}
sindex = j
result[sindex].TIndex = i
delete(sizeOfMap, val.Name)
} else if opts.SizeOf != "" {
sizeOfMap[opts.SizeOf] = i
}
// SizeFrom
if opts.SizeFrom != "" {
if !validSizeType(val.Type) {
panic(ErrInvalidSizeFrom)
}
for j := 0; j < i; j++ {
val := result[j]
if opts.SizeFrom == val.Name {
sindex = j
result[sindex].TIndex = i
}
}
if sindex == -1 {
panic(fmt.Errorf("couldn't find SizeFrom field %s", opts.SizeFrom))
}
}
// Expr
ifExpr := parseExpr(opts.IfExpr, val.Tag.Get("struct-if"))
sizeExpr := parseExpr(opts.SizeExpr, val.Tag.Get("struct-size"))
bitsExpr := parseExpr(opts.BitsExpr, val.Tag.Get("struct-bits"))
inExpr := parseExpr(opts.InExpr, val.Tag.Get("struct-in"))
outExpr := parseExpr(opts.OutExpr, val.Tag.Get("struct-out"))
whileExpr := parseExpr(opts.WhileExpr, val.Tag.Get("struct-while"))
switchExpr := parseExpr(opts.SwitchExpr, val.Tag.Get("struct-switch"))
caseExpr := parseExpr(opts.CaseExpr, val.Tag.Get("struct-case"))
if sizeExpr != nil && !validSizeType(val.Type) {
panic(ErrInvalidSize)
}
if bitsExpr != nil && !validBitType(ftyp) {
panic(ErrInvalidBits)
}
// Flags
flags := FieldFlags(0)
if opts.VariantBoolFlag {
flags |= VariantBoolFlag
}
if opts.InvertedBoolFlag {
flags |= InvertedBoolFlag
}
if opts.DefaultFlag {
flags |= DefaultFlag
}
result = append(result, field{
Name: val.Name,
Index: i,
BinaryType: ftyp,
NativeType: val.Type,
Order: opts.Order,
SIndex: sindex,
TIndex: tindex,
Skip: opts.Skip,
Trivial: isTypeTrivial(ftyp),
BitSize: opts.BitSize,
Flags: flags,
IfExpr: ifExpr,
SizeExpr: sizeExpr,
BitsExpr: bitsExpr,
InExpr: inExpr,
OutExpr: outExpr,
WhileExpr: whileExpr,
SwitchExpr: switchExpr,
CaseExpr: caseExpr,
})
}
for fieldName := range sizeOfMap {
panic(fmt.Errorf("couldn't find SizeOf field %s", fieldName))
}
return
}
func cachedFieldsFromStruct(typ reflect.Type) (result fields) {
cacheMutex.RLock()
result, ok := fieldCache[typ]
cacheMutex.RUnlock()
if ok {
return
}
result = fieldsFromStruct(typ)
cacheMutex.Lock()
fieldCache[typ] = result
cacheMutex.Unlock()
return
}
// isTypeTrivial determines if a given type is constant-size.
func isTypeTrivial(typ reflect.Type) bool {
if typ == nil {
return false
}
switch typ.Kind() {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128:
return true
case reflect.Array, reflect.Ptr:
return isTypeTrivial(typ.Elem())
case reflect.Struct:
for _, field := range cachedFieldsFromStruct(typ) {
if !isTypeTrivial(field.BinaryType) {
return false
}
}
return true
default:
return false
}
}
func (f *field) sizer(v reflect.Value) (Sizer, bool) {
if s, ok := v.Interface().(Sizer); ok {
return s, true
}
if !v.CanAddr() {
return nil, false
}
if s, ok := v.Addr().Interface().(Sizer); ok {
return s, true
}
return nil, false
}
func (f *field) bitSizer(v reflect.Value) (BitSizer, bool) {
if s, ok := v.Interface().(BitSizer); ok {
return s, true
}
if !v.CanAddr() {
return nil, false
}
if s, ok := v.Addr().Interface().(BitSizer); ok {
return s, true
}
return nil, false
}
func (f *field) bitSizeUsingInterface(val reflect.Value) (int, bool) {
if s, ok := f.bitSizer(val); ok {
return s.BitSize(), true
}
if s, ok := f.sizer(val); ok {
return s.SizeOf() * 8, true
}
return 0, false
}