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

add support for Struct in Map #105

Merged
merged 21 commits into from
Mar 24, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add unit test for issue 90 and 103
svyotov committed Jan 12, 2020
commit 71f1eca902063d11c0c45150433cccb2b9ccef30
55 changes: 55 additions & 0 deletions issue90_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mergo

import (
"testing"

"github.com/magiconair/properties/assert"
svyotov marked this conversation as resolved.
Show resolved Hide resolved
)

func TestMergoSimpleMap(t *testing.T) {
svyotov marked this conversation as resolved.
Show resolved Hide resolved
dst := map[string]string{"key1": "loosethis", "key2": "keepthis"}
src := map[string]string{"key1": "key10"}
exp := map[string]string{"key1": "key10", "key2": "keepthis"}
Merge(&dst, src, WithAppendSlice, WithOverride)
svyotov marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, dst, exp)
}

type CustomStruct struct {
SomeMap map[string]string
}

var testDataStructMap = []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"}}}},
}

func TestMergoStructMap(t *testing.T) {

for _, data := range testDataStructMap {
dst := data.dst
src := data.src
exp := data.exp

Merge(&dst, src, WithAppendSlice, WithOverride)
svyotov marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, dst, exp)
}
}