Skip to content

Commit

Permalink
Merge pull request #221 from zhiyu0729/issue220
Browse files Browse the repository at this point in the history
Fix: validate before set
  • Loading branch information
darccio authored Mar 15, 2023
2 parents 5b9bbdb + 424f8b2 commit bd316d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions issue220_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mergo_test

import (
"reflect"
"testing"

"github.com/imdario/mergo"
)

func TestIssue220(t *testing.T) {
dst := []interface{}{
map[string]int{
"a": 1,
},
}
src := []interface{}{
"nil",
}
expected := []interface{}{
map[string]int{
"a": 1,
},
}

err := mergo.Merge(&dst, src, mergo.WithSliceDeepCopy)
if err != nil {
t.Errorf("unexpected error %v", err)
}

if !reflect.DeepEqual(dst, expected) {
t.Errorf("expected: %#v\ngot: %#v", expected, dst)
}
}
2 changes: 1 addition & 1 deletion merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}

if src.Kind() != reflect.Map {
if overwrite {
if overwrite && dst.CanSet() {
dst.Set(src)
}
return
Expand Down

0 comments on commit bd316d3

Please sign in to comment.