-
Notifications
You must be signed in to change notification settings - Fork 22
/
cache.go
45 lines (37 loc) · 854 Bytes
/
cache.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
package aper
import (
"fmt"
"reflect"
"sync"
)
var (
fieldParametersCache sync.Map
structFieldCache structFieldCacheT
)
type structFieldCacheT struct {
m sync.Map
}
func (f *structFieldCacheT) load(k reflect.Type) ([]structFieldElem, error) {
v, ok := f.m.Load(k)
if ok {
return v.([]structFieldElem), nil
}
numField := k.NumField()
result := make([]structFieldElem, 0, numField)
for i := 0; i < numField; i++ {
field := k.Field(i)
if field.PkgPath != "" {
return nil, fmt.Errorf("struct contains unexported fields : " + field.PkgPath)
}
result = append(result, structFieldElem{
FieldName: field.Name,
FieldParameters: parseFieldParameters(field.Tag.Get("aper")),
})
}
f.m.Store(k, result)
return result, nil
}
type structFieldElem struct {
FieldName string
FieldParameters fieldParameters
}