-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
79 lines (66 loc) · 2.99 KB
/
option.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
package instruct
import (
"reflect"
"strings"
"github.com/rrgmc/instruct/resolver"
)
// FieldNameMapper maps a struct field name to the target field name.
// The default one uses [strings.ToLower].
type FieldNameMapper func(operation string, name string) string
type DefaultOptions[IT any, DC DecodeContext] struct {
TagName string // struct tag name. Default "inreq".
DefaultRequired bool // whether the default for fields should be "required" or "not required"
DecodeOperations map[string]DecodeOperation[IT, DC] // list of decode operations
defaultMapTags *mapTagsList // list of DEFAULT map tags
FieldNameMapper FieldNameMapper // field name mapper. Default one uses [strings.ToLower].
structInfoProvider structInfoProvider[IT, DC] // allows caching of structInfo
Resolver Resolver // interface used to convert strings to the struct field type.
}
func (o *DefaultOptions[IT, DC]) DefaultMapTagsSet(t reflect.Type, m MapTags) {
t = reflectElem(t)
o.defaultMapTags.Set(t, m)
o.structInfoProvider.remove(t)
}
func (o *DefaultOptions[IT, DC]) StructInfoCache(cache bool) {
if cache {
o.structInfoProvider = &cachedStructInfoProvider[IT, DC]{}
} else {
o.structInfoProvider = &defaultStructInfoProvider[IT, DC]{}
}
}
type DecodeOptions[IT any, DC DecodeContext] struct {
Ctx DC // decode context to be sent to DecodeOperation.
MapTags MapTags // decode call-specific MapTags. They may override existing ones.
UseDecodeMapTagsAsDefault bool // internal flag to allow Decode functions without an instance to set MapTags as a default one.
}
type TypeDefaultOptions[IT any, DC DecodeContext] struct {
DefaultOptions[IT, DC]
MapTags MapTags // type-specific MapTags. They may override existing ones.
}
// NewDefaultOptions returns a DefaultOptions with the default values.
func NewDefaultOptions[IT any, DC DecodeContext]() DefaultOptions[IT, DC] {
return DefaultOptions[IT, DC]{
TagName: "instruct",
DefaultRequired: true,
DecodeOperations: map[string]DecodeOperation[IT, DC]{},
defaultMapTags: &mapTagsList{},
FieldNameMapper: DefaultFieldNameMapper,
structInfoProvider: defaultStructInfoProvider[IT, DC]{},
Resolver: resolver.NewResolver(),
}
}
// NewDecodeOptions returns a DecodeOptions with the default values.
func NewDecodeOptions[IT any, DC DecodeContext]() DecodeOptions[IT, DC] {
return DecodeOptions[IT, DC]{}
}
// NewTypeDefaultOptions returns a TypeDefaultOptions with the default values.
func NewTypeDefaultOptions[IT any, DC DecodeContext]() TypeDefaultOptions[IT, DC] {
return TypeDefaultOptions[IT, DC]{
DefaultOptions: NewDefaultOptions[IT, DC](),
}
}
// helpers
// DefaultFieldNameMapper converts names to lowercase using [strings.ToLower].
func DefaultFieldNameMapper(operation string, name string) string {
return strings.ToLower(name)
}