Support interface{} as value type in Decode.
diff --git a/decode.go b/decode.go
index cef8c3a..1a16dda 100644
--- a/decode.go
+++ b/decode.go
@@ -26,6 +26,8 @@
// time.Time fields have the vaule of time.Parse() assigned. The default layout
// is time.RFC3339 but can be set in the field's tag.
//
+// interface{} fields have the value assigned as string.
+//
// Arrays and slices of string, boolean, numeric, time.Duration and time.Time
// fields have the value interpreted as a comma separated list of values. The
// individual values are trimmed of whitespace and empty values are ignored. A
@@ -229,6 +231,13 @@
}
v.Set(m)
+ case isInterface(t):
+ val, err := value()
+ if err != nil {
+ return err
+ }
+ v.Set(reflect.ValueOf(val))
+
default:
return fmt.Errorf("unsupported type %s", t)
}
@@ -266,15 +275,16 @@
return key, opts
}
-func isArray(t reflect.Type) bool { return t.Kind() == reflect.Array || t.Kind() == reflect.Slice }
-func isBool(t reflect.Type) bool { return t.Kind() == reflect.Bool }
-func isDuration(t reflect.Type) bool { return t == reflect.TypeOf(time.Second) }
-func isMap(t reflect.Type) bool { return t.Kind() == reflect.Map }
-func isNumeric(t reflect.Type) bool { return isInt(t) || isUint(t) || isFloat(t) }
-func isPtr(t reflect.Type) bool { return t.Kind() == reflect.Ptr }
-func isString(t reflect.Type) bool { return t.Kind() == reflect.String }
-func isStruct(t reflect.Type) bool { return t.Kind() == reflect.Struct }
-func isTime(t reflect.Type) bool { return t == reflect.TypeOf(time.Time{}) }
+func isArray(t reflect.Type) bool { return t.Kind() == reflect.Array || t.Kind() == reflect.Slice }
+func isBool(t reflect.Type) bool { return t.Kind() == reflect.Bool }
+func isDuration(t reflect.Type) bool { return t == reflect.TypeOf(time.Second) }
+func isInterface(t reflect.Type) bool { return t.Kind() == reflect.Interface }
+func isMap(t reflect.Type) bool { return t.Kind() == reflect.Map }
+func isNumeric(t reflect.Type) bool { return isInt(t) || isUint(t) || isFloat(t) }
+func isPtr(t reflect.Type) bool { return t.Kind() == reflect.Ptr }
+func isString(t reflect.Type) bool { return t.Kind() == reflect.String }
+func isStruct(t reflect.Type) bool { return t.Kind() == reflect.Struct }
+func isTime(t reflect.Type) bool { return t == reflect.TypeOf(time.Time{}) }
func isFloat(t reflect.Type) bool {
return t.Kind() == reflect.Float32 || t.Kind() == reflect.Float64
}
diff --git a/decode_test.go b/decode_test.go
index 4115fbb..f7257ee 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -25,6 +25,7 @@
F64 float64
D time.Duration
TM time.Time
+ IF interface{}
}
in := `
S=abc
@@ -44,6 +45,7 @@
F64=6.4
D=5s
TM=2015-01-02T12:34:56Z
+ IF=interface
`
out := &S{
S: "abc",
@@ -63,6 +65,7 @@
F64: 6.4,
D: 5 * time.Second,
TM: tm(t, time.RFC3339, "2015-01-02T12:34:56Z"),
+ IF: "interface",
}
testDecode(t, in, &S{}, out)
}
@@ -86,6 +89,7 @@
F64 float64 `properties:",default=6.4"`
D time.Duration `properties:",default=5s"`
TM time.Time `properties:",default=2015-01-02T12:34:56Z"`
+ IF interface{} `properties:",default=abc"`
}
out := &S{
S: "abc",
@@ -105,6 +109,7 @@
F64: 6.4,
D: 5 * time.Second,
TM: tm(t, time.RFC3339, "2015-01-02T12:34:56Z"),
+ IF: "abc",
}
testDecode(t, "", &S{}, out)
}
@@ -251,6 +256,7 @@
D map[string]S
E map[string]int
F map[string]int `properties:"-"`
+ G map[string]interface{}
}
in := `
A.foo=bar
@@ -262,6 +268,7 @@
C.bar.three=3
C.bar.four=4
D.foo.a=bar
+ G.foo=bar
`
out := &X{
A: map[string]string{"foo": "bar", "bar": "bang"},
@@ -269,6 +276,7 @@
C: map[string]map[string]string{"foo": map[string]string{"one": "1", "two": "2"}, "bar": map[string]string{"three": "3", "four": "4"}},
D: map[string]S{"foo": S{"bar"}},
E: map[string]int{},
+ G: map[string]interface{}{"foo": "bar"},
}
testDecode(t, in, &X{}, out)
}