1
+ // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2
+ // Revel Framework source code and usage is governed by a MIT style
3
+ // license that can be found in the LICENSE file.
4
+
1
5
package revel
2
6
3
7
import (
@@ -39,7 +43,7 @@ type Binder struct {
39
43
Unbind func (output map [string ]string , name string , val interface {})
40
44
}
41
45
42
- // An adapter for easily making one-key-value binders.
46
+ // ValueBinder is adapter for easily making one-key-value binders.
43
47
func ValueBinder (f func (value string , typ reflect.Type ) reflect.Value ) func (* Params , string , reflect.Type ) reflect.Value {
44
48
return func (params * Params , name string , typ reflect.Type ) reflect.Value {
45
49
vals , ok := params .Values [name ]
@@ -50,11 +54,13 @@ func ValueBinder(f func(value string, typ reflect.Type) reflect.Value) func(*Par
50
54
}
51
55
}
52
56
57
+ // Revel's default date and time constants
53
58
const (
54
- DEFAULT_DATE_FORMAT = "2006-01-02"
55
- DEFAULT_DATETIME_FORMAT = "2006-01-02 15:04"
59
+ DefaultDateFormat = "2006-01-02"
60
+ DefaultDateTimeFormat = "2006-01-02 15:04"
56
61
)
57
62
63
+ // Binders type and kind definition
58
64
var (
59
65
// These are the lookups to find a Binder for any type of data.
60
66
// The most specific binder found will be used (Type before Kind)
@@ -134,19 +140,11 @@ var (
134
140
},
135
141
}
136
142
137
- // Booleans support a couple different value formats:
138
- // "true" and "false"
139
- // "on" and "" (a checkbox)
140
- // "1" and "0" (why not)
143
+ // Booleans support a various value formats,
144
+ // refer `revel.Atob` method.
141
145
BoolBinder = Binder {
142
146
Bind : ValueBinder (func (val string , typ reflect.Type ) reflect.Value {
143
- v := strings .TrimSpace (strings .ToLower (val ))
144
- switch v {
145
- case "true" , "on" , "1" :
146
- return reflect .ValueOf (true )
147
- }
148
- // Return false by default.
149
- return reflect .ValueOf (false )
147
+ return reflect .ValueOf (Atob (val ))
150
148
}),
151
149
Unbind : func (output map [string ]string , name string , val interface {}) {
152
150
output [name ] = fmt .Sprintf ("%t" , val )
@@ -195,46 +193,6 @@ var (
195
193
}
196
194
)
197
195
198
- // Sadly, the binder lookups can not be declared initialized -- that results in
199
- // an "initialization loop" compile error.
200
- func init () {
201
- KindBinders [reflect .Int ] = IntBinder
202
- KindBinders [reflect .Int8 ] = IntBinder
203
- KindBinders [reflect .Int16 ] = IntBinder
204
- KindBinders [reflect .Int32 ] = IntBinder
205
- KindBinders [reflect .Int64 ] = IntBinder
206
-
207
- KindBinders [reflect .Uint ] = UintBinder
208
- KindBinders [reflect .Uint8 ] = UintBinder
209
- KindBinders [reflect .Uint16 ] = UintBinder
210
- KindBinders [reflect .Uint32 ] = UintBinder
211
- KindBinders [reflect .Uint64 ] = UintBinder
212
-
213
- KindBinders [reflect .Float32 ] = FloatBinder
214
- KindBinders [reflect .Float64 ] = FloatBinder
215
-
216
- KindBinders [reflect .String ] = StringBinder
217
- KindBinders [reflect .Bool ] = BoolBinder
218
- KindBinders [reflect .Slice ] = Binder {bindSlice , unbindSlice }
219
- KindBinders [reflect .Struct ] = Binder {bindStruct , unbindStruct }
220
- KindBinders [reflect .Ptr ] = PointerBinder
221
- KindBinders [reflect .Map ] = MapBinder
222
-
223
- TypeBinders [reflect .TypeOf (time.Time {})] = TimeBinder
224
-
225
- // Uploads
226
- TypeBinders [reflect .TypeOf (& os.File {})] = Binder {bindFile , nil }
227
- TypeBinders [reflect .TypeOf ([]byte {})] = Binder {bindByteArray , nil }
228
- TypeBinders [reflect .TypeOf ((* io .Reader )(nil )).Elem ()] = Binder {bindReadSeeker , nil }
229
- TypeBinders [reflect .TypeOf ((* io .ReadSeeker )(nil )).Elem ()] = Binder {bindReadSeeker , nil }
230
-
231
- OnAppStart (func () {
232
- DateTimeFormat = Config .StringDefault ("format.datetime" , DEFAULT_DATETIME_FORMAT )
233
- DateFormat = Config .StringDefault ("format.date" , DEFAULT_DATE_FORMAT )
234
- TimeFormats = append (TimeFormats , DateTimeFormat , DateFormat )
235
- })
236
- }
237
-
238
196
// Used to keep track of the index for individual keyvalues.
239
197
type sliceValue struct {
240
198
index int // Index extracted from brackets. If -1, no index was provided.
@@ -334,7 +292,7 @@ func unbindSlice(output map[string]string, name string, val interface{}) {
334
292
func bindStruct (params * Params , name string , typ reflect.Type ) reflect.Value {
335
293
result := reflect .New (typ ).Elem ()
336
294
fieldValues := make (map [string ]reflect.Value )
337
- for key , _ := range params .Values {
295
+ for key := range params .Values {
338
296
if ! strings .HasPrefix (key , name + "." ) {
339
297
continue
340
298
}
@@ -511,3 +469,43 @@ func binderForType(typ reflect.Type) (Binder, bool) {
511
469
}
512
470
return binder , true
513
471
}
472
+
473
+ // Sadly, the binder lookups can not be declared initialized -- that results in
474
+ // an "initialization loop" compile error.
475
+ func init () {
476
+ KindBinders [reflect .Int ] = IntBinder
477
+ KindBinders [reflect .Int8 ] = IntBinder
478
+ KindBinders [reflect .Int16 ] = IntBinder
479
+ KindBinders [reflect .Int32 ] = IntBinder
480
+ KindBinders [reflect .Int64 ] = IntBinder
481
+
482
+ KindBinders [reflect .Uint ] = UintBinder
483
+ KindBinders [reflect .Uint8 ] = UintBinder
484
+ KindBinders [reflect .Uint16 ] = UintBinder
485
+ KindBinders [reflect .Uint32 ] = UintBinder
486
+ KindBinders [reflect .Uint64 ] = UintBinder
487
+
488
+ KindBinders [reflect .Float32 ] = FloatBinder
489
+ KindBinders [reflect .Float64 ] = FloatBinder
490
+
491
+ KindBinders [reflect .String ] = StringBinder
492
+ KindBinders [reflect .Bool ] = BoolBinder
493
+ KindBinders [reflect .Slice ] = Binder {bindSlice , unbindSlice }
494
+ KindBinders [reflect .Struct ] = Binder {bindStruct , unbindStruct }
495
+ KindBinders [reflect .Ptr ] = PointerBinder
496
+ KindBinders [reflect .Map ] = MapBinder
497
+
498
+ TypeBinders [reflect .TypeOf (time.Time {})] = TimeBinder
499
+
500
+ // Uploads
501
+ TypeBinders [reflect .TypeOf (& os.File {})] = Binder {bindFile , nil }
502
+ TypeBinders [reflect .TypeOf ([]byte {})] = Binder {bindByteArray , nil }
503
+ TypeBinders [reflect .TypeOf ((* io .Reader )(nil )).Elem ()] = Binder {bindReadSeeker , nil }
504
+ TypeBinders [reflect .TypeOf ((* io .ReadSeeker )(nil )).Elem ()] = Binder {bindReadSeeker , nil }
505
+
506
+ OnAppStart (func () {
507
+ DateTimeFormat = Config .StringDefault ("format.datetime" , DefaultDateTimeFormat )
508
+ DateFormat = Config .StringDefault ("format.date" , DefaultDateFormat )
509
+ TimeFormats = append (TimeFormats , DateTimeFormat , DateFormat )
510
+ })
511
+ }
0 commit comments