Skip to content

Commit

Permalink
Fields don't need to be anonymous to support "squash"
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 13, 2015
1 parent bb4fbaf commit 6282b1c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
26 changes: 13 additions & 13 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,22 +617,22 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
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
Expand Down
22 changes: 22 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Basic struct {
Vdata interface{}
}

type BasicSquash struct {
Test Basic `mapstructure:",squash"`
}

type Embedded struct {
Basic
Vunique string
Expand Down Expand Up @@ -181,6 +185,24 @@ func TestBasic_Merge(t *testing.T) {
}
}

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()

Expand Down

0 comments on commit 6282b1c

Please sign in to comment.