-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 #170. Consider the playground link for an in-depth analysis. | ||
|
||
type Issue170A struct{} | ||
|
||
type Issue170InMap struct { | ||
As []*Issue170A | ||
} | ||
|
||
type Issue170ValueMap struct { | ||
InMap map[string]Issue170InMap | ||
} | ||
|
||
type Issue170Transformer struct{} | ||
|
||
func (t Issue170Transformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error { | ||
switch typ { | ||
case reflect.TypeOf([]*Issue170A{}): | ||
return t.transform | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t Issue170Transformer) 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().([]*Issue170A) | ||
dst.Set(reflect.ValueOf(dstReal)) | ||
|
||
return nil | ||
} | ||
|
||
var Issue170TestData = []struct { | ||
Dst Issue170ValueMap | ||
Src Issue170ValueMap | ||
}{ | ||
{ | ||
// Each As must have at least an element | ||
Dst: Issue170ValueMap{InMap: map[string]Issue170InMap{"foo": {As: []*Issue170A{{}}}}}, | ||
Src: Issue170ValueMap{InMap: map[string]Issue170InMap{"foo": {As: []*Issue170A{{}}}}}, | ||
}, | ||
} | ||
|
||
func TestIssue170MapWithValue(t *testing.T) { | ||
// And additionally should not panic with every test case | ||
for _, data := range Issue170TestData { | ||
err := mergo.Merge(&data.Dst, data.Src, mergo.WithTransformers(Issue170Transformer{})) | ||
if err != nil { | ||
t.Errorf("Error while merging %s", err) | ||
} | ||
} | ||
} |