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

Add WithNoOverrideEmptyStructValues #179

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions emptyStructValues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mergo_test

import (
"testing"

"github.com/imdario/mergo"
)

type parent struct {
S string
Child *child
}

type child struct {
S string
}

func TestDoNotOverwriteEmptyValueWithinStruct(t *testing.T) {
src := parent{
Child: &child{S: "src"},
S: "src",
}
dest := parent{
Child: &child{S: ""},
S: "dest",
}
if err := mergo.Merge(&dest, src, mergo.WithNoOverrideEmptyStructValues); err != nil {
t.Error(err)
}
if dest.Child.S != "" {
t.Errorf("dest.Child.S overwritten")
}
}

func TestOverwriteEmptyStruct(t *testing.T) {
src := parent{
Child: &child{S: "src"},
S: "src",
}
dest := parent{
S: "dest",
}
if err := mergo.Merge(&dest, src, mergo.WithNoOverrideEmptyStructValues); err != nil {
t.Error(err)
}
if dest.Child.S != "src" {
t.Errorf("dest.Child.S not overwritten")
}
}
27 changes: 18 additions & 9 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ func isExportedComponent(field *reflect.StructField) bool {
}

type Config struct {
Overwrite bool
AppendSlice bool
TypeCheck bool
Transformers Transformers
overwriteWithEmptyValue bool
overwriteSliceWithEmptyValue bool
sliceDeepCopy bool
debug bool
Overwrite bool
AppendSlice bool
TypeCheck bool
Transformers Transformers
overwriteWithEmptyValue bool
overwriteSliceWithEmptyValue bool
doNotOverwriteEmptyStructValuesInDst bool
sliceDeepCopy bool
debug bool
}

type Transformers interface {
Expand All @@ -60,6 +61,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
typeCheck := config.TypeCheck
overwriteWithEmptySrc := config.overwriteWithEmptyValue
overwriteSliceWithEmptySrc := config.overwriteSliceWithEmptyValue
doNotOverwriteEmptyStructValuesInDst := config.doNotOverwriteEmptyStructValuesInDst
sliceDeepCopy := config.sliceDeepCopy

if !src.IsValid() {
Expand All @@ -86,9 +88,11 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}
}

overwriteStructValues := depth == 0 || !doNotOverwriteEmptyStructValuesInDst

switch dst.Kind() {
case reflect.Struct:
if hasMergeableFields(dst) {
if overwriteStructValues && hasMergeableFields(dst) {
for i, n := 0, dst.NumField(); i < n; i++ {
if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, config); err != nil {
return
Expand Down Expand Up @@ -321,6 +325,11 @@ func WithOverrideEmptySlice(config *Config) {
config.overwriteSliceWithEmptyValue = true
}

// WithNoOverrideEmptyStructValues will make merge not overwrite the empty values within non-empty structs in dst
func WithNoOverrideEmptyStructValues(config *Config) {
config.doNotOverwriteEmptyStructValuesInDst = true
}

// WithAppendSlice will make merge append slices instead of overwriting it.
func WithAppendSlice(config *Config) {
config.AppendSlice = true
Expand Down