Skip to content

Commit

Permalink
test: add failing test for mitchellhGH-347
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Dec 18, 2023
1 parent 8244341 commit d48a857
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions mapstructure_bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,110 @@ func TestDecode_nonComparableType(t *testing.T) {
t.Fatalf("fail: %#v", decodeTo.NonComparableType)
}
}

// GH-347: Decoding maps with multiple indirection results in an error
func TestDecodeToMapWithMultipleIndirection(t *testing.T) {
t.Run("Struct", func(t *testing.T) {
type Struct struct {
Foo string `mapstructure:"foo"`
}

v := Struct{
Foo: "bar",
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := Decode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Map", func(t *testing.T) {
v := map[string]interface{}{
"foo": "bar",
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := Decode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Array", func(t *testing.T) {
v := [1]map[string]interface{}{
{
"foo": "bar",
},
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := WeakDecode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Slice", func(t *testing.T) {
v := []map[string]interface{}{
{
"foo": "bar",
},
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := WeakDecode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})
}

0 comments on commit d48a857

Please sign in to comment.