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

fix: WithoutDereference should respect non-nil struct pointers #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -13,6 +13,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Golang/Intellij
.idea

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

Expand Down
19 changes: 18 additions & 1 deletion issue131_test.go
Expand Up @@ -12,6 +12,11 @@ type foz struct {
C *bool
D *bool
E *bool
F *baz
}

type baz struct {
A *bool
}

func TestIssue131MergeWithOverwriteWithEmptyValue(t *testing.T) {
Expand Down Expand Up @@ -76,13 +81,17 @@ func TestIssue131MergeWithoutDereference(t *testing.T) {
C: nil,
D: func(v bool) *bool { return &v }(false),
E: func(v bool) *bool { return &v }(true),
F: &baz{
A: func(v bool) *bool { return &v }(true),
},
}
dest := foz{
A: func(v bool) *bool { return &v }(true),
B: "dest",
C: func(v bool) *bool { return &v }(false),
D: nil,
E: func(v bool) *bool { return &v }(false),
F: nil,
}
if err := mergo.Merge(&dest, src, mergo.WithoutDereference); err != nil {
t.Error(err)
Expand All @@ -100,6 +109,14 @@ func TestIssue131MergeWithoutDereference(t *testing.T) {
t.Errorf("dest.D not merged in properly: %v != %v", src.D, *dest.D)
}
if *dest.E == true {
t.Errorf("dest.Eshould not have been merged: %v == %v", *src.E, *dest.E)
t.Errorf("dest.E should not have been merged: %v == %v", *src.E, *dest.E)
}

if dest.F == nil {
t.Errorf("dest.F should not have be overriden with nil: %v == %v", src.F, dest.F)
}

if *dest.F.A == false {
t.Errorf("dest.F.A not merged in properly: %v != %v", *src.F.A, *dest.F.A)
}
}
2 changes: 1 addition & 1 deletion merge.go
Expand Up @@ -269,7 +269,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
return
}
} else {
} else if src.Elem().Kind() != reflect.Struct {
if overwriteWithEmptySrc || (overwrite && !src.IsNil()) || dst.IsNil() {
dst.Set(src)
}
Expand Down