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

Test case for #167 #168

Closed
wants to merge 1 commit into from
Closed
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
64 changes: 64 additions & 0 deletions issue167_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mergo_test

import (
"fmt"
"reflect"
"testing"

"github.com/imdario/mergo"
)

// This test has been stripped to minimally reproduce the core issue of #167. Consider the playground link for an in-depth analysis.

type Issue167A struct{}

type Issue167InMap struct {
As []*Issue167A
}

type Issue167ValueMap struct {
InMap map[string]Issue167InMap
}

type Issue167Transformer struct{}

func (t Issue167Transformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
switch typ {
case reflect.TypeOf([]*Issue167A{}):
return t.transform
}

return nil
}

func (t Issue167Transformer) transform(dst, src reflect.Value) error {
if !dst.CanSet() {
return fmt.Errorf("dst.CanSet() is false, so we're unable to Set() the new value later")
}

dstReal := dst.Interface().([]*Issue167A)
dst.Set(reflect.ValueOf(dstReal))

return nil
}

var Issue167TestData = []struct {
Dst Issue167ValueMap
Src Issue167ValueMap
}{
{
// Each As must have at least an element
Dst: Issue167ValueMap{InMap: map[string]Issue167InMap{"foo": {As: []*Issue167A{{}}}}},
Src: Issue167ValueMap{InMap: map[string]Issue167InMap{"foo": {As: []*Issue167A{{}}}}},
},
}

func TestIssue167MapWithValue(t *testing.T) {
// And additionally should not panic with every test case
for _, data := range Issue167TestData {
err := mergo.Merge(&data.Dst, data.Src, mergo.WithTransformers(Issue167Transformer{}))
if err != nil {
t.Errorf("Error while merging %s", err)
}
}
}