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
|
package attributevalue
import (
"reflect"
"strings"
"sync"
)
var fieldCache = &fieldCacher{}
type fieldCacheKey struct {
typ reflect.Type
opts structFieldOptions
}
type fieldCacher struct {
cache sync.Map
}
func (c *fieldCacher) Load(key fieldCacheKey) (*cachedFields, bool) {
if v, ok := c.cache.Load(key); ok {
return v.(*cachedFields), true
}
return nil, false
}
func (c *fieldCacher) LoadOrStore(key fieldCacheKey, fs *cachedFields) (*cachedFields, bool) {
v, ok := c.cache.LoadOrStore(key, fs)
return v.(*cachedFields), ok
}
type cachedFields struct {
fields []field
fieldsByName map[string]int
}
func (f *cachedFields) All() []field {
return f.fields
}
func (f *cachedFields) FieldByName(name string) (field, bool) {
if i, ok := f.fieldsByName[name]; ok {
return f.fields[i], ok
}
for _, f := range f.fields {
if strings.EqualFold(f.Name, name) {
return f, true
}
}
return field{}, false
}
|