Fields don't need to be anonymous to support "squash"
diff --git a/mapstructure.go b/mapstructure.go index d3cb4e8..ecc8218 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -617,22 +617,22 @@ fmt.Errorf("%s: unsupported type: %s", fieldType.Name, fieldKind)) continue } + } - // We have an embedded field. We "squash" the fields down - // if specified in the tag. - squash := false - tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",") - for _, tag := range tagParts[1:] { - if tag == "squash" { - squash = true - break - } + // We have an embedded field. We "squash" the fields down + // if specified in the tag. + squash := false + tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",") + for _, tag := range tagParts[1:] { + if tag == "squash" { + squash = true + break } + } - if squash { - structs = append(structs, val.FieldByName(fieldType.Name)) - continue - } + if squash { + structs = append(structs, val.FieldByName(fieldType.Name)) + continue } // Normal struct field, store it away
diff --git a/mapstructure_test.go b/mapstructure_test.go index e1a47db..8a27647 100644 --- a/mapstructure_test.go +++ b/mapstructure_test.go
@@ -17,6 +17,10 @@ Vdata interface{} } +type BasicSquash struct { + Test Basic `mapstructure:",squash"` +} + type Embedded struct { Basic Vunique string @@ -181,6 +185,24 @@ } } +func TestDecode_BasicSquash(t *testing.T) { + t.Parallel() + + input := map[string]interface{}{ + "vstring": "foo", + } + + var result BasicSquash + err := Decode(input, &result) + if err != nil { + t.Fatalf("got an err: %s", err.Error()) + } + + if result.Test.Vstring != "foo" { + t.Errorf("vstring value should be 'foo': %#v", result.Test.Vstring) + } +} + func TestDecode_Embedded(t *testing.T) { t.Parallel()