From 7c7592023899ce23a058077d57a3ae10a27b2f7e Mon Sep 17 00:00:00 2001 From: abicky Date: Thu, 28 Jan 2021 01:56:34 +0900 Subject: [PATCH] Fix panic that occurs when a struct has a blank field This commit fixes https://github.com/imdario/mergo/issues/174. --- issue174_test.go | 21 +++++++++++++++++++++ merge.go | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 issue174_test.go diff --git a/issue174_test.go b/issue174_test.go new file mode 100644 index 0000000..d06b68b --- /dev/null +++ b/issue174_test.go @@ -0,0 +1,21 @@ +package mergo_test + +import ( + "testing" + + "github.com/imdario/mergo" +) + +type structWithBlankField struct { + _ struct{} + A struct{} +} + +func TestIssue174(t *testing.T) { + dst := structWithBlankField{} + src := structWithBlankField{} + + if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil { + t.Error(err) + } +} diff --git a/merge.go b/merge.go index afa84a1..3f13275 100644 --- a/merge.go +++ b/merge.go @@ -95,7 +95,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co } } } else { - if (isReflectNil(dst) || overwrite) && (!isEmptyValue(src) || overwriteWithEmptySrc) { + if dst.CanSet() && (isReflectNil(dst) || overwrite) && (!isEmptyValue(src) || overwriteWithEmptySrc) { dst.Set(src) } }