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

Add configuration option to specify tag value that indicates squash #18

Merged
merged 1 commit into from
Aug 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ type DecoderConfig struct {
// defaults to "mapstructure"
TagName string

// The option of the value in the tag that indicates a field should
// be squashed. This defaults to "squash".
SquashTagOption string

// IgnoreUntaggedFields ignores all struct fields without explicit
// TagName, comparable to `mapstructure:"-"` as default behaviour.
IgnoreUntaggedFields bool
Expand Down Expand Up @@ -400,6 +404,10 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) {
config.TagName = "mapstructure"
}

if config.SquashTagOption == "" {
config.SquashTagOption = "squash"
}

if config.MatchName == nil {
config.MatchName = strings.EqualFold
}
Expand Down Expand Up @@ -969,7 +977,7 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
}

// If "squash" is specified in the tag, we squash the field down.
squash = squash || strings.Index(tagValue[index+1:], "squash") != -1
squash = squash || strings.Contains(tagValue[index+1:], d.config.SquashTagOption)
if squash {
// When squashing, the embedded type can be a pointer to a struct.
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
Expand Down Expand Up @@ -1356,7 +1364,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
// We always parse the tags cause we're looking for other tags too
tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
for _, tag := range tagParts[1:] {
if tag == "squash" {
if tag == d.config.SquashTagOption {
squash = true
break
}
Expand Down
60 changes: 60 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type BasicSquash struct {
Test Basic `mapstructure:",squash"`
}

type BasicJSONInline struct {
Test Basic `json:",inline"`
}

type Embedded struct {
Basic
Vunique string
Expand Down Expand Up @@ -488,6 +492,62 @@ func TestDecodeFrom_BasicSquash(t *testing.T) {
}
}

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

input := map[string]interface{}{
"vstring": "foo",
}

var result BasicJSONInline
d, err := NewDecoder(&DecoderConfig{TagName: "json", SquashTagOption: "inline", Result: &result})
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}

if err := d.Decode(input); 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 TestDecodeFrom_BasicJSONInline(t *testing.T) {
t.Parallel()

var v interface{}
var ok bool

input := BasicJSONInline{
Test: Basic{
Vstring: "foo",
},
}

var result map[string]interface{}
d, err := NewDecoder(&DecoderConfig{TagName: "json", SquashTagOption: "inline", Result: &result})
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}

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

if _, ok = result["Test"]; ok {
t.Error("test 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)
}
}

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

Expand Down
Loading