-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_cache.go
More file actions
57 lines (46 loc) · 1.02 KB
/
metadata_cache.go
File metadata and controls
57 lines (46 loc) · 1.02 KB
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
package rigging
import (
"reflect"
"sync"
)
type structFieldMeta struct {
index int
field reflect.StructField
tagCfg tagConfig
}
var structFieldMetaCache sync.Map
func getStructFieldMeta(t reflect.Type) []structFieldMeta {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil
}
if cached, ok := structFieldMetaCache.Load(t); ok {
if fields, ok := cached.([]structFieldMeta); ok {
return cloneStructFieldMeta(fields)
}
}
fields := make([]structFieldMeta, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
fields = append(fields, structFieldMeta{
index: i,
field: field,
tagCfg: parseTag(field.Tag.Get("conf")),
})
}
structFieldMetaCache.Store(t, fields)
return cloneStructFieldMeta(fields)
}
func cloneStructFieldMeta(fields []structFieldMeta) []structFieldMeta {
if len(fields) == 0 {
return nil
}
cloned := make([]structFieldMeta, len(fields))
copy(cloned, fields)
return cloned
}