-
-
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.
Merge pull request #105 from svyotov/fix-panic-on-merge
add support for Struct in Map
- Loading branch information
Showing
5 changed files
with
209 additions
and
117 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
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,50 @@ | ||
package mergo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type CustomStruct struct { | ||
SomeMap map[string]string | ||
} | ||
|
||
func TestMergoStructMap(t *testing.T) { | ||
var testData = []struct { | ||
name string | ||
src map[string]CustomStruct | ||
dst map[string]CustomStruct | ||
exp map[string]CustomStruct | ||
}{ | ||
{name: "Normal", | ||
dst: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "loosethis", "key2": "keepthis"}}}, | ||
src: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
exp: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10", "key2": "keepthis"}}}, | ||
}, | ||
{name: "Init of struct key", dst: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{}}}, | ||
src: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
exp: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
}, | ||
{name: "Not Init of struct key", dst: map[string]CustomStruct{}, | ||
src: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
exp: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
}, | ||
{name: "Nil struct key", dst: map[string]CustomStruct{"a": CustomStruct{SomeMap: nil}}, | ||
src: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}, | ||
exp: map[string]CustomStruct{"a": CustomStruct{SomeMap: map[string]string{"key1": "key10"}}}}, | ||
} | ||
|
||
for _, data := range testData { | ||
dst := data.dst | ||
src := data.src | ||
exp := data.exp | ||
|
||
err := Merge(&dst, src, WithAppendSlice, WithOverride) | ||
if err != nil { | ||
t.Errorf("mergo error was not nil, %v", err) | ||
} | ||
if !reflect.DeepEqual(dst, exp) { | ||
t.Errorf("Actual: %#v did not match \nExpected: %#v", dst, exp) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.