-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_binding.go
191 lines (159 loc) · 6.34 KB
/
context_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// see: https://github.com/gin-gonic/gin/blob/master/binding/binding.go
package chain
import "net/http"
// Binding describes the interface which needs to be implemented for binding the
// data present in the request such as JSON request body, query parameters or
// the form POST.
type Binding interface {
Bind(*Context, any) error
}
// These implement the Binding interface and can be used to bind the data
// present in the request to struct instances.
var (
BindingXML Binding = xmlBinding{} // xml
BindingJSON Binding = jsonBinding{} // json
BindingPath Binding = pathBinding{} // path
BindingForm Binding = formBinding{} // form
BindingFormPost Binding = formPostBinding{} // form
BindingFormMultipart Binding = formMultipartBinding{} // form
BindingQuery Binding = queryBinding{} // query
BindingHeader Binding = headerBinding{} // header
BindingDefault Binding = &BindingDefaultStruct{} // query, json, xml, form
)
type BindingDefaultStruct struct {
BindHeader bool
}
func (s *BindingDefaultStruct) Bind(ctx *Context, obj any) error {
bb := []Binding{BindingQuery}
if ctx.paramCount > 0 {
bb = append(bb, BindingPath)
}
if s.BindHeader {
bb = append(bb, BindingHeader)
}
if ctx.Request.Method != http.MethodGet {
switch ctx.GetContentType() {
case "application/json":
bb = append(bb, BindingJSON)
case "application/xml", "text/xml":
bb = append(bb, BindingXML)
case "multipart/form-data":
bb = append(bb, BindingFormMultipart)
default: // case "application/x-www-form-urlencoded":
bb = append(bb, BindingForm)
}
}
for _, b := range bb {
if err := b.Bind(ctx, obj); err != nil {
return err
}
}
return nil
}
// Bind checks the Method and Content-Type to select a binding engine automatically,
// Depending on the "Content-Type" header different bindings are used, for example:
//
// "application/json" --> JSON binding
// "application/xml" --> XML binding
//
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (ctx *Context) Bind(obj any) error {
return ctx.MustBindWith(obj, BindingDefault)
}
// ShouldBind checks the Method and Content-Type to select a binding engine automatically,
// Depending on the "Content-Type" header different bindings are used, for example:
//
// "application/json" --> JSON binding
// "application/xml" --> XML binding
//
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.
func (ctx *Context) ShouldBind(obj any) error {
return ctx.ShouldBindWith(obj, BindingDefault)
}
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (ctx *Context) ShouldBindWith(obj any, b Binding) error {
if err := b.Bind(ctx, obj); err != nil {
return err
}
return validate(obj)
}
// MustBindWith binds the passed struct pointer using the specified binding engine.
// It will abort the request with HTTP 400 if any error occurs.
// See the binding package.
func (ctx *Context) MustBindWith(obj any, b Binding) error {
if err := ctx.ShouldBindWith(obj, b); err != nil {
ctx.BadRequest()
return err
}
return nil
}
// BindJSON is a shortcut for c.MustBindWith(obj, BindingJSON).
func (ctx *Context) BindJSON(obj any) error {
return ctx.MustBindWith(obj, BindingJSON)
}
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, BindingJSON).
func (c *Context) ShouldBindJSON(obj any) error {
return c.ShouldBindWith(obj, BindingJSON)
}
// BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).
func (ctx *Context) BindXML(obj any) error {
return ctx.MustBindWith(obj, BindingXML)
}
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, BindingXML).
func (c *Context) ShouldBindXML(obj any) error {
return c.ShouldBindWith(obj, BindingXML)
}
// BindPath is a shortcut for c.MustBindWith(obj, BindingPath).
func (ctx *Context) BindPath(obj any) error {
return ctx.MustBindWith(obj, BindingPath)
}
// ShouldBindPath is a shortcut for c.ShouldBindWith(obj, BindingPath).
func (ctx *Context) ShouldBindPath(obj any) error {
return ctx.ShouldBindWith(obj, BindingPath)
}
// BindQuery is a shortcut for c.MustBindWith(obj, BindingQuery).
func (ctx *Context) BindQuery(obj any) error {
return ctx.MustBindWith(obj, BindingQuery)
}
// ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, BindingQuery).
func (ctx *Context) ShouldBindQuery(obj any) error {
return ctx.ShouldBindWith(obj, BindingQuery)
}
// BindHeader is a shortcut for c.MustBindWith(obj, BindingHeader).
func (ctx *Context) BindHeader(obj any) error {
return ctx.MustBindWith(obj, BindingHeader)
}
// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, BindingHeader).
func (ctx *Context) ShouldBindHeader(obj any) error {
return ctx.ShouldBindWith(obj, BindingHeader)
}
// BindForm is a shortcut for c.MustBindWith(obj, BindingForm).
func (ctx *Context) BindForm(obj any) error {
return ctx.MustBindWith(obj, BindingForm)
}
// ShouldBindForm is a shortcut for c.ShouldBindWith(obj, BindingForm).
func (ctx *Context) ShouldBindForm(obj any) error {
return ctx.ShouldBindWith(obj, BindingForm)
}
// BindFormPost is a shortcut for c.MustBindWith(obj, BindingFormPost).
func (ctx *Context) BindFormPost(obj any) error {
return ctx.MustBindWith(obj, BindingFormPost)
}
// ShouldBindFormPost is a shortcut for c.ShouldBindWith(obj, BindingFormPost).
func (ctx *Context) ShouldBindFormPost(obj any) error {
return ctx.ShouldBindWith(obj, BindingFormPost)
}
// BindFormMultipart is a shortcut for c.MustBindWith(obj, BindingFormMultipart).
func (ctx *Context) BindFormMultipart(obj any) error {
return ctx.MustBindWith(obj, BindingFormMultipart)
}
// ShouldBindFormMultipart is a shortcut for c.ShouldBindWith(obj, BindingFormMultipart).
func (ctx *Context) ShouldBindFormMultipart(obj any) error {
return ctx.ShouldBindWith(obj, BindingFormMultipart)
}