-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_info_helper.go
112 lines (93 loc) · 3.12 KB
/
struct_info_helper.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package instruct
import (
"fmt"
"reflect"
"sync"
)
type buildContext struct {
usedValues map[string]map[string]bool
clone bool
skipMapTags bool
skipStructField bool
skipStructOption bool
}
func (d *buildContext) ValueUsed(operation string, name string) {
if d.usedValues == nil {
d.usedValues = map[string]map[string]bool{}
}
if _, ok := d.usedValues[operation]; !ok {
d.usedValues[operation] = map[string]bool{}
}
d.usedValues[operation][name] = true
}
func (d *buildContext) GetUsedValues(operation string) map[string]bool {
if d.usedValues == nil {
return nil
}
operationValues, ok := d.usedValues[operation]
if !ok {
return nil
}
return operationValues
}
// structInfoFindOptionsFieldStructField finds a struct option field inside the struct fields.
func structInfoFindOptionsFieldStructField[IT any, DC DecodeContext](ctx *buildContext, t reflect.Type, lvl level,
mapTags MapTags, options *DefaultOptions[IT, DC]) (*Tag, error) {
var tag *Tag
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if isOptionField(field) {
if tag != nil {
return nil, fmt.Errorf("only one StructOption is allowed per struct for field '%s'", lvl.StringPath())
}
var err error
tag, err = parseStructTag(ctx, field, lvl, mapTags, options)
if err != nil {
return nil, fmt.Errorf("error on field '%s': %w", lvl.StringPath(), err)
}
}
}
return tag, nil
}
// structInfoFindOptionsFieldMapTags finds a struct option field from the MapTags.
func structInfoFindOptionsFieldMapTags[IT any, DC DecodeContext](ctx *buildContext, t reflect.Type, lvl level,
mapTags MapTags, options *DefaultOptions[IT, DC]) (*Tag, error) {
if mapTags != nil {
if _, ok := mapTags.findStringPath(lvl.Append(StructOptionMapTag).Path()); ok {
return parseStructTag(ctx, reflect.StructField{Name: StructOptionMapTag},
lvl.Append(StructOptionMapTag), mapTags, options)
}
}
return nil, nil
}
// structInfoProvider abstracts a posssible cache of structInfo
type structInfoProvider[IT any, DC DecodeContext] interface {
provide(t reflect.Type, mapTags MapTags, options DefaultOptions[IT, DC]) (*structInfo, error)
remove(t reflect.Type)
}
// defaultStructInfoProvider is a structInfoProvider that never caches.
type defaultStructInfoProvider[IT any, DC DecodeContext] struct {
}
func (d defaultStructInfoProvider[IT, DC]) provide(t reflect.Type, mapTags MapTags, options DefaultOptions[IT, DC]) (*structInfo, error) {
return buildStructInfo(t, mapTags, options)
}
func (d defaultStructInfoProvider[IT, DC]) remove(t reflect.Type) {}
// cachedStructInfoProvider is a structInfoProvider that always caches.
type cachedStructInfoProvider[IT any, DC DecodeContext] struct {
cache sync.Map
}
func (d *cachedStructInfoProvider[IT, DC]) provide(t reflect.Type, mapTags MapTags, options DefaultOptions[IT, DC]) (*structInfo, error) {
csi, ok := d.cache.Load(t)
if ok {
return csi.(*structInfo), nil
}
si, err := buildStructInfo(t, mapTags, options)
if err != nil {
return nil, err
}
d.cache.Store(t, si)
return si, nil
}
func (d *cachedStructInfoProvider[IT, DC]) remove(t reflect.Type) {
d.cache.Delete(t)
}