Merge pull request #61 from gernoteger/#48
fix for #48: Zero pointer to struct fields, even if ZeroFields:false
diff --git a/mapstructure.go b/mapstructure.go
index 81b0c01..b0ab9a3 100644
--- a/mapstructure.go
+++ b/mapstructure.go
@@ -1,5 +1,5 @@
// The mapstructure package exposes functionality to convert an
-// abitrary map[string]interface{} into a native Go structure.
+// arbitrary map[string]interface{} into a native Go structure.
//
// The Go structure can be arbitrarily complex, containing slices,
// other structs, etc. and the decoder will properly decode nested
@@ -660,14 +660,6 @@
fieldType := structType.Field(i)
fieldKind := fieldType.Type.Kind()
- if fieldType.Anonymous {
- if fieldKind != reflect.Struct {
- errors = appendErrors(errors,
- fmt.Errorf("%s: unsupported type: %s", fieldType.Name, fieldKind))
- continue
- }
- }
-
// If "squash" is specified in the tag, we squash the field down.
squash := false
tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
@@ -707,7 +699,7 @@
if !rawMapVal.IsValid() {
// Do a slower search by iterating over each key and
// doing case-insensitive search.
- for dataValKey, _ := range dataValKeys {
+ for dataValKey := range dataValKeys {
mK, ok := dataValKey.Interface().(string)
if !ok {
// Not a string key
@@ -755,7 +747,7 @@
if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
keys := make([]string, 0, len(dataValKeysUnused))
- for rawKey, _ := range dataValKeysUnused {
+ for rawKey := range dataValKeysUnused {
keys = append(keys, rawKey.(string))
}
sort.Strings(keys)
@@ -770,7 +762,7 @@
// Add the unused keys to the list of unused keys if we're tracking metadata
if d.config.Metadata != nil {
- for rawKey, _ := range dataValKeysUnused {
+ for rawKey := range dataValKeysUnused {
key := rawKey.(string)
if name != "" {
key = fmt.Sprintf("%s.%s", name, key)
diff --git a/mapstructure_test.go b/mapstructure_test.go
index ea21924..f949572 100644
--- a/mapstructure_test.go
+++ b/mapstructure_test.go
@@ -42,6 +42,13 @@
Vunique string
}
+type SliceAlias []string
+
+type EmbeddedSlice struct {
+ SliceAlias `mapstructure:"slice_alias"`
+ Vunique string
+}
+
type SquashOnNonStructType struct {
InvalidSquashType int `mapstructure:",squash"`
}
@@ -271,8 +278,41 @@
var result EmbeddedPointer
err := Decode(input, &result)
- if err == nil {
- t.Fatal("should get error")
+ if err != nil {
+ t.Fatalf("err: %s", err)
+ }
+
+ expected := EmbeddedPointer{
+ Basic: &Basic{
+ Vstring: "innerfoo",
+ },
+ Vunique: "bar",
+ }
+ if !reflect.DeepEqual(result, expected) {
+ t.Fatalf("bad: %#v", result)
+ }
+}
+
+func TestDecode_EmbeddedSlice(t *testing.T) {
+ t.Parallel()
+
+ input := map[string]interface{}{
+ "slice_alias": []string{"foo", "bar"},
+ "vunique": "bar",
+ }
+
+ var result EmbeddedSlice
+ err := Decode(input, &result)
+ if err != nil {
+ t.Fatalf("got an err: %s", err.Error())
+ }
+
+ if !reflect.DeepEqual(result.SliceAlias, SliceAlias([]string{"foo", "bar"})) {
+ t.Errorf("slice value: %#v", result.SliceAlias)
+ }
+
+ if result.Vunique != "bar" {
+ t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
@@ -856,10 +896,10 @@
t.Parallel()
input := []map[string]interface{}{
- map[string]interface{}{
+ {
"foo": "bar",
},
- map[string]interface{}{
+ {
"bar": "baz",
},
}