@@ -28,6 +28,7 @@ import (
28
28
"reflect"
29
29
"strconv"
30
30
"strings"
31
+ "time"
31
32
)
32
33
33
34
// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
@@ -40,6 +41,27 @@ type BindUnmarshaler interface {
40
41
//
41
42
// Notice: tag is the name of the struct tag. such as "form", "query", etc.
42
43
// If the tag value is equal to "-", ignore this field.
44
+ //
45
+ // Support the types of the struct fields as follow:
46
+ // - bool
47
+ // - int
48
+ // - int8
49
+ // - int16
50
+ // - int32
51
+ // - int64
52
+ // - uint
53
+ // - uint8
54
+ // - uint16
55
+ // - uint32
56
+ // - uint64
57
+ // - string
58
+ // - float32
59
+ // - float64
60
+ // - time.Time // use time.Time.UnmarshalText(), so only support RFC3339 format
61
+ // - time.Duration // use time.ParseDuration()
62
+ // - interface { UnmarshalBind(param string) error }
63
+ // And any pointer to the type above.
64
+ //
43
65
func BindURLValues (ptr interface {}, data url.Values , tag string ) error {
44
66
typ := reflect .TypeOf (ptr ).Elem ()
45
67
val := reflect .ValueOf (ptr ).Elem ()
@@ -135,6 +157,13 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
135
157
case reflect .Int32 :
136
158
return setIntField (val , 32 , structField )
137
159
case reflect .Int64 :
160
+ if _ , ok := structField .Interface ().(time.Duration ); ok {
161
+ v , err := time .ParseDuration (val )
162
+ if err == nil {
163
+ structField .SetInt (int64 (v ))
164
+ }
165
+ return err
166
+ }
138
167
return setIntField (val , 64 , structField )
139
168
case reflect .Uint :
140
169
return setUintField (val , 0 , structField )
@@ -155,6 +184,12 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
155
184
case reflect .String :
156
185
structField .SetString (val )
157
186
default :
187
+ if _ , ok := structField .Interface ().(time.Time ); ok {
188
+ if val == "" {
189
+ return nil
190
+ }
191
+ return structField .Addr ().Interface ().(* time.Time ).UnmarshalText ([]byte (val ))
192
+ }
158
193
return errors .New ("unknown type" )
159
194
}
160
195
return nil
@@ -200,8 +235,9 @@ func unmarshalFieldPtr(value string, field reflect.Value) (bool, error) {
200
235
201
236
func setIntField (value string , bitSize int , field reflect.Value ) error {
202
237
if value == "" {
203
- value = "0"
238
+ return nil
204
239
}
240
+
205
241
intVal , err := strconv .ParseInt (value , 10 , bitSize )
206
242
if err == nil {
207
243
field .SetInt (intVal )
@@ -211,8 +247,9 @@ func setIntField(value string, bitSize int, field reflect.Value) error {
211
247
212
248
func setUintField (value string , bitSize int , field reflect.Value ) error {
213
249
if value == "" {
214
- value = "0"
250
+ return nil
215
251
}
252
+
216
253
uintVal , err := strconv .ParseUint (value , 10 , bitSize )
217
254
if err == nil {
218
255
field .SetUint (uintVal )
@@ -222,8 +259,9 @@ func setUintField(value string, bitSize int, field reflect.Value) error {
222
259
223
260
func setBoolField (value string , field reflect.Value ) error {
224
261
if value == "" {
225
- value = "false"
262
+ return nil
226
263
}
264
+
227
265
boolVal , err := strconv .ParseBool (value )
228
266
if err == nil {
229
267
field .SetBool (boolVal )
@@ -233,8 +271,9 @@ func setBoolField(value string, field reflect.Value) error {
233
271
234
272
func setFloatField (value string , bitSize int , field reflect.Value ) error {
235
273
if value == "" {
236
- value = "0.0"
274
+ return nil
237
275
}
276
+
238
277
floatVal , err := strconv .ParseFloat (value , bitSize )
239
278
if err == nil {
240
279
field .SetFloat (floatVal )
0 commit comments