-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
149 lines (130 loc) · 6 KB
/
options.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package inreq
import (
"net/http"
"reflect"
"github.com/rrgmc/instruct"
)
// WithTagName sets the tag name to check on structs. The default is "inreq".
func WithTagName(tagName string) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.TagName = tagName
})
}
// WithDefaultRequired sets whether the default for fields should be "required" or "not required"
func WithDefaultRequired(defaultRequired bool) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.DefaultRequired = defaultRequired
})
}
// WithSliceSplitSeparator sets the string to be used as separator on string-to-array conversion. Default is ",".
func WithSliceSplitSeparator(sep string) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultSharedOptionFunc(func(o *sharedDefaultOptions) {
o.sliceSplitSeparator = sep
})
}
// WithFieldNameMapper sets the field name mapper. Default one uses [strings.ToLower].
func WithFieldNameMapper(fieldNameMapper FieldNameMapper) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.FieldNameMapper = fieldNameMapper
})
}
// WithPathValue sets the function used to extract the path from the request.
func WithPathValue(pathValue PathValue) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultSharedOptionFunc(func(o *sharedDefaultOptions) {
o.pathValue = pathValue
})
}
// WithBodyDecoder sets the interface used to parse body data into structs.
func WithBodyDecoder(bodyDecoder BodyDecoder) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultSharedOptionFunc(func(o *sharedDefaultOptions) {
o.bodyDecoder = bodyDecoder
})
}
// WithDefaultDecodeOperations adds the default operations (query, path, header, form and body).
// If the non-"Custom" calls are used, this option is added by default.
func WithDefaultDecodeOperations() DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.DecodeOperations[OperationQuery] = &DecodeOperationQuery{}
o.DecodeOperations[OperationPath] = &DecodeOperationPath{}
o.DecodeOperations[OperationHeader] = &DecodeOperationHeader{}
o.DecodeOperations[OperationForm] = &DecodeOperationForm{}
o.DecodeOperations[OperationBody] = &DecodeOperationBody{}
})
}
// WithDecodeOperation adds a decode operation.
func WithDecodeOperation(name string, operation DecodeOperation) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
if operation == nil {
delete(o.DecodeOperations, name)
} else {
o.DecodeOperations[name] = operation
}
})
}
// WithResolver sets the decode Resolver.
func WithResolver(resolver Resolver) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.Resolver = resolver
})
}
// WithDefaultMapTags adds a "default" MapTags. The default one is checked alongside the tags, so the
// check for unused fields takes both in account. Passing a struct without any struct tags and using
// WithMapTags will result in "field configuration not found" errors (except in free-standing functions like
// Decode, CustomDecode, DecodeType and CustomDecodeType.
func WithDefaultMapTags(dataForType any, tags MapTags) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.DefaultMapTagsSet(reflect.TypeOf(dataForType), tags)
})
}
// WithDefaultMapTagsType is the same as WithDefaultMapTags using a reflect.Type.
func WithDefaultMapTagsType(typ reflect.Type, tags MapTags) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.DefaultMapTagsSet(typ, tags)
})
}
// WithStructInfoCache sets whether to cache info for structs on parse. Default is false.
func WithStructInfoCache(cache bool) DefaultAndTypeDefaultOption {
return defaultAndTypeDefaultOptionFunc(func(o *instruct.DefaultOptions[*http.Request, DecodeContext]) {
o.StructInfoCache(cache)
})
}
// WithAllowReadBody sets whether operations are allowed to read the request body. Default is false.
func WithAllowReadBody(allowReadBody bool) FullOption {
return fullSharedOptionFunc(func(o *sharedDefaultOptions) {
o.defaultDecodeOptions.allowReadBody = allowReadBody
}, func(o *decodeOptions) {
o.allowReadBody = allowReadBody
})
}
// WithEnsureAllQueryUsed sets whether to check if all query parameters were used.
func WithEnsureAllQueryUsed(ensureAllQueryUsed bool) FullOption {
return fullSharedOptionFunc(func(o *sharedDefaultOptions) {
o.defaultDecodeOptions.ensureAllQueryUsed = ensureAllQueryUsed
}, func(o *decodeOptions) {
o.ensureAllQueryUsed = ensureAllQueryUsed
})
}
// WithEnsureAllFormUsed sets whether to check if all form parameters were used.
func WithEnsureAllFormUsed(ensureAllFormUsed bool) FullOption {
return fullSharedOptionFunc(func(o *sharedDefaultOptions) {
o.defaultDecodeOptions.ensureAllFormUsed = ensureAllFormUsed
}, func(o *decodeOptions) {
o.ensureAllFormUsed = ensureAllFormUsed
})
}
// WithMapTags sets decode-operation-specific MapTags. These override the default cached struct information
// but don't change the original one. This should be used to override configurations on each call.
func WithMapTags(tags MapTags) TypeDefaultAndDecodeOption {
return typeAndDecodeOptionFunc(func(o *typeDefaultOptions) {
o.options.MapTags = tags
}, func(o *decodeOptions) {
o.options.MapTags = tags
})
}
// withUseDecodeMapTagsAsDefault is an internal option to allow WithMapTags to set default map tags for
// free-standing Decode functions.
func withUseDecodeMapTagsAsDefault(useDecodeMapTagsAsDefault bool) DecodeOption {
return decodeOptionFunc(func(o *decodeOptions) {
o.options.UseDecodeMapTagsAsDefault = useDecodeMapTagsAsDefault
})
}