nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: imdario/mergo
Pull request #221 (2022-08-17 07:49:44)

Fix: validate before set
Fix: #220 

Signed-off-by: Zhiyu Wang <zhiyuwang.newbis@gmail.com>


Patch:
diff --git a/merge.go b/merge.go
index 4b47d0b..16e55e1 100644
--- a/merge.go
+++ b/merge.go
@@ -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


Test patch:
diff --git a/issue220_test.go b/issue220_test.go
new file mode 100644
index 0000000..164f5bd
--- /dev/null
+++ b/issue220_test.go
@@ -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)
+	}
+}