Embedded non-structs are supported (such as slices)
diff --git a/mapstructure.go b/mapstructure.go index d1cb607..b0ab89b 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -651,14 +651,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), ",")
diff --git a/mapstructure_test.go b/mapstructure_test.go index cc1fffb..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) } }