/
bind.go
420 lines (392 loc) · 10.8 KB
/
bind.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package binding
import (
jsonpkg "encoding/json"
"net/http"
"reflect"
"strings"
"sync"
"github.com/andeya/ameda"
"github.com/andeya/goutil"
"github.com/bytedance/go-tagexpr/v2"
"github.com/bytedance/go-tagexpr/v2/validator"
)
// Binding binding and verification tool for http request
type Binding struct {
vd *validator.Validator
recvs map[uintptr]*receiver
lock sync.RWMutex
bindErrFactory func(failField, msg string) error
config Config
jsonUnmarshalFunc func(data []byte, v interface{}) error
}
// New creates a binding tool.
// NOTE:
//
// Use default tag name for config fields that are empty
func New(config *Config) *Binding {
if config == nil {
config = new(Config)
}
b := &Binding{
recvs: make(map[uintptr]*receiver, 1024),
config: *config,
}
b.config.init()
b.vd = validator.New(b.config.Validator)
return b.SetErrorFactory(nil, nil)
}
// SetLooseZeroMode if set to true,
// the empty string request parameter is bound to the zero value of parameter.
// NOTE:
//
// The default is false;
// Suitable for these parameter types: query/header/cookie/form .
func (b *Binding) SetLooseZeroMode(enable bool) *Binding {
b.config.LooseZeroMode = enable
for k := range b.recvs {
delete(b.recvs, k)
}
return b
}
var defaultValidatingErrFactory = newDefaultErrorFactory("validating")
var defaultBindErrFactory = newDefaultErrorFactory("binding")
// SetErrorFactory customizes the factory of validation error.
// NOTE:
//
// If errFactory==nil, the default is used
func (b *Binding) SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) *Binding {
if bindErrFactory == nil {
bindErrFactory = defaultBindErrFactory
}
if validatingErrFactory == nil {
validatingErrFactory = defaultValidatingErrFactory
}
b.bindErrFactory = bindErrFactory
b.vd.SetErrorFactory(validatingErrFactory)
return b
}
// BindAndValidate binds the request parameters and validates them if needed.
func (b *Binding) BindAndValidate(recvPointer interface{}, req *http.Request, pathParams PathParams) error {
return b.IBindAndValidate(recvPointer, wrapRequest(req), pathParams)
}
// Bind binds the request parameters.
func (b *Binding) Bind(recvPointer interface{}, req *http.Request, pathParams PathParams) error {
return b.IBind(recvPointer, wrapRequest(req), pathParams)
}
// IBindAndValidate binds the request parameters and validates them if needed.
func (b *Binding) IBindAndValidate(recvPointer interface{}, req Request, pathParams PathParams) error {
v, hasVd, err := b.bind(recvPointer, req, pathParams)
if err != nil {
return err
}
if hasVd {
return b.vd.Validate(v)
}
return nil
}
// IBind binds the request parameters.
func (b *Binding) IBind(recvPointer interface{}, req Request, pathParams PathParams) error {
_, _, err := b.bind(recvPointer, req, pathParams)
return err
}
// Validate validates whether the fields of value is valid.
func (b *Binding) Validate(value interface{}) error {
return b.vd.Validate(value)
}
func (b *Binding) bind(pointer interface{}, req Request, pathParams PathParams) (elemValue reflect.Value, hasVd bool, err error) {
elemValue, err = b.receiverValueOf(pointer)
if err != nil {
return
}
if elemValue.Kind() == reflect.Struct {
hasVd, err = b.bindStruct(pointer, elemValue, req, pathParams)
} else {
hasVd, err = b.bindNonstruct(pointer, elemValue, req, pathParams)
}
return
}
func (b *Binding) bindNonstruct(pointer interface{}, _ reflect.Value, req Request, _ PathParams) (hasVd bool, err error) {
bodyCodec := getBodyCodec(req)
switch bodyCodec {
case bodyJSON:
hasVd = true
bodyBytes, err := req.GetBody()
if err != nil {
return hasVd, err
}
err = b.bindJSON(pointer, bodyBytes)
case bodyProtobuf:
hasVd = true
bodyBytes, err := req.GetBody()
if err != nil {
return hasVd, err
}
err = bindProtobuf(pointer, bodyBytes)
case bodyForm:
postForm, err := req.GetPostForm()
if err != nil {
return false, err
}
b, _ := jsonpkg.Marshal(postForm)
err = jsonpkg.Unmarshal(b, pointer)
default:
// query and form
form, err := req.GetForm()
if err != nil {
return false, err
}
b, _ := jsonpkg.Marshal(form)
err = jsonpkg.Unmarshal(b, pointer)
}
return
}
func (b *Binding) bindStruct(structPointer interface{}, structValue reflect.Value, req Request, pathParams PathParams) (hasVd bool, err error) {
recv, err := b.getOrPrepareReceiver(structValue)
if err != nil {
return
}
expr, err := b.vd.VM().Run(structValue)
if err != nil {
return
}
var bodyString string
bodyCodec, bodyBytes, postForm, fileHeaders, err := recv.getBodyInfo(req)
if len(bodyBytes) > 0 {
err = b.prebindBody(structPointer, structValue, bodyCodec, bodyBytes)
if err != nil {
return
}
bodyString = ameda.UnsafeBytesToString(bodyBytes)
}
queryValues := recv.getQuery(req)
cookies := recv.getCookies(req)
h := recv.getHeader(req)
for _, param := range recv.params {
for i, info := range param.tagInfos {
var found bool
switch info.paramIn {
case raw_body:
err = param.bindRawBody(info, expr, bodyBytes)
found = err == nil
case path:
found, err = param.bindPath(info, expr, pathParams)
case query:
found, err = param.bindQuery(info, expr, queryValues)
case cookie:
found, err = param.bindCookie(info, expr, cookies)
case header:
found, err = param.bindHeader(info, expr, h)
case form, json, protobuf:
if info.paramIn == in(bodyCodec) {
found, err = param.bindOrRequireBody(info, expr, bodyCodec, bodyString, postForm, fileHeaders,
recv.hasDefaultVal)
} else if info.required {
found = false
err = info.requiredError
}
case default_val:
found, err = param.bindDefaultVal(expr, param.defaultVal)
}
if found && err == nil {
break
}
if (found || i == len(param.tagInfos)-1) && err != nil {
return recv.hasVd, err
}
}
}
return recv.hasVd, nil
}
func (b *Binding) receiverValueOf(receiver interface{}) (reflect.Value, error) {
v := reflect.ValueOf(receiver)
if v.Kind() == reflect.Ptr {
v = ameda.DereferencePtrValue(v)
if v.IsValid() && v.CanAddr() {
return v, nil
}
}
return v, b.bindErrFactory("", "receiver must be a non-nil pointer")
}
func (b *Binding) getOrPrepareReceiver(value reflect.Value) (*receiver, error) {
runtimeTypeID := ameda.ValueFrom(value).RuntimeTypeID()
b.lock.RLock()
recv, ok := b.recvs[runtimeTypeID]
b.lock.RUnlock()
if ok {
return recv, nil
}
t := value.Type()
expr, err := b.vd.VM().Run(reflect.New(t).Elem())
if err != nil {
return nil, err
}
recv = &receiver{
params: make([]*paramInfo, 0, 16),
looseZeroMode: b.config.LooseZeroMode,
}
var errExprSelector tagexpr.ExprSelector
var errMsg string
var fieldsWithValidTag = make(map[string]bool)
expr.RangeFields(func(fh *tagexpr.FieldHandler) bool {
if !fh.Value(true).CanSet() {
selector := fh.StringSelector()
errMsg = "field cannot be set: " + selector
errExprSelector = tagexpr.ExprSelector(selector)
return true
}
tagKVs := b.config.parse(fh.StructField())
p := recv.getOrAddParam(fh, b.bindErrFactory)
tagInfos := [maxIn]*tagInfo{}
L:
for _, tagKV := range tagKVs {
paramIn := undefined
switch tagKV.name {
case b.config.Validator:
recv.hasVd = true
continue L
case b.config.PathParam:
paramIn = path
case b.config.FormBody:
paramIn = form
case b.config.Query:
paramIn = query
case b.config.Cookie:
paramIn = cookie
case b.config.Header:
paramIn = header
case b.config.protobufBody:
paramIn = protobuf
case b.config.jsonBody:
paramIn = json
case b.config.RawBody:
paramIn = raw_body
case b.config.defaultVal:
paramIn = default_val
default:
continue L
}
if paramIn == default_val {
tagInfos[paramIn] = &tagInfo{paramIn: default_val, paramName: tagKV.value}
} else {
tagInfos[paramIn] = tagKV.toInfo(paramIn == header)
}
}
for i, info := range tagInfos {
if info != nil {
if info.paramIn != default_val && info.paramName == "-" {
p.omitIns[in(i)] = true
recv.assginIn(in(i), false)
} else {
info.paramIn = in(i)
p.tagInfos = append(p.tagInfos, info)
recv.assginIn(in(i), true)
}
}
}
fs := string(fh.FieldSelector())
switch len(p.tagInfos) {
case 0:
var canDefault = true
for s := range fieldsWithValidTag {
if strings.HasPrefix(fs, s) {
canDefault = false
break
}
}
if canDefault {
if !goutil.IsExportedName(p.structField.Name) {
canDefault = false
}
}
// Supports the default binding order when there is no valid tag in the superior field of the exportable field
if canDefault {
for _, i := range sortedDefaultIn {
if p.omitIns[i] {
recv.assginIn(i, false)
continue
}
p.tagInfos = append(p.tagInfos, &tagInfo{
paramIn: i,
paramName: p.structField.Name,
})
recv.assginIn(i, true)
}
}
case 1:
if p.tagInfos[0].paramIn == default_val {
last := p.tagInfos[0]
p.tagInfos = make([]*tagInfo, 0, len(sortedDefaultIn)+1)
for _, i := range sortedDefaultIn {
if p.omitIns[i] {
recv.assginIn(i, false)
continue
}
p.tagInfos = append(p.tagInfos, &tagInfo{
paramIn: i,
paramName: p.structField.Name,
})
recv.assginIn(i, true)
}
p.tagInfos = append(p.tagInfos, last)
}
fallthrough
default:
fieldsWithValidTag[fs+tagexpr.FieldSeparator] = true
}
if !recv.hasVd {
_, recv.hasVd = tagKVs.lookup(b.config.Validator)
}
return true
})
if errMsg != "" {
return nil, b.bindErrFactory(errExprSelector.String(), errMsg)
}
if !recv.hasVd {
recv.hasVd, _ = b.findVdTag(ameda.DereferenceType(t), false, 20, map[reflect.Type]bool{})
}
recv.initParams()
b.lock.Lock()
b.recvs[runtimeTypeID] = recv
b.lock.Unlock()
return recv, nil
}
func (b *Binding) findVdTag(t reflect.Type, inMapOrSlice bool, depth int, exist map[reflect.Type]bool) (hasVd bool, err error) {
if depth <= 0 || exist[t] {
return
}
depth--
switch t.Kind() {
case reflect.Struct:
exist[t] = true
for i := t.NumField() - 1; i >= 0; i-- {
field := t.Field(i)
if inMapOrSlice {
tagKVs := b.config.parse(field)
for _, tagKV := range tagKVs {
if tagKV.name == b.config.Validator {
return true, nil
}
}
}
hasVd, _ = b.findVdTag(ameda.DereferenceType(field.Type), inMapOrSlice, depth, exist)
if hasVd {
return true, nil
}
}
return false, nil
case reflect.Slice, reflect.Array, reflect.Map:
return b.findVdTag(ameda.DereferenceType(t.Elem()), true, depth, exist)
default:
return false, nil
}
}
func (b *Binding) bindJSON(pointer interface{}, bodyBytes []byte) error {
if b.jsonUnmarshalFunc != nil {
return b.jsonUnmarshalFunc(bodyBytes, pointer)
} else {
return jsonpkg.Unmarshal(bodyBytes, pointer)
}
}
func (b *Binding) ResetJSONUnmarshaler(fn JSONUnmarshaler) {
b.jsonUnmarshalFunc = fn
}