Skip to content

Commit

Permalink
changed merge semantics:
Browse files Browse the repository at this point in the history
if an atomic value exists in the dst but it also exists in the src it will be overwritten.
  • Loading branch information
edofic committed Apr 30, 2014
1 parent 08bd59d commit 47edc31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mergo.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int) er
return err
}
default:
if dst.CanSet() && isEmptyValue(dst) {
if dst.CanSet() && !isEmptyValue(src) {
dst.Set(src)
}
}
Expand Down
34 changes: 32 additions & 2 deletions mergo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type pointerTest struct {
C *simpleTest
}

type sliceTest struct {
S []int
}

func TestNil(t *testing.T) {
if err := Merge(nil, nil); err != NilArgumentsErr {
t.Fail()
Expand Down Expand Up @@ -67,8 +71,8 @@ func TestComplexStruct(t *testing.T) {
if a.sz == 1 {
t.Fatalf("a's private field sz not preserved from merge: a.sz(%d) == b.sz(%d)", a.sz, b.sz)
}
if a.Id == b.Id {
t.Fatalf("a's field Id not preserved from merge: a.Id(%s) == b.Id(%s)", a.Id, b.Id)
if a.Id != b.Id {
t.Fatalf("a's field Id not merged properly: a.Id(%s) != b.Id(%s)", a.Id, b.Id)
}
}

Expand Down Expand Up @@ -96,6 +100,32 @@ func TestPointerStructNil(t *testing.T) {
}
}

func TestSliceStruct(t *testing.T) {
a := sliceTest{}
b := sliceTest{[]int{1, 2, 3}}
if err := Merge(&a, b); err != nil {
t.FailNow()
}
if len(b.S) != 3 {
t.FailNow()
}
if len(a.S) != len(b.S) {
t.Fatalf("b not merged in a properly %d != %d", len(a.S), len(b.S))
}

a = sliceTest{[]int{1}}
b = sliceTest{[]int{1, 2, 3}}
if err := Merge(&a, b); err != nil {
t.FailNow()
}
if len(b.S) != 3 {
t.FailNow()
}
if len(a.S) != len(b.S) {
t.Fatalf("b not merged in a properly %d != %d", len(a.S), len(b.S))
}
}

func TestMaps(t *testing.T) {
m := map[string]simpleTest{
"a": simpleTest{},
Expand Down

0 comments on commit 47edc31

Please sign in to comment.