/
binding.go
370 lines (320 loc) · 9.99 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
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
// Package binding deserializes data from HTTP requests into a struct
// ready for your application to use (without reflection). It also
// facilitates data validation and error handling.
package binding
import (
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"strconv"
"strings"
"time"
)
// Bind takes data out of the request and deserializes into a struct according
// to the Content-Type of the request. If no Content-Type is specified, there
// better be data in the query string, otherwise an error will be produced.
func Bind(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors
contentType := req.Header.Get("Content-Type")
if strings.Contains(contentType, "form-urlencoded") {
return Form(req, userStruct)
}
if strings.Contains(contentType, "multipart/form-data") {
return MultipartForm(req, userStruct)
}
if strings.Contains(contentType, "json") {
return Json(req, userStruct)
}
if contentType == "" {
if len(req.URL.Query()) > 0 {
return Form(req, userStruct)
} else {
errs.Add([]string{}, ContentTypeError, "Empty Content-Type")
}
} else {
errs.Add([]string{}, ContentTypeError, "Unsupported Content-Type")
}
return errs
}
// Form deserializes form data out of the request into a struct you provide.
// This function invokes data validation after deserialization.
func Form(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors
parseErr := req.ParseForm()
if parseErr != nil {
errs.Add([]string{}, DeserializationError, parseErr.Error())
return errs
}
return bindForm(req, userStruct, req.Form, nil, errs)
}
// MultipartForm reads a multipart form request and deserializes its data and
// files into a struct you provide. Files should be deserialized into
// *multipart.FileHeader fields.
func MultipartForm(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors
multipartReader, err := req.MultipartReader()
if err != nil {
errs.Add([]string{}, DeserializationError, err.Error())
return errs
}
form, parseErr := multipartReader.ReadForm(MaxMemory)
if parseErr != nil {
errs.Add([]string{}, DeserializationError, parseErr.Error())
return errs
}
req.MultipartForm = form
return bindForm(req, userStruct, req.MultipartForm.Value, req.MultipartForm.File, errs)
}
// Json deserializes a JSON request body into a struct you specify
// using the standard encoding/json package (which uses reflection).
// This function invokes data validation after deserialization.
func Json(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors
if req.Body != nil {
defer req.Body.Close()
err := json.NewDecoder(req.Body).Decode(userStruct)
if err != nil && err != io.EOF {
errs.Add([]string{}, DeserializationError, err.Error())
return errs
}
} else {
errs.Add([]string{}, DeserializationError, "Empty request body")
return errs
}
errs = append(errs, Validate(req, userStruct)...)
return errs
}
// Validate ensures that all conditions have been met on every field in the
// populated struct. Validation should occur after the request has been
// deserialized into the struct.
func Validate(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors
if validator, ok := userStruct.(Validator); ok {
errs = validator.Validate(req, errs)
}
return errs
}
func bindForm(req *http.Request, userStruct FieldMapper, formData map[string][]string,
formFile map[string][]*multipart.FileHeader, errs Errors) Errors {
fm := userStruct.FieldMap()
for fieldPointer, fieldNameOrSpec := range fm {
fieldName, fieldHasSpec, fieldSpec := fieldNameAndSpec(fieldNameOrSpec)
_, isFile := fieldPointer.(**multipart.FileHeader)
_, isFileSlice := fieldPointer.(*[]**multipart.FileHeader)
strs := formData[fieldName]
if !isFile && !isFileSlice {
if len(strs) == 0 {
if fieldHasSpec && fieldSpec.Required {
errs.Add([]string{fieldName}, RequiredError, "Required")
}
continue
}
if binder, ok := fieldPointer.(Binder); ok {
errs = binder.Bind(strs, errs)
continue
}
}
errorHandler := func(err error) {
if err != nil {
errs.Add([]string{fieldName}, TypeError, err.Error())
}
}
if fieldSpec.Binder != nil {
errs = fieldSpec.Binder(strs, errs)
continue
}
switch t := fieldPointer.(type) {
case *uint8:
val, err := strconv.ParseUint(strs[0], 10, 8)
errorHandler(err)
*t = uint8(val)
case *uint16:
val, err := strconv.ParseUint(strs[0], 10, 16)
errorHandler(err)
*t = uint16(val)
case *uint32:
val, err := strconv.ParseUint(strs[0], 10, 32)
errorHandler(err)
*t = uint32(val)
case *uint64:
val, err := strconv.ParseUint(strs[0], 10, 64)
errorHandler(err)
*t = val
case *int8:
val, err := strconv.ParseInt(strs[0], 10, 8)
errorHandler(err)
*t = int8(val)
case *int16:
val, err := strconv.ParseInt(strs[0], 10, 16)
errorHandler(err)
*t = int16(val)
case *int32:
val, err := strconv.ParseInt(strs[0], 10, 32)
errorHandler(err)
*t = int32(val)
case *int64:
val, err := strconv.ParseInt(strs[0], 10, 64)
errorHandler(err)
*t = val
case *float32:
val, err := strconv.ParseFloat(strs[0], 32)
errorHandler(err)
*t = float32(val)
case *[]float32:
for _, str := range strs {
val, err := strconv.ParseFloat(str, 32)
errorHandler(err)
*t = append(*t, float32(val))
}
case *float64:
val, err := strconv.ParseFloat(strs[0], 64)
errorHandler(err)
*t = val
case *[]float64:
for _, str := range strs {
val, err := strconv.ParseFloat(str, 64)
errorHandler(err)
*t = append(*t, val)
}
case *uint:
val, err := strconv.ParseUint(strs[0], 10, 0)
errorHandler(err)
*t = uint(val)
case *[]uint:
for _, str := range strs {
val, err := strconv.ParseUint(str, 10, 0)
errorHandler(err)
*t = append(*t, uint(val))
}
case *int:
val, err := strconv.ParseInt(strs[0], 10, 0)
errorHandler(err)
*t = int(val)
case *[]int:
for _, str := range strs {
val, err := strconv.ParseInt(str, 10, 0)
errorHandler(err)
*t = append(*t, int(val))
}
case *bool:
val, err := strconv.ParseBool(strs[0])
errorHandler(err)
*t = val
case *[]bool:
for _, str := range strs {
val, err := strconv.ParseBool(str)
errorHandler(err)
*t = append(*t, val)
}
case *string:
*t = strs[0]
case *[]string:
*t = strs
case *time.Time:
timeFormat := TimeFormat
if fieldSpec.TimeFormat != "" {
timeFormat = fieldSpec.TimeFormat
}
val, err := time.Parse(timeFormat, strs[0])
errorHandler(err)
*t = val
case *[]time.Time:
timeFormat := TimeFormat
if fieldSpec.TimeFormat != "" {
timeFormat = fieldSpec.TimeFormat
}
for _, str := range strs {
val, err := time.Parse(timeFormat, str)
errorHandler(err)
*t = append(*t, val)
}
case **multipart.FileHeader:
if files, ok := formFile[fieldName]; ok {
*t = files[0]
}
case *[]**multipart.FileHeader:
if files, ok := formFile[fieldName]; ok {
for _, file := range files {
*t = append(*t, &file)
}
}
default:
errorHandler(errors.New("Field type is unsupported by the application"))
}
}
errs = append(errs, Validate(req, userStruct)...)
return errs
}
func fieldNameAndSpec(fieldNameOrSpec interface{}) (string, bool, Field) {
var fieldName string
fieldSpec, fieldHasSpec := fieldNameOrSpec.(Field)
if fieldHasSpec {
fieldName = fieldSpec.Form
} else if name, ok := fieldNameOrSpec.(string); ok {
fieldName = name
}
return fieldName, fieldHasSpec, fieldSpec
}
type (
// Only types that are FieldMappers can have request data deserialized into them.
FieldMapper interface {
// FieldMap returns a map that keys field names from the request
// to pointers into which the values will be deserialized.
FieldMap() FieldMap
}
// FieldMap is a map of pointers to struct fields -> field names from the request.
// The values could also be Field structs to specify metadata about the field.
FieldMap map[interface{}]interface{}
// Field describes the properties of a struct field.
Field struct {
// Target is the struct field to deserialize into.
//Target interface{}
// Form is the form field name to bind from
Form string
// Required indicates whether the field is required. A required
// field that deserializes into the zero value for that type
// will generate an error.
Required bool
// TimeFormat specifies the time format for time.Time fields.
TimeFormat string
// Binder is a function that converts the incoming request value(s)
// to the field type; in other words, this field is populated
// by executing this function. Useful when the custom type doesn't
// implement the Binder interface.
Binder func([]string, Errors) Errors
}
// Binder is an interface which can deserialize itself from a slice of string
// coming from the request. Implement this interface so the type can be
// populated from form data in HTTP requests.
Binder interface {
// Bind populates the type with data in []string, which comes from the
// HTTP request.
Bind([]string, Errors) Errors
}
// Validator can be implemented by your type to handle some
// rudimentary request validation separately from your
// application logic.
Validator interface {
// Validate validates that the request is OK. It is recommended
// that validation be limited to checking values for syntax and
// semantics, enough to know that you can make sense of the request
// in your application. For example, you might verify that a credit
// card number matches a valid pattern, but you probably wouldn't
// perform an actual credit card authorization here.
Validate(*http.Request, Errors) Errors
}
)
var (
// Maximum amount of memory to use when parsing a multipart form.
// Set this to whatever value you prefer; default is 10 MB.
MaxMemory = int64(1024 * 1024 * 10)
// If no TimeFormat is specified for a time.Time field, this
// format will be used by default when parsing.
TimeFormat = time.RFC3339
)
const (
jsonContentType = "application/json; charset=utf-8"
StatusUnprocessableEntity = 422
)