Skip to content

Commit

Permalink
Merge working as designed (don't override non zero values on destinat…
Browse files Browse the repository at this point in the history
…ion).
  • Loading branch information
darccio committed Apr 6, 2015
1 parent 9f1127a commit d304790
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, ov
return
}
}
if !isEmptyValue(srcElement) && (overwrite || !dstElement.IsValid()) {
if !isEmptyValue(srcElement) && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
dst.SetMapIndex(key, srcElement)
}
}
Expand All @@ -64,14 +64,14 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, ov
if src.IsNil() {
break
} else if dst.IsNil() {
if dst.CanSet() && (isEmptyValue(dst) || overwrite) {
if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
dst.Set(src)
}
} else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, overwrite); err != nil {
return
}
default:
if dst.CanSet() && !isEmptyValue(src) {
if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
dst.Set(src)
}
}
Expand Down
44 changes: 22 additions & 22 deletions mergo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestKb(t *testing.T) {
b.KeyValue = bkv

ekv := make(map[string]interface{})
ekv["Key1"] = "not value 1"
ekv["Key1"] = "value1"
ekv["Key2"] = "value2"
ekv["Key3"] = "value3"
expected := testStruct{}
expected.Name = "A"
expected.Name = "B"
expected.KeyValue = ekv

Merge(&b, a)
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestSimpleStruct(t *testing.T) {
t.FailNow()
}
if a.Value != 42 {
t.Fatalf("b not merged in a properly: a.Value(%d) != b.Value(%d)", a.Value, b.Value)
t.Fatalf("b not merged in properly: a.Value(%d) != b.Value(%d)", a.Value, b.Value)
}
if !reflect.DeepEqual(a, b) {
t.FailNow()
Expand All @@ -108,13 +108,13 @@ func TestComplexStruct(t *testing.T) {
t.FailNow()
}
if a.St.Value != 42 {
t.Fatalf("b not merged in a properly: a.St.Value(%d) != b.St.Value(%d)", a.St.Value, b.St.Value)
t.Fatalf("b not merged in properly: a.St.Value(%d) != b.St.Value(%d)", a.St.Value, b.St.Value)
}
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 merged properly: a.Id(%s) != b.Id(%s)", a.Id, b.Id)
if a.Id == b.Id {
t.Fatalf("a's field Id merged unexpectedly: a.Id(%s) == b.Id(%s)", a.Id, b.Id)
}
}

Expand All @@ -141,7 +141,7 @@ func TestPointerStruct(t *testing.T) {
t.FailNow()
}
if a.C.Value != b.C.Value {
//t.Fatalf("b not merged in a properly: a.C.Value(%d) != b.C.Value(%d)", a.C.Value, b.C.Value)
t.Fatalf("b not merged in properly: a.C.Value(%d) != b.C.Value(%d)", a.C.Value, b.C.Value)
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func TestEmbeddedStruct(t *testing.T) {
embeddedStruct{"bar"},
},
expected: embeddingStruct{
embeddedStruct{"foo"},
embeddedStruct{"bar"},
},
},
}
Expand Down Expand Up @@ -227,19 +227,19 @@ func TestSliceStruct(t *testing.T) {
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))
t.Fatalf("b not merged in a proper way %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 {
if len(a.S) != 1 {
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))
if len(a.S) == len(b.S) {
t.Fatalf("b merged unexpectedly %d != %d", len(a.S), len(b.S))
}
}

Expand Down Expand Up @@ -363,19 +363,19 @@ func TestMap(t *testing.T) {
o := b["st"].(*simpleTest)
p := b["nt"].(simpleTest)
if c.Ct.St.Value != 42 {
t.Fatalf("b not merged in a properly: c.Ct.St.Value(%d) != b.Ct.St.Value(%d)", c.Ct.St.Value, n["value"])
t.Fatalf("b not merged in properly: c.Ct.St.Value(%d) != b.Ct.St.Value(%d)", c.Ct.St.Value, n["value"])
}
if c.St.Value != 144 {
t.Fatalf("b not merged in a properly: c.St.Value(%d) != b.St.Value(%d)", c.St.Value, o.Value)
t.Fatalf("b not merged in properly: c.St.Value(%d) != b.St.Value(%d)", c.St.Value, o.Value)
}
if c.Nt.Value != 3 {
t.Fatalf("b not merged in a properly: c.Nt.Value(%d) != b.Nt.Value(%d)", c.St.Value, p.Value)
t.Fatalf("b not merged in properly: c.Nt.Value(%d) != b.Nt.Value(%d)", c.St.Value, p.Value)
}
if c.Ct.sz == 1 {
t.Fatalf("a's private field sz not preserved from merge: c.Ct.sz(%d) == b.Ct.sz(%d)", c.Ct.sz, m["sz"])
}
if c.Ct.Id != m["id"] {
t.Fatalf("a's field Id not merged properly: c.Ct.Id(%s) != b.Ct.Id(%s)", c.Ct.Id, m["id"])
if c.Ct.Id == m["id"] {
t.Fatalf("a's field Id merged unexpectedly: c.Ct.Id(%s) == b.Ct.Id(%s)", c.Ct.Id, m["id"])
}
}

Expand All @@ -388,7 +388,7 @@ func TestSimpleMap(t *testing.T) {
t.FailNow()
}
if a.Value != 42 {
t.Fatalf("b not merged in a properly: a.Value(%d) != b.Value(%v)", a.Value, b["value"])
t.Fatalf("b not merged in properly: a.Value(%d) != b.Value(%v)", a.Value, b["value"])
}
}

Expand All @@ -409,10 +409,10 @@ func TestBackAndForth(t *testing.T) {
ok bool
)
if v, ok = m["a"]; v.(int) != pt.A || !ok {
t.Fatalf("pt not merged properly: m[`a`](%d) != pt.A(%d)", v, pt.A)
t.Fatalf("pt not merged in properly: m[`a`](%d) != pt.A(%d)", v, pt.A)
}
if v, ok = m["b"]; !ok {
t.Fatalf("pt not merged properly: B is missing in m")
t.Fatalf("pt not merged in properly: B is missing in m")
}
var st *simpleTest
if st = v.(*simpleTest); st.Value != 66 {
Expand All @@ -423,13 +423,13 @@ func TestBackAndForth(t *testing.T) {
t.Fatal(err)
}
if bpt.A != pt.A {
t.Fatalf("pt not merged properly: bpt.A(%d) != pt.A(%d)", bpt.A, pt.A)
t.Fatalf("pt not merged in properly: bpt.A(%d) != pt.A(%d)", bpt.A, pt.A)
}
if bpt.hidden == pt.hidden {
t.Fatalf("pt unexpectedly merged: bpt.hidden(%d) == pt.hidden(%d)", bpt.hidden, pt.hidden)
}
if bpt.B.Value != pt.B.Value {
t.Fatalf("pt not merged properly: bpt.B.Value(%d) != pt.B.Value(%d)", bpt.B.Value, pt.B.Value)
t.Fatalf("pt not merged in properly: bpt.B.Value(%d) != pt.B.Value(%d)", bpt.B.Value, pt.B.Value)
}
}

Expand Down

0 comments on commit d304790

Please sign in to comment.