Skip to content

Commit

Permalink
Merge pull request #251 from joshkaplinsky/joshkaplinsky/without-dere…
Browse files Browse the repository at this point in the history
…ference-should-respect-struct

fix: `WithoutDereference` should respect non-nil struct pointers
  • Loading branch information
darccio authored Aug 17, 2024
2 parents 96f24af + f33862a commit 59ea6a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 59ea6a9

Please sign in to comment.