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 strict override #112

Merged
merged 2 commits into from
Apr 15, 2019
Merged
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
14 changes: 12 additions & 2 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func hasExportedField(dst reflect.Value) (exported bool) {
type Config struct {
Overwrite bool
AppendSlice bool
TypeCheck bool
Transformers Transformers
overwriteWithEmptyValue bool
}
Expand All @@ -41,6 +42,7 @@ type Transformers interface {
// short circuiting on recursive types.
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
overwrite := config.Overwrite
typeCheck := config.TypeCheck
overwriteWithEmptySrc := config.overwriteWithEmptyValue
config.overwriteWithEmptyValue = false

Expand Down Expand Up @@ -129,10 +131,13 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}

if (!isEmptyValue(src) || overwriteWithEmptySrc) && (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())
}
dstSlice = srcSlice
} else if config.AppendSlice {
if srcSlice.Type() != dstSlice.Type() {
return fmt.Errorf("cannot append two slice with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
return fmt.Errorf("cannot append two slices with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
}
dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
}
Expand Down Expand Up @@ -228,11 +233,16 @@ func WithOverride(config *Config) {
config.Overwrite = true
}

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

// WithTypeCheck will make merge check types while overwriting it (must be used with WithOverride).
func WithTypeCheck(config *Config) {
config.TypeCheck = true
}

func merge(dst, src interface{}, opts ...func(*Config)) error {
var (
vDst, vSrc reflect.Value
Expand Down
41 changes: 31 additions & 10 deletions mergo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package mergo
import (
"io/ioutil"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -734,23 +735,43 @@ func TestBooleanPointer(t *testing.T) {
}

func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) {
src := map[string]interface{}{
"foo": []string{"a", "b"},
}
dst := map[string]interface{}{
"foo": []int{1, 2},
testCases := []struct {
name string
options []func(*Config)
err string
}{
{
"With override and append slice",
[]func(*Config){WithOverride, WithAppendSlice},
"cannot append two slices with different type",
},
{
"With override and type check",
[]func(*Config){WithOverride, WithTypeCheck},
"cannot override two slices with different type",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
src := map[string]interface{}{
"foo": []string{"a", "b"},
}
dst := map[string]interface{}{
"foo": []int{1, 2},
}

if err := Merge(&src, &dst, WithOverride, WithAppendSlice); err == nil {
t.Fatal("expected an error, got nothing")
if err := Merge(&src, &dst, tc.options...); err == nil || !strings.Contains(err.Error(), tc.err) {
t.Fatalf("expected %q, got %q", tc.err, err)
}
})
}
}

func TestMergeSliceDifferentType(t *testing.T) {
func TestMergeSlicesIsNotSupported(t *testing.T) {
src := []string{"a", "b"}
dst := []int{1, 2}

if err := Merge(&src, &dst, WithOverride, WithAppendSlice); err == nil {
t.Fatal("expected an error, got nothing")
if err := Merge(&src, &dst, WithOverride, WithAppendSlice); err != ErrNotSupported {
t.Fatalf("expected %q, got %q", ErrNotSupported, err)
}
}