Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix squash decoder option to squash only embedded fields #194

Merged
merged 1 commit into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions mapstructure.go
Expand Up @@ -843,7 +843,7 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
keyName := f.Name

// If Squash is set in the config, we squash the field down.
squash := d.config.Squash && v.Kind() == reflect.Struct
squash := d.config.Squash && v.Kind() == reflect.Struct && f.Anonymous
// Determine the name of the key in the map
if index := strings.Index(tagValue, ","); index != -1 {
if tagValue[:index] == "-" {
Expand Down Expand Up @@ -1187,7 +1187,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
fieldKind := fieldType.Type.Kind()

// If "squash" is specified in the tag, we squash the field down.
squash := d.config.Squash && fieldKind == reflect.Struct
squash := d.config.Squash && fieldKind == reflect.Struct && fieldType.Anonymous
remain := false

// We always parse the tags cause we're looking for other tags too
Expand Down
71 changes: 70 additions & 1 deletion mapstructure_test.go
Expand Up @@ -61,6 +61,12 @@ type EmbeddedSquash struct {
Vunique string
}

type EmbeddedAndNamed struct {
Basic
Named Basic
Vunique string
}

type SliceAlias []string

type EmbeddedSlice struct {
Expand Down Expand Up @@ -612,9 +618,12 @@ func TestDecode_EmbeddedSquashConfig(t *testing.T) {
input := map[string]interface{}{
"vstring": "foo",
"vunique": "bar",
"Named": map[string]interface{}{
"vstring": "baz",
},
}

var result Embedded
var result EmbeddedAndNamed
config := &DecoderConfig{
Squash: true,
Result: &result,
Expand All @@ -637,6 +646,66 @@ func TestDecode_EmbeddedSquashConfig(t *testing.T) {
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}

if result.Named.Vstring != "baz" {
t.Errorf("Named.vstring value should be 'baz': %#v", result.Named.Vstring)
}
}

func TestDecodeFrom_EmbeddedSquashConfig(t *testing.T) {
t.Parallel()

input := EmbeddedAndNamed{
Basic: Basic{Vstring: "foo"},
Named: Basic{Vstring: "baz"},
Vunique: "bar",
}

result := map[string]interface{}{}
config := &DecoderConfig{
Squash: true,
Result: &result,
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}

err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}

if _, ok := result["Basic"]; ok {
t.Error("basic should not be present in map")
}

v, ok := result["Vstring"]
if !ok {
t.Error("vstring should be present in map")
} else if !reflect.DeepEqual(v, "foo") {
t.Errorf("vstring value should be 'foo': %#v", v)
}

v, ok = result["Vunique"]
if !ok {
t.Error("vunique should be present in map")
} else if !reflect.DeepEqual(v, "bar") {
t.Errorf("vunique value should be 'bar': %#v", v)
}

v, ok = result["Named"]
if !ok {
t.Error("Named should be present in map")
} else {
named := v.(map[string]interface{})
v, ok := named["Vstring"]
if !ok {
t.Error("Named: vstring should be present in map")
} else if !reflect.DeepEqual(v, "baz") {
t.Errorf("Named: vstring should be 'baz': %#v", v)
}
}
}

func TestDecode_SquashOnNonStructType(t *testing.T) {
Expand Down