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

support for pointer fields #3

Merged
merged 2 commits into from
Jun 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions mergo.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int) er
switch dst.Kind() {
case reflect.Struct:
for i, n := 0, dst.NumField(); i < n; i++ {
if err := deepMerge(dst.Field(i), src.Field(i), visited, depth + 1); err != nil {
if err := deepMerge(dst.Field(i), src.Field(i), visited, depth+1); err != nil {
return err
}
}
Expand All @@ -87,20 +87,28 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int) er
case reflect.Struct:
fallthrough
case reflect.Map:
if err := deepMerge(dstElement, srcElement, visited, depth + 1); err != nil {
if err := deepMerge(dstElement, srcElement, visited, depth+1); err != nil {
return err
}
}
if !dstElement.IsValid() {
dst.SetMapIndex(key, srcElement)
}
}
case reflect.Ptr:
fallthrough
case reflect.Interface:
if err := deepMerge(dst.Elem(), src.Elem(), visited, depth + 1); err != nil {
if src.IsNil() {
break
} else if dst.IsNil() {
if dst.CanSet() && isEmptyValue(dst) {
dst.Set(src)
}
} else if err := deepMerge(dst.Elem(), src.Elem(), visited, depth+1); err != nil {
return err
}
default:
if dst.CanSet() && isEmptyValue(dst) {
if dst.CanSet() && !isEmptyValue(src) {
dst.Set(src)
}
}
Expand Down
66 changes: 62 additions & 4 deletions mergo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
package mergo

import (
"io/ioutil"
"launchpad.net/goyaml"
"reflect"
"testing"
"launchpad.net/goyaml"
"io/ioutil"
)

type simpleTest struct {
Expand All @@ -22,6 +22,14 @@ type complexTest struct {
Id string
}

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 @@ -63,8 +71,58 @@ 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)
}
}

func TestPointerStruct(t *testing.T) {
s1 := simpleTest{}
s2 := simpleTest{19}
a := pointerTest{&s1}
b := pointerTest{&s2}
if err := Merge(&a, b); err != nil {
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)
}
}

func TestPointerStructNil(t *testing.T) {
a := pointerTest{nil}
b := pointerTest{&simpleTest{19}}
if err := Merge(&a, b); err != nil {
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)
}
}

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))
}
}

Expand Down