Permalink
Browse files

Move special convert cases up

This is needed for types which are aliases to primitive
types because otherwise they are simply caught in
the main switch. time.Duration is an example of this.
  • Loading branch information...
1 parent b8c92b2 commit 561c010cf140bae84891a4f2d41d3ff335fbae2c Jesse van den Kieboom committed Sep 4, 2012
Showing with 11 additions and 13 deletions.
  1. +11 −13 convert.go
View
@@ -77,6 +77,17 @@ func convertToString(val reflect.Value, options reflect.StructTag) string {
func convert(val string, retval reflect.Value, options reflect.StructTag) error {
tp := retval.Type()
+ // Support for time.Duration
+ if tp == reflect.TypeOf((*time.Duration)(nil)).Elem() {
+ parsed, err := time.ParseDuration(val)
+
+ if err != nil {
+ return err
+ }
+
+ retval.SetInt(int64(parsed))
+ }
+
switch tp.Kind() {
case reflect.String:
retval.SetString(val)
@@ -168,19 +179,6 @@ func convert(val string, retval reflect.Value, options reflect.StructTag) error
retval.SetMapIndex(reflect.Indirect(keyval), reflect.Indirect(valueval))
}
- // Special cases
-
- // Support for time.Duration
- if tp == reflect.TypeOf((*time.Duration)(nil)).Elem() {
- parsed, err := time.ParseDuration(val)
-
- if err != nil {
- return err
- }
-
- retval.SetInt(int64(parsed))
- }
-
return nil
}

0 comments on commit 561c010

Please sign in to comment.