You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
The squash decoder option should squash only embedded structs as documented, but it squashes all struct fields regardless of being embedded or not.
The proposed fix is trivial and was implemented in one of the similar PRs (#134): test for fieldType.Anonymous flag when this option is enabled.
However, this would be a breaking change since some users might rely on the "squash everything" behavior of the option already. I will create a PR with a straightforward fix but not sure if such a breaking change is acceptable in this repo.
Alternatively, the config.Squash option could be complemented with something along the lines of config.SquashEmbedded.
Test case to demonstrate the issue:
package mapstructure_test
import (
"testing""github.com/mitchellh/mapstructure"
)
type (
Configstruct {
Nested// expected to be squashedNamedNested// expected to not be squashed
}
Nestedstruct {
Valuestring
}
)
funcTestDecode_SquashOption(t*testing.T) {
input:=map[string]interface{}{
"Value": "foo",
"Named": map[string]interface{}{
"Value": "bar",
},
}
result:=Config{}
decoder, err:=mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &result,
Squash: true,
})
iferr!=nil {
t.Fatalf("got an err: %s", err.Error())
}
err=decoder.Decode(input)
iferr!=nil {
t.Fatalf("got an err: %s", err.Error())
}
ifresult.Nested.Value!="foo" {
t.Errorf("Nested.Value value should be 'foo': %#v", result.Nested.Value)
}
ifresult.Named.Value!="bar" {
t.Errorf("Named.Value value should be 'bar': %#v", result.Named.Value)
}
}
funcTestDecodeFrom_SquashOption(t*testing.T) {
input:=Config{
Nested: Nested{Value: "foo"},
Named: Nested{Value: "bar"},
}
result:=map[string]interface{}{}
decoder, err:=mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &result,
Squash: true,
})
iferr!=nil {
t.Fatalf("got an err: %s", err.Error())
}
err=decoder.Decode(input)
iferr!=nil {
t.Fatalf("got an err: %s", err.Error())
}
ifv, ok:=result["Value"]; !ok {
t.Error("Value (from an embedded field) should be present in map")
} elseifv!="foo" {
t.Errorf("Value (from an embedded field) should be 'foo': %#v", v)
}
ifv, ok:=result["Named"]; !ok {
t.Error("Named field should be present in map")
} else {
named:=v.(map[string]interface{})
ifv, ok:=named["Value"]; !ok {
t.Error("Named: Value should be present in map")
} elseifv!="bar" {
t.Errorf("Named: Value should be 'bar': %#v", v)
}
}
}
The text was updated successfully, but these errors were encountered:
The squash decoder option should squash only embedded structs as documented, but it squashes all struct fields regardless of being embedded or not.
The proposed fix is trivial and was implemented in one of the similar PRs (#134): test for
fieldType.Anonymous
flag when this option is enabled.However, this would be a breaking change since some users might rely on the "squash everything" behavior of the option already. I will create a PR with a straightforward fix but not sure if such a breaking change is acceptable in this repo.
Alternatively, the
config.Squash
option could be complemented with something along the lines ofconfig.SquashEmbedded
.Test case to demonstrate the issue:
The text was updated successfully, but these errors were encountered: