Skip to content

Commit

Permalink
Merge pull request #125 from rusdevops/master
Browse files Browse the repository at this point in the history
added WithOverrideEmptySlice config flag
  • Loading branch information
vdemeester committed Oct 3, 2019
2 parents 4c317f2 + bee5429 commit 1afb360
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
37 changes: 37 additions & 0 deletions issue125_test.go
@@ -0,0 +1,37 @@
package mergo

import (
"encoding/json"
"testing"
)

var (
data = `{"FirstSlice":[], "SecondSlice": null}`
)

type settings struct {
FirstSlice []string `json:"FirstSlice"`
SecondSlice []string `json:"SecondSlice"`
}

func TestIssue125MergeWithOverwrite(t *testing.T) {

defaultSettings := settings{
FirstSlice: []string{},
SecondSlice: []string{},
}

var something settings
if err := json.Unmarshal([]byte(data), &something); err != nil {
t.Errorf("Error while Unmarshalling maprequest: %s", err)
}
if err := Merge(&something, defaultSettings, WithOverrideEmptySlice); err != nil {
t.Errorf("Error while merging: %s", err)
}
if something.FirstSlice == nil {
t.Error("Invalid merging first slice")
}
if something.SecondSlice == nil {
t.Error("Invalid merging second slice")
}
}
21 changes: 14 additions & 7 deletions merge.go
Expand Up @@ -26,11 +26,12 @@ func hasExportedField(dst reflect.Value) (exported bool) {
}

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

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

if !src.IsValid() {
Expand Down Expand Up @@ -130,7 +132,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
dstSlice = reflect.ValueOf(dstElement.Interface())
}

if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
if (!isEmptyValue(src) || overwriteWithEmptySrc || overwriteSliceWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
if typeCheck && srcSlice.Type() != dstSlice.Type() {
return fmt.Errorf("cannot override two slices with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
}
Expand Down Expand Up @@ -159,7 +161,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
if !dst.CanSet() {
break
}
if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
if (!isEmptyValue(src) || overwriteWithEmptySrc || overwriteSliceWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
dst.Set(src)
} else if config.AppendSlice {
if src.Type() != dst.Type() {
Expand Down Expand Up @@ -244,6 +246,11 @@ func WithOverride(config *Config) {
config.Overwrite = true
}

// WithOverride will make merge override empty dst slice with empty src slice.
func WithOverrideEmptySlice(config *Config) {
config.overwriteSliceWithEmptyValue = true
}

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

0 comments on commit 1afb360

Please sign in to comment.