Skip to content

Commit

Permalink
WithoutDereference should respect structs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkaplinsky committed Mar 12, 2024
1 parent cde9f0e commit f33862a
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 f33862a

Please sign in to comment.