-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinding.go
173 lines (141 loc) · 3.77 KB
/
binding.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
package fox
import (
"bytes"
"errors"
"io"
"net/http"
"reflect"
"github.com/gin-gonic/gin/binding"
)
// ErrBindNonPointerValue is required bind pointer
var ErrBindNonPointerValue = errors.New("can not bind to non-pointer value")
// DefaultBinder default binder
var DefaultBinder binding.Binding = binding.JSON
// Query binder
var Query = &queryBinding{}
var binders = map[string]binding.Binding{
binding.MIMEMultipartPOSTForm: binding.FormMultipart, // form
binding.MIMEPOSTForm: binding.Form, // form
}
var bodyBinders = map[string]binding.BindingBody{
binding.MIMEJSON: binding.JSON, // json
binding.MIMEYAML: binding.YAML, // yaml
binding.MIMEXML: binding.XML, // xml
binding.MIMEXML2: binding.XML, // xml
binding.MIMEPROTOBUF: binding.ProtoBuf, // protobuf
binding.MIMETOML: binding.TOML, // toml
}
// bind request arguments
func bind(ctx *Context, obj interface{}) (err error) {
vPtr := reflect.ValueOf(obj)
if vPtr.Kind() != reflect.Ptr {
return ErrBindNonPointerValue
}
// bind request body
// --------------------------------------------------------------------------
var (
contentType = filterFlags(ctx.Request.Header.Get("Content-Type"))
body []byte
)
if body, err = ctx.RequestBody(); err != nil {
return err
}
defer func() {
// copy the request body to the next handler
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(body))
}()
if ctx.Request.Method == http.MethodGet {
err = binding.Form.Bind(ctx.Request, obj)
} else if binder, exists := binders[contentType]; exists {
err = binder.Bind(ctx.Request, obj)
} else if bodyBinder, exists := bodyBinders[contentType]; exists {
if len(body) > 0 {
err = bodyBinder.BindBody(body, obj)
}
} else if DefaultBinder != nil {
if bodyBinder, ok := DefaultBinder.(binding.BindingBody); ok {
if len(body) > 0 {
err = bodyBinder.BindBody(body, obj)
}
} else {
err = DefaultBinder.Bind(ctx.Request, obj)
}
}
if err != nil {
return err
}
// bind request query, header and uri
// --------------------------------------------------------------------------
vPtr = vPtr.Elem()
for vPtr.Kind() == reflect.Ptr {
if vPtr.IsNil() {
vPtr.Set(reflect.New(vPtr.Type().Elem()))
}
vPtr = vPtr.Elem()
}
if vPtr.Kind() != reflect.Struct {
return
}
var vType = vPtr.Type()
var hasQueryField, hasURIField, hasHeaderField bool
for i := 0; i < vPtr.NumField(); i++ {
field := vType.Field(i)
if tag := field.Tag.Get("query"); tag != "" && tag != "-" {
hasQueryField = true
}
if tag := field.Tag.Get("uri"); tag != "" && tag != "-" {
hasURIField = true
}
if tag := field.Tag.Get("header"); tag != "" && tag != "-" {
hasHeaderField = true
}
}
// bind query params
if hasQueryField {
if err = Query.Bind(ctx.Request, obj); err != nil {
return err
}
}
// bind uri path
if hasURIField && len(ctx.Params) > 0 {
m := make(map[string][]string)
for _, v := range ctx.Params {
m[v.Key] = []string{v.Value}
}
if err = binding.Uri.BindUri(m, obj); err != nil {
return err
}
}
// bind header fields
if hasHeaderField {
if err = binding.Header.Bind(ctx.Request, obj); err != nil {
return err
}
}
if valider, ok := obj.(IsValider); ok {
return valider.IsValid()
}
return nil
}
type queryBinding struct{}
func (queryBinding) Name() string {
return "query"
}
func (queryBinding) Bind(req *http.Request, obj any) error {
values := req.URL.Query()
if err := binding.MapFormWithTag(obj, values, "query"); err != nil {
return err
}
if binding.Validator == nil {
return nil
}
return binding.Validator.ValidateStruct(obj)
}
func filterFlags(content string) string {
for i, char := range content {
if char == ' ' || char == ';' {
return content[:i]
}
}
return content
}