forked from liilinc/go-tagexpr
/
bind.go
325 lines (299 loc) · 8.27 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
package binding
import (
jsonpkg "encoding/json"
"net/http"
"reflect"
"strings"
"sync"
"github.com/henrylee2cn/ameda"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/goutil/tpack"
"github.com/bytedance/go-tagexpr"
"github.com/bytedance/go-tagexpr/validator"
)
// Binding binding and verification tool for http request
type Binding struct {
vd *validator.Validator
recvs map[int32]*receiver
lock sync.RWMutex
bindErrFactory func(failField, msg string) error
config Config
}
// 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[int32]*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 {
v, hasVd, err := b.bind(recvPointer, req, pathParams)
if err != nil {
return err
}
if hasVd {
return b.vd.Validate(v)
}
return nil
}
// Bind binds the request parameters.
func (b *Binding) Bind(recvPointer interface{}, req *http.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 *http.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 *http.Request, _ PathParams) (hasVd bool, err error) {
bodyCodec, bodyBytes, err := getBodyInfo(req)
if err != nil {
return
}
switch bodyCodec {
case bodyJSON:
hasVd = true
err = bindJSON(pointer, bodyBytes)
case bodyProtobuf:
hasVd = true
err = bindProtobuf(pointer, bodyBytes)
case bodyForm:
b, _ := jsonpkg.Marshal(req.PostForm)
err = jsonpkg.Unmarshal(b, pointer)
default:
// query and form
b, _ := jsonpkg.Marshal(req.Form)
err = jsonpkg.Unmarshal(b, pointer)
}
return
}
func (b *Binding) bindStruct(structPointer interface{}, structValue reflect.Value, req *http.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
}
bodyCodec, bodyBytes, err := recv.getBodyInfo(req)
if len(bodyBytes) > 0 {
err = recv.prebindBody(structPointer, structValue, bodyCodec, bodyBytes)
}
if err != nil {
return
}
bodyString := ameda.UnsafeBytesToString(bodyBytes)
postForm := req.PostForm
queryValues := recv.getQuery(req)
cookies := recv.getCookies(req)
for _, param := range recv.params {
for i, info := range param.tagInfos {
var found bool
switch info.paramIn {
case path:
found, err = param.bindPath(info, expr, pathParams)
case query:
found, err = param.bindQuery(info, expr, queryValues)
case cookie:
err = param.bindCookie(info, expr, cookies)
found = err == nil
case header:
found, err = param.bindHeader(info, expr, req.Header)
case form, json, protobuf:
if info.paramIn == in(bodyCodec) {
found, err = param.bindOrRequireBody(info, expr, bodyCodec, bodyString, postForm)
} else if info.required {
found = false
err = info.requiredError
}
case raw_body:
err = param.bindRawBody(info, expr, bodyBytes)
found = err == nil
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 = goutil.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 := tpack.From(value).RuntimeTypeID()
b.lock.RLock()
recv, ok := b.recvs[runtimeTypeID]
b.lock.RUnlock()
if ok {
return recv, nil
}
expr, err := b.vd.VM().Run(reflect.New(value.Type()).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.defaultSplit()
}
}
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())
if len(p.tagInfos) == 0 {
var canDefault = true
for s := range fieldsWithValidTag {
if strings.HasPrefix(fs, s) {
canDefault = false
break
}
}
// Support default binding order when there is no valid tag in the superior fields
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)
}
}
} else {
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)
}
recv.initParams()
b.lock.Lock()
b.recvs[runtimeTypeID] = recv
b.lock.Unlock()
return recv, nil
}