forked from bytedance/go-tagexpr
/
param_info.go
268 lines (251 loc) · 6.22 KB
/
param_info.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
package binding
import (
"net/http"
"net/url"
"reflect"
"strconv"
"github.com/bytedance/go-tagexpr"
"github.com/henrylee2cn/goutil"
"github.com/tidwall/gjson"
)
type paramInfo struct {
fieldSelector string
structField reflect.StructField
tagInfos []*tagInfo
omitIns map[in]bool
bindErrFactory func(failField, msg string) error
looseZeroMode bool
}
func (p *paramInfo) name(paramIn in) string {
var name string
for _, info := range p.tagInfos {
if info.paramIn == json {
name = info.paramName
break
}
}
if name == "" {
return p.structField.Name
}
return name
}
func (p *paramInfo) getField(expr *tagexpr.TagExpr, initZero bool) (reflect.Value, error) {
fh, found := expr.Field(p.fieldSelector)
if found {
v := fh.Value(initZero)
if v.IsValid() {
return v, nil
}
}
return reflect.Value{}, nil
}
func (p *paramInfo) bindRawBody(info *tagInfo, expr *tagexpr.TagExpr, bodyBytes []byte) error {
if len(bodyBytes) == 0 {
if info.required {
return info.requiredError
}
return nil
}
v, err := p.getField(expr, true)
if err != nil || !v.IsValid() {
return err
}
v = goutil.DereferenceValue(v)
switch v.Kind() {
case reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
return info.typeError
}
v.Set(reflect.ValueOf(bodyBytes))
return nil
case reflect.String:
v.Set(reflect.ValueOf(goutil.BytesToString(bodyBytes)))
return nil
default:
return info.typeError
}
}
func (p *paramInfo) bindPath(info *tagInfo, expr *tagexpr.TagExpr, pathParams PathParams) (bool, error) {
r, found := pathParams.Get(info.paramName)
if !found {
if info.required {
return false, info.requiredError
}
return false, nil
}
return true, p.bindStringSlice(info, expr, []string{r})
}
func (p *paramInfo) bindQuery(info *tagInfo, expr *tagexpr.TagExpr, queryValues url.Values) (bool, error) {
return p.bindMapStrings(info, expr, queryValues)
}
func (p *paramInfo) bindHeader(info *tagInfo, expr *tagexpr.TagExpr, header http.Header) (bool, error) {
return p.bindMapStrings(info, expr, header)
}
func (p *paramInfo) bindCookie(info *tagInfo, expr *tagexpr.TagExpr, cookies []*http.Cookie) error {
var r []string
for _, c := range cookies {
if c.Name == info.paramName {
r = append(r, c.Value)
}
}
if len(r) == 0 {
if info.required {
return info.requiredError
}
return nil
}
return p.bindStringSlice(info, expr, r)
}
func (p *paramInfo) bindOrRequireBody(info *tagInfo, expr *tagexpr.TagExpr, bodyCodec codec, bodyString string, postForm map[string][]string) (bool, error) {
switch bodyCodec {
case bodyForm:
return p.bindMapStrings(info, expr, postForm)
case bodyJSON:
err := p.checkRequireJSON(info, expr, bodyString, false)
return err == nil, err
case bodyProtobuf:
err := p.checkRequireProtobuf(info, expr, false)
return err == nil, err
default:
return false, info.contentTypeError
}
}
func (p *paramInfo) checkRequireProtobuf(info *tagInfo, expr *tagexpr.TagExpr, checkOpt bool) error {
if checkOpt && !info.required {
v, err := p.getField(expr, false)
if err != nil || !v.IsValid() {
return info.requiredError
}
}
return nil
}
func (p *paramInfo) checkRequireJSON(info *tagInfo, expr *tagexpr.TagExpr, bodyString string, checkOpt bool) error {
if jsonIndependentRequired && (checkOpt || info.required) {
r := gjson.Get(bodyString, info.namePath)
if !r.Exists() {
return info.requiredError
}
v, err := p.getField(expr, false)
if err != nil || !v.IsValid() {
return info.requiredError
}
}
return nil
}
func (p *paramInfo) bindMapStrings(info *tagInfo, expr *tagexpr.TagExpr, values map[string][]string) (bool, error) {
r, ok := values[info.paramName]
if !ok || len(r) == 0 {
if info.required {
return false, info.requiredError
}
return false, nil
}
return true, p.bindStringSlice(info, expr, r)
}
// NOTE: len(a)>0
func (p *paramInfo) bindStringSlice(info *tagInfo, expr *tagexpr.TagExpr, a []string) error {
v, err := p.getField(expr, true)
if err != nil || !v.IsValid() {
return err
}
v = goutil.DereferenceValue(v)
switch v.Kind() {
case reflect.String:
v.Set(reflect.ValueOf(a[0]))
return nil
case reflect.Bool:
var bol bool
bol, err = strconv.ParseBool(a[0])
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetBool(bol)
return nil
}
case reflect.Float32:
var f float64
f, err = strconv.ParseFloat(a[0], 32)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetFloat(f)
return nil
}
case reflect.Float64:
var f float64
f, err = strconv.ParseFloat(a[0], 64)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetFloat(f)
return nil
}
case reflect.Int64, reflect.Int:
var i int64
i, err = strconv.ParseInt(a[0], 10, 64)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetInt(i)
return nil
}
case reflect.Int32:
var i int64
i, err = strconv.ParseInt(a[0], 10, 32)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetInt(i)
return nil
}
case reflect.Int16:
var i int64
i, err = strconv.ParseInt(a[0], 10, 16)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetInt(i)
return nil
}
case reflect.Int8:
var i int64
i, err = strconv.ParseInt(a[0], 10, 8)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetInt(i)
return nil
}
case reflect.Uint64, reflect.Uint:
var u uint64
u, err = strconv.ParseUint(a[0], 10, 64)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetUint(u)
return nil
}
case reflect.Uint32:
var u uint64
u, err = strconv.ParseUint(a[0], 10, 32)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetUint(u)
return nil
}
case reflect.Uint16:
var u uint64
u, err = strconv.ParseUint(a[0], 10, 16)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetUint(u)
return nil
}
case reflect.Uint8:
var u uint64
u, err = strconv.ParseUint(a[0], 10, 8)
if err == nil || (a[0] == "" && p.looseZeroMode) {
v.SetUint(u)
return nil
}
case reflect.Slice:
vv, err := stringsToValue(v.Type().Elem(), a, p.looseZeroMode)
if err == nil {
v.Set(vv)
return nil
}
fallthrough
default:
fn := typeUnmarshalFuncs[v.Type()]
if fn != nil {
vv, err := fn(a[0], p.looseZeroMode)
if err == nil {
v.Set(vv)
return nil
}
}
}
return info.typeError
}