Skip to content

Commit

Permalink
add test for #187's working case
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 7, 2020
1 parent 20e21c6 commit 5ffcd79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mapstructure.go
Expand Up @@ -847,21 +847,21 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
// Determine the name of the key in the map
if index := strings.Index(tagValue, ","); index != -1 {
if tagValue[:index] == "-" {
continue;
continue
}
// If "omitempty" is specified in the tag, it ignores empty values.
if strings.Index(tagValue[index + 1:], "omitempty") != -1 && isEmptyValue(v) {
if strings.Index(tagValue[index+1:], "omitempty") != -1 && isEmptyValue(v) {
continue
}

// If "squash" is specified in the tag, we squash the field down.
squash = !squash && strings.Index(tagValue[index + 1:], "squash") != -1
squash = !squash && strings.Index(tagValue[index+1:], "squash") != -1
if squash && v.Kind() != reflect.Struct {
return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
}
keyName = tagValue[:index]
} else if len(tagValue) > 0 {
if tagValue == "-" {
if tagValue == "-" {
continue
}
keyName = tagValue
Expand Down
21 changes: 21 additions & 0 deletions mapstructure_test.go
Expand Up @@ -354,6 +354,27 @@ func TestBasic_Struct(t *testing.T) {
}
}

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

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

var iface interface{} = &Basic{}
err := Decode(input, &iface)
if err != nil {
t.Fatalf("got an err: %s", err)
}

expected := &Basic{
Vstring: "foo",
}
if !reflect.DeepEqual(iface, expected) {
t.Fatalf("bad: %#v", iface)
}
}

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

Expand Down

0 comments on commit 5ffcd79

Please sign in to comment.