-
Notifications
You must be signed in to change notification settings - Fork 665
/
struct_fielder.go
96 lines (80 loc) · 2.73 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
96
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package reflectcodec
import (
"fmt"
"reflect"
"sync"
"github.com/ava-labs/avalanchego/codec"
)
// TagValue is the value the tag must have to be serialized.
const TagValue = "true"
var _ StructFielder = (*structFielder)(nil)
// 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.
// 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) ([]int, error)
}
func NewStructFielder(tagNames []string) StructFielder {
return &structFielder{
tags: tagNames,
serializedFieldIndices: make(map[reflect.Type][]int),
}
}
type structFielder struct {
lock sync.RWMutex
// multiple tags per field can be specified. A field is serialized/deserialized
// if it has at least one of the specified tags.
tags []string
// 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][]int
}
func (s *structFielder) GetSerializedFields(t reflect.Type) ([]int, error) {
if serializedFields, ok := s.getCachedSerializedFields(t); ok { // use pre-computed result
return serializedFields, nil
}
s.lock.Lock()
defer s.lock.Unlock()
numFields := t.NumField()
serializedFields := make([]int, 0, numFields)
for i := 0; i < numFields; i++ { // Go through all fields of this struct
field := t.Field(i)
// Multiple tags per fields can be specified.
// Serialize/Deserialize field if it has
// any tag with the right value
var captureField bool
for _, tag := range s.tags {
if field.Tag.Get(tag) == TagValue {
captureField = true
break
}
}
if !captureField {
continue
}
if !field.IsExported() { // Can only marshal exported fields
return nil, fmt.Errorf("can not marshal %w: %s",
codec.ErrUnexportedField,
field.Name,
)
}
serializedFields = append(serializedFields, i)
}
s.serializedFieldIndices[t] = serializedFields // cache result
return serializedFields, nil
}
func (s *structFielder) getCachedSerializedFields(t reflect.Type) ([]int, bool) {
s.lock.RLock()
defer s.lock.RUnlock()
cachedFields, ok := s.serializedFieldIndices[t]
return cachedFields, ok
}