/
factory.go
283 lines (228 loc) · 7.82 KB
/
factory.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package request
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"reflect"
"sort"
"strings"
"github.com/swaggest/form/v5"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/refl"
"github.com/swaggest/rest"
"github.com/swaggest/rest/nethttp"
)
var _ DecoderMaker = &DecoderFactory{}
const (
defaultTag = "default"
jsonTag = "json"
fileTag = "file"
formDataTag = "formData"
)
// DecoderFactory decodes http requests.
//
// Please use NewDecoderFactory to create instance.
type DecoderFactory struct {
// ApplyDefaults enables default value assignment for fields missing explicit value in request.
// Default value is retrieved from `default` field tag.
ApplyDefaults bool
// JSONReader allows custom JSON decoder for request body.
// If not set encoding/json.Decoder is used.
JSONReader func(rd io.Reader, v interface{}) error
formDecoders map[rest.ParamIn]*form.Decoder
decoderFunctions map[rest.ParamIn]decoderFunc
defaultValDecoder *form.Decoder
customDecoders []customDecoder
}
type customDecoder struct {
types []interface{}
fn form.DecodeFunc
}
// NewDecoderFactory creates request decoder factory.
func NewDecoderFactory() *DecoderFactory {
df := DecoderFactory{}
df.SetDecoderFunc(rest.ParamInCookie, cookiesToURLValues)
df.SetDecoderFunc(rest.ParamInFormData, formDataToURLValues)
df.SetDecoderFunc(rest.ParamInHeader, headerToURLValues)
df.SetDecoderFunc(rest.ParamInQuery, queryToURLValues)
defaultValDecoder := form.NewDecoder()
defaultValDecoder.RegisterTagNameFunc(func(field reflect.StructField) string {
return field.Name
})
df.defaultValDecoder = defaultValDecoder
return &df
}
// SetDecoderFunc adds custom decoder function for values of particular field tag name.
func (df *DecoderFactory) SetDecoderFunc(tagName rest.ParamIn, d func(r *http.Request) (url.Values, error)) {
if df.decoderFunctions == nil {
df.decoderFunctions = make(map[rest.ParamIn]decoderFunc)
}
if df.formDecoders == nil {
df.formDecoders = make(map[rest.ParamIn]*form.Decoder)
}
df.decoderFunctions[tagName] = d
dec := form.NewDecoder()
dec.SetTagName(string(tagName))
dec.SetMode(form.ModeExplicit)
df.formDecoders[tagName] = dec
}
// MakeDecoder creates request.RequestDecoder for a http method and request structure.
//
// Input is checked for `json`, `file` tags only for methods with body semantics (POST, PUT, PATCH) or
// if input implements openapi3.RequestBodyEnforcer.
//
// CustomMapping can be nil, otherwise it is used instead of field tags to match decoded fields with struct.
func (df *DecoderFactory) MakeDecoder(
method string,
input interface{},
customMapping rest.RequestMapping,
) nethttp.RequestDecoder {
m := decoder{
decoders: make([]valueDecoderFunc, 0),
in: make([]rest.ParamIn, 0),
}
if df.ApplyDefaults && refl.HasTaggedFields(input, defaultTag) {
df.makeDefaultDecoder(input, &m)
}
cm := df.prepareCustomMapping(input, customMapping)
if len(cm) > 0 {
df.makeCustomMappingDecoder(cm, &m)
}
for in, formDecoder := range df.formDecoders {
if _, exists := cm[in]; exists {
continue
}
if refl.HasTaggedFields(input, string(in)) {
df.jsonParams(formDecoder, in, input)
m.decoders = append(m.decoders, makeDecoder(in, formDecoder, df.decoderFunctions[in]))
m.in = append(m.in, in)
}
}
method = strings.ToUpper(method)
_, forceRequestBody := input.(openapi3.RequestBodyEnforcer)
if method != http.MethodPost && method != http.MethodPut && method != http.MethodPatch && !forceRequestBody {
return &m
}
// Checking for body tags.
if refl.HasTaggedFields(input, jsonTag) || refl.FindEmbeddedSliceOrMap(input) != nil {
if df.JSONReader != nil {
m.decoders = append(m.decoders, decodeJSONBody(df.JSONReader))
} else {
m.decoders = append(m.decoders, decodeJSONBody(readJSON))
}
m.in = append(m.in, rest.ParamInBody)
}
if hasFileFields(input, fileTag) || hasFileFields(input, formDataTag) {
m.decoders = append(m.decoders, decodeFiles)
m.in = append(m.in, rest.ParamInFormData)
}
return &m
}
func (df *DecoderFactory) prepareCustomMapping(input interface{}, customMapping rest.RequestMapping) rest.RequestMapping {
// Copy custom mapping to avoid mutability issues on original map.
cm := make(rest.RequestMapping, len(customMapping))
for k, v := range customMapping {
cm[k] = v
}
// Move header names to custom mapping and/or apply canonical form to match net/http request decoder.
if hdm, exists := cm[rest.ParamInHeader]; !exists && refl.HasTaggedFields(input, string(rest.ParamInHeader)) {
hdm = make(map[string]string)
refl.WalkTaggedFields(reflect.ValueOf(input), func(v reflect.Value, sf reflect.StructField, tag string) {
hdm[sf.Name] = http.CanonicalHeaderKey(tag)
}, string(rest.ParamInHeader))
cm[rest.ParamInHeader] = hdm
} else if exists {
for k, v := range hdm {
hdm[k] = http.CanonicalHeaderKey(v)
}
}
fields := make(map[string]bool)
refl.WalkTaggedFields(reflect.ValueOf(input), func(v reflect.Value, sf reflect.StructField, tag string) {
fields[sf.Name] = true
}, "")
// Check if there are non-existent fields in mapping.
var nonExistent []string
for _, items := range cm {
for k := range items {
if _, exists := fields[k]; !exists {
nonExistent = append(nonExistent, k)
}
}
}
if len(nonExistent) > 0 {
sort.Strings(nonExistent)
panic("non existent fields in mapping: " + strings.Join(nonExistent, ", "))
}
return cm
}
// jsonParams configures custom decoding for parameters with JSON struct values.
func (df *DecoderFactory) jsonParams(formDecoder *form.Decoder, in rest.ParamIn, input interface{}) {
// Check fields for struct values with json tags. E.g. query parameter with json value.
refl.WalkTaggedFields(reflect.ValueOf(input), func(v reflect.Value, sf reflect.StructField, tag string) {
fieldVal := v.Interface()
if refl.HasTaggedFields(fieldVal, jsonTag) {
// If value is a struct with `json` tags, custom decoder unmarshals json
// from a string value into a struct.
formDecoder.RegisterFunc(func(s string) (interface{}, error) {
var err error
f := reflect.New(sf.Type)
if df.JSONReader != nil {
err = df.JSONReader(bytes.NewBufferString(s), f.Interface())
} else {
err = json.Unmarshal([]byte(s), f.Interface())
}
if err != nil {
return nil, err
}
return reflect.Indirect(f).Interface(), nil
}, fieldVal)
}
}, string(in))
}
func (df *DecoderFactory) makeDefaultDecoder(input interface{}, m *decoder) {
defaults := url.Values{}
refl.WalkTaggedFields(reflect.ValueOf(input), func(v reflect.Value, sf reflect.StructField, tag string) {
defaults[sf.Name] = []string{tag}
}, defaultTag)
dec := df.defaultValDecoder
m.decoders = append(m.decoders, func(r *http.Request, v interface{}, validator rest.Validator) error {
return dec.Decode(v, defaults)
})
m.in = append(m.in, defaultTag)
}
func (df *DecoderFactory) makeCustomMappingDecoder(customMapping rest.RequestMapping, m *decoder) {
for in, mapping := range customMapping {
dec := form.NewDecoder()
dec.SetTagName(string(in))
// Copy mapping to avoid mutability.
mm := make(map[string]string, len(mapping))
for k, v := range mapping {
mm[k] = v
}
dec.RegisterTagNameFunc(func(field reflect.StructField) string {
n := mm[field.Name]
if n == "" && !field.Anonymous {
return "-"
}
return n
})
for _, c := range df.customDecoders {
dec.RegisterFunc(c.fn, c.types...)
}
m.decoders = append(m.decoders, makeDecoder(in, dec, df.decoderFunctions[in]))
m.in = append(m.in, in)
}
}
// RegisterFunc adds custom type handling.
func (df *DecoderFactory) RegisterFunc(fn form.DecodeFunc, types ...interface{}) {
for _, fd := range df.formDecoders {
fd.RegisterFunc(fn, types...)
}
df.defaultValDecoder.RegisterFunc(fn, types...)
df.customDecoders = append(df.customDecoders, customDecoder{
fn: fn,
types: types,
})
}