From f33862a52373a787536aff7428212ce0ecb0cac0 Mon Sep 17 00:00:00 2001 From: Josh Kaplinsky <37640086+joshkaplinsky@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:24:21 -0400 Subject: [PATCH] WithoutDereference should respect structs --- .gitignore | 3 +++ issue131_test.go | 19 ++++++++++++++++++- merge.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 529c341..45ad0f1 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/issue131_test.go b/issue131_test.go index a702063..ff7b43e 100644 --- a/issue131_test.go +++ b/issue131_test.go @@ -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) { @@ -76,6 +81,9 @@ 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), @@ -83,6 +91,7 @@ func TestIssue131MergeWithoutDereference(t *testing.T) { 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) @@ -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) } } diff --git a/merge.go b/merge.go index 0ef9b21..fd47c95 100644 --- a/merge.go +++ b/merge.go @@ -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) }