-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
binder.go
530 lines (468 loc) · 15.2 KB
/
binder.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// A Binder translates between string parameters and Go data structures.
type Binder struct {
// Bind takes the name and type of the desired parameter and constructs it
// from one or more values from Params.
//
// Example
//
// Request:
// url?id=123&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=rob
//
// Action:
// Example.Action(id int, ol []int, ul []string, user User)
//
// Calls:
// Bind(params, "id", int): 123
// Bind(params, "ol", []int): {1, 2}
// Bind(params, "ul", []string): {"str", "array"}
// Bind(params, "user", User): User{Name:"rob"}
//
// Note that only exported struct fields may be bound.
Bind func(params *Params, name string, typ reflect.Type) reflect.Value
// Unbind serializes a given value to one or more URL parameters of the given
// name.
Unbind func(output map[string]string, name string, val interface{})
}
// ValueBinder is adapter for easily making one-key-value binders.
func ValueBinder(f func(value string, typ reflect.Type) reflect.Value) func(*Params, string, reflect.Type) reflect.Value {
return func(params *Params, name string, typ reflect.Type) reflect.Value {
vals, ok := params.Values[name]
if !ok || len(vals) == 0 {
return reflect.Zero(typ)
}
return f(vals[0], typ)
}
}
// Revel's default date and time constants
const (
DefaultDateFormat = "2006-01-02"
DefaultDateTimeFormat = "2006-01-02 15:04"
)
// Binders type and kind definition
var (
// These are the lookups to find a Binder for any type of data.
// The most specific binder found will be used (Type before Kind)
TypeBinders = make(map[reflect.Type]Binder)
KindBinders = make(map[reflect.Kind]Binder)
// Applications can add custom time formats to this array, and they will be
// automatically attempted when binding a time.Time.
TimeFormats = []string{}
DateFormat string
DateTimeFormat string
IntBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
if len(val) == 0 {
return reflect.Zero(typ)
}
intValue, err := strconv.ParseInt(val, 10, 64)
if err != nil {
WARN.Println(err)
return reflect.Zero(typ)
}
pValue := reflect.New(typ)
pValue.Elem().SetInt(intValue)
return pValue.Elem()
}),
Unbind: func(output map[string]string, key string, val interface{}) {
output[key] = fmt.Sprintf("%d", val)
},
}
UintBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
if len(val) == 0 {
return reflect.Zero(typ)
}
uintValue, err := strconv.ParseUint(val, 10, 64)
if err != nil {
WARN.Println(err)
return reflect.Zero(typ)
}
pValue := reflect.New(typ)
pValue.Elem().SetUint(uintValue)
return pValue.Elem()
}),
Unbind: func(output map[string]string, key string, val interface{}) {
output[key] = fmt.Sprintf("%d", val)
},
}
FloatBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
if len(val) == 0 {
return reflect.Zero(typ)
}
floatValue, err := strconv.ParseFloat(val, 64)
if err != nil {
WARN.Println(err)
return reflect.Zero(typ)
}
pValue := reflect.New(typ)
pValue.Elem().SetFloat(floatValue)
return pValue.Elem()
}),
Unbind: func(output map[string]string, key string, val interface{}) {
output[key] = fmt.Sprintf("%f", val)
},
}
StringBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
return reflect.ValueOf(val)
}),
Unbind: func(output map[string]string, name string, val interface{}) {
output[name] = val.(string)
},
}
// Booleans support a various value formats,
// refer `revel.Atob` method.
BoolBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
return reflect.ValueOf(Atob(val))
}),
Unbind: func(output map[string]string, name string, val interface{}) {
output[name] = fmt.Sprintf("%t", val)
},
}
PointerBinder = Binder{
Bind: func(params *Params, name string, typ reflect.Type) reflect.Value {
v := Bind(params, name, typ.Elem())
if v.CanAddr() {
return v.Addr()
}
return v
},
Unbind: func(output map[string]string, name string, val interface{}) {
Unbind(output, name, reflect.ValueOf(val).Elem().Interface())
},
}
TimeBinder = Binder{
Bind: ValueBinder(func(val string, typ reflect.Type) reflect.Value {
for _, f := range TimeFormats {
if r, err := time.Parse(f, val); err == nil {
return reflect.ValueOf(r)
}
}
return reflect.Zero(typ)
}),
Unbind: func(output map[string]string, name string, val interface{}) {
var (
t = val.(time.Time)
format = DateTimeFormat
h, m, s = t.Clock()
)
if h == 0 && m == 0 && s == 0 {
format = DateFormat
}
output[name] = t.Format(format)
},
}
MapBinder = Binder{
Bind: bindMap,
Unbind: unbindMap,
}
)
// Used to keep track of the index for individual keyvalues.
type sliceValue struct {
index int // Index extracted from brackets. If -1, no index was provided.
value reflect.Value // the bound value for this slice element.
}
// This function creates a slice of the given type, Binds each of the individual
// elements, and then sets them to their appropriate location in the slice.
// If elements are provided without an explicit index, they are added (in
// unspecified order) to the end of the slice.
func bindSlice(params *Params, name string, typ reflect.Type) reflect.Value {
// Collect an array of slice elements with their indexes (and the max index).
maxIndex := -1
numNoIndex := 0
sliceValues := []sliceValue{}
// Factor out the common slice logic (between form values and files).
processElement := func(key string, vals []string, files []*multipart.FileHeader) {
if !strings.HasPrefix(key, name+"[") {
return
}
// Extract the index, and the index where a sub-key starts. (e.g. field[0].subkey)
index := -1
leftBracket, rightBracket := len(name), strings.Index(key[len(name):], "]")+len(name)
if rightBracket > leftBracket+1 {
index, _ = strconv.Atoi(key[leftBracket+1 : rightBracket])
}
subKeyIndex := rightBracket + 1
// Handle the indexed case.
if index > -1 {
if index > maxIndex {
maxIndex = index
}
sliceValues = append(sliceValues, sliceValue{
index: index,
value: Bind(params, key[:subKeyIndex], typ.Elem()),
})
return
}
// It's an un-indexed element. (e.g. element[])
numNoIndex += len(vals) + len(files)
for _, val := range vals {
// Unindexed values can only be direct-bound.
sliceValues = append(sliceValues, sliceValue{
index: -1,
value: BindValue(val, typ.Elem()),
})
}
for _, fileHeader := range files {
sliceValues = append(sliceValues, sliceValue{
index: -1,
value: BindFile(fileHeader, typ.Elem()),
})
}
}
for key, vals := range params.Values {
processElement(key, vals, nil)
}
for key, fileHeaders := range params.Files {
processElement(key, nil, fileHeaders)
}
resultArray := reflect.MakeSlice(typ, maxIndex+1, maxIndex+1+numNoIndex)
for _, sv := range sliceValues {
if sv.index != -1 {
resultArray.Index(sv.index).Set(sv.value)
} else {
resultArray = reflect.Append(resultArray, sv.value)
}
}
return resultArray
}
// Break on dots and brackets.
// e.g. bar => "bar", bar.baz => "bar", bar[0] => "bar"
func nextKey(key string) string {
fieldLen := strings.IndexAny(key, ".[")
if fieldLen == -1 {
return key
}
return key[:fieldLen]
}
func unbindSlice(output map[string]string, name string, val interface{}) {
v := reflect.ValueOf(val)
for i := 0; i < v.Len(); i++ {
Unbind(output, fmt.Sprintf("%s[%d]", name, i), v.Index(i).Interface())
}
}
func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
resultPointer := reflect.New(typ)
result := resultPointer.Elem()
if params.JSON != nil {
// Try to inject the response as a json into the created result
if err := json.Unmarshal(params.JSON, resultPointer.Interface()); err != nil {
WARN.Println("W: bindStruct: Unable to unmarshal request:", name, err)
}
return result
}
fieldValues := make(map[string]reflect.Value)
for key := range params.Values {
if !strings.HasPrefix(key, name+".") {
continue
}
// Get the name of the struct property.
// Strip off the prefix. e.g. foo.bar.baz => bar.baz
suffix := key[len(name)+1:]
fieldName := nextKey(suffix)
fieldLen := len(fieldName)
if _, ok := fieldValues[fieldName]; !ok {
// Time to bind this field. Get it and make sure we can set it.
fieldValue := result.FieldByName(fieldName)
if !fieldValue.IsValid() {
WARN.Println("W: bindStruct: Field not found:", fieldName)
continue
}
if !fieldValue.CanSet() {
WARN.Println("W: bindStruct: Field not settable:", fieldName)
continue
}
boundVal := Bind(params, key[:len(name)+1+fieldLen], fieldValue.Type())
fieldValue.Set(boundVal)
fieldValues[fieldName] = boundVal
}
}
return result
}
func unbindStruct(output map[string]string, name string, iface interface{}) {
val := reflect.ValueOf(iface)
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
structField := typ.Field(i)
fieldValue := val.Field(i)
// PkgPath is specified to be empty exactly for exported fields.
if structField.PkgPath == "" {
Unbind(output, fmt.Sprintf("%s.%s", name, structField.Name), fieldValue.Interface())
}
}
}
// Helper that returns an upload of the given name, or nil.
func getMultipartFile(params *Params, name string) multipart.File {
for _, fileHeader := range params.Files[name] {
file, err := fileHeader.Open()
if err == nil {
return file
}
WARN.Println("Failed to open uploaded file", name, ":", err)
}
return nil
}
func bindFile(params *Params, name string, typ reflect.Type) reflect.Value {
reader := getMultipartFile(params, name)
if reader == nil {
return reflect.Zero(typ)
}
// If it's already stored in a temp file, just return that.
if osFile, ok := reader.(*os.File); ok {
return reflect.ValueOf(osFile)
}
// Otherwise, have to store it.
tmpFile, err := ioutil.TempFile("", "revel-upload")
if err != nil {
WARN.Println("Failed to create a temp file to store upload:", err)
return reflect.Zero(typ)
}
// Register it to be deleted after the request is done.
params.tmpFiles = append(params.tmpFiles, tmpFile)
_, err = io.Copy(tmpFile, reader)
if err != nil {
WARN.Println("Failed to copy upload to temp file:", err)
return reflect.Zero(typ)
}
_, err = tmpFile.Seek(0, 0)
if err != nil {
WARN.Println("Failed to seek to beginning of temp file:", err)
return reflect.Zero(typ)
}
return reflect.ValueOf(tmpFile)
}
func bindByteArray(params *Params, name string, typ reflect.Type) reflect.Value {
if reader := getMultipartFile(params, name); reader != nil {
b, err := ioutil.ReadAll(reader)
if err == nil {
return reflect.ValueOf(b)
}
WARN.Println("Error reading uploaded file contents:", err)
}
return reflect.Zero(typ)
}
func bindReadSeeker(params *Params, name string, typ reflect.Type) reflect.Value {
if reader := getMultipartFile(params, name); reader != nil {
return reflect.ValueOf(reader.(io.ReadSeeker))
}
return reflect.Zero(typ)
}
// bindMap converts parameters using map syntax into the corresponding map. e.g.:
// params["a[5]"]=foo, name="a", typ=map[int]string => map[int]string{5: "foo"}
func bindMap(params *Params, name string, typ reflect.Type) reflect.Value {
var (
keyType = typ.Key()
valueType = typ.Elem()
resultPtr = reflect.New(reflect.MapOf(keyType, valueType))
result = resultPtr.Elem()
)
result.Set(reflect.MakeMap(typ))
if params.JSON != nil {
// Try to inject the response as a json into the created result
if err := json.Unmarshal(params.JSON, resultPtr.Interface()); err != nil {
WARN.Println("W: bindMap: Unable to unmarshal request:", name, err)
}
return result
}
for paramName, values := range params.Values {
if !strings.HasPrefix(paramName, name+"[") || paramName[len(paramName)-1] != ']' {
continue
}
key := paramName[len(name)+1 : len(paramName)-1]
result.SetMapIndex(BindValue(key, keyType), BindValue(values[0], valueType))
}
return result
}
func unbindMap(output map[string]string, name string, iface interface{}) {
mapValue := reflect.ValueOf(iface)
for _, key := range mapValue.MapKeys() {
Unbind(output, name+"["+fmt.Sprintf("%v", key.Interface())+"]",
mapValue.MapIndex(key).Interface())
}
}
// Bind takes the name and type of the desired parameter and constructs it
// from one or more values from Params.
// Returns the zero value of the type upon any sort of failure.
func Bind(params *Params, name string, typ reflect.Type) reflect.Value {
if binder, found := binderForType(typ); found {
return binder.Bind(params, name, typ)
}
return reflect.Zero(typ)
}
func BindValue(val string, typ reflect.Type) reflect.Value {
return Bind(&Params{Values: map[string][]string{"": {val}}}, "", typ)
}
func BindFile(fileHeader *multipart.FileHeader, typ reflect.Type) reflect.Value {
return Bind(&Params{Files: map[string][]*multipart.FileHeader{"": {fileHeader}}}, "", typ)
}
func Unbind(output map[string]string, name string, val interface{}) {
if binder, found := binderForType(reflect.TypeOf(val)); found {
if binder.Unbind != nil {
binder.Unbind(output, name, val)
} else {
ERROR.Printf("revel/binder: can not unbind %s=%s", name, val)
}
}
}
func binderForType(typ reflect.Type) (Binder, bool) {
binder, ok := TypeBinders[typ]
if !ok {
binder, ok = KindBinders[typ.Kind()]
if !ok {
WARN.Println("revel/binder: no binder for type:", typ)
return Binder{}, false
}
}
return binder, true
}
// Sadly, the binder lookups can not be declared initialized -- that results in
// an "initialization loop" compile error.
func init() {
KindBinders[reflect.Int] = IntBinder
KindBinders[reflect.Int8] = IntBinder
KindBinders[reflect.Int16] = IntBinder
KindBinders[reflect.Int32] = IntBinder
KindBinders[reflect.Int64] = IntBinder
KindBinders[reflect.Uint] = UintBinder
KindBinders[reflect.Uint8] = UintBinder
KindBinders[reflect.Uint16] = UintBinder
KindBinders[reflect.Uint32] = UintBinder
KindBinders[reflect.Uint64] = UintBinder
KindBinders[reflect.Float32] = FloatBinder
KindBinders[reflect.Float64] = FloatBinder
KindBinders[reflect.String] = StringBinder
KindBinders[reflect.Bool] = BoolBinder
KindBinders[reflect.Slice] = Binder{bindSlice, unbindSlice}
KindBinders[reflect.Struct] = Binder{bindStruct, unbindStruct}
KindBinders[reflect.Ptr] = PointerBinder
KindBinders[reflect.Map] = MapBinder
TypeBinders[reflect.TypeOf(time.Time{})] = TimeBinder
// Uploads
TypeBinders[reflect.TypeOf(&os.File{})] = Binder{bindFile, nil}
TypeBinders[reflect.TypeOf([]byte{})] = Binder{bindByteArray, nil}
TypeBinders[reflect.TypeOf((*io.Reader)(nil)).Elem()] = Binder{bindReadSeeker, nil}
TypeBinders[reflect.TypeOf((*io.ReadSeeker)(nil)).Elem()] = Binder{bindReadSeeker, nil}
OnAppStart(func() {
DateTimeFormat = Config.StringDefault("format.datetime", DefaultDateTimeFormat)
DateFormat = Config.StringDefault("format.date", DefaultDateFormat)
TimeFormats = append(TimeFormats, DateTimeFormat, DateFormat)
})
}