-
Notifications
You must be signed in to change notification settings - Fork 670
/
struct_fielder.go
95 lines (82 loc) · 2.88 KB
/
struct_fielder.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package reflectcodec
import (
"fmt"
"reflect"
"strconv"
"sync"
"unicode"
)
const (
// SliceLenTagName that specifies the length of a slice.
SliceLenTagName = "len"
// TagValue is the value the tag must have to be serialized.
TagValue = "true"
)
var _ StructFielder = &structFielder{}
type FieldDesc struct {
Index int
MaxSliceLen uint32
}
// StructFielder handles discovery of serializable fields in a struct.
type StructFielder interface {
// Returns the fields that have been marked as serializable in [t], which is
// a struct type. Additionally, returns the custom maximum length slice that
// may be serialized into the field, if any.
// Returns an error if a field has tag "[tagName]: [TagValue]" but the field
// is un-exported.
// GetSerializedField(Foo) --> [1,5,8] means Foo.Field(1), Foo.Field(5),
// Foo.Field(8) are to be serialized/deserialized.
GetSerializedFields(t reflect.Type) ([]FieldDesc, error)
}
func NewStructFielder(tagName string, maxSliceLen uint32) StructFielder {
return &structFielder{
tagName: tagName,
maxSliceLen: maxSliceLen,
serializedFieldIndices: make(map[reflect.Type][]FieldDesc),
}
}
type structFielder struct {
lock sync.Mutex
tagName string
maxSliceLen uint32
// Key: a struct type
// Value: Slice where each element is index in the struct type of a field
// that is serialized/deserialized e.g. Foo --> [1,5,8] means Foo.Field(1),
// etc. are to be serialized/deserialized. We assume this cache is pretty
// small (a few hundred keys at most) and doesn't take up much memory.
serializedFieldIndices map[reflect.Type][]FieldDesc
}
func (s *structFielder) GetSerializedFields(t reflect.Type) ([]FieldDesc, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.serializedFieldIndices == nil {
s.serializedFieldIndices = make(map[reflect.Type][]FieldDesc)
}
if serializedFields, ok := s.serializedFieldIndices[t]; ok { // use pre-computed result
return serializedFields, nil
}
numFields := t.NumField()
serializedFields := make([]FieldDesc, 0, numFields)
for i := 0; i < numFields; i++ { // Go through all fields of this struct
field := t.Field(i)
if field.Tag.Get(s.tagName) != TagValue { // Skip fields we don't need to serialize
continue
}
if unicode.IsLower(rune(field.Name[0])) { // Can only marshal exported fields
return nil, fmt.Errorf("can't marshal un-exported field %s", field.Name)
}
sliceLenField := field.Tag.Get(SliceLenTagName)
maxSliceLen := s.maxSliceLen
if newLen, err := strconv.ParseUint(sliceLenField, 10, 31); err == nil {
maxSliceLen = uint32(newLen)
}
serializedFields = append(serializedFields, FieldDesc{
Index: i,
MaxSliceLen: maxSliceLen,
})
}
s.serializedFieldIndices[t] = serializedFields // cache result
return serializedFields, nil
}