Skip to content

Commit

Permalink
use src map if dst is nil and can't be set
Browse files Browse the repository at this point in the history
see darccio#90

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Feb 18, 2021
1 parent cd55aea commit 00a5190
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/imdario/mergo
module github.com/ndeloof/mergo

go 1.13

Expand Down
42 changes: 42 additions & 0 deletions issue90_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mergo

import (
"reflect"
"testing"
)

type structWithMap struct {
Data map[string]string
}


func TestIssue90(t *testing.T) {
dst := map[string]structWithMap{
"struct": {
Data: nil,
},
}
src := map[string]structWithMap{
"struct": {
Data: map[string]string{
"foo": "bar",
},
},
}
expected := map[string]structWithMap{
"struct": {
Data: map[string]string{
"foo": "bar",
},
},
}

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

if !reflect.DeepEqual(dst, expected) {
t.Errorf("expected: %#v\ngot: %#v", expected, dst)
}
}

This comment has been minimized.

Copy link
@thaJeztah

thaJeztah Feb 18, 2021

"Missing newline at end of file"

This comment has been minimized.

Copy link
@ndeloof

ndeloof Feb 18, 2021

Author Owner

as defined by ANSI C 1989 standard
I wish there will be a time software industry will not be based on decision made 30 years ago.

This comment has been minimized.

Copy link
@thaJeztah

thaJeztah Feb 18, 2021

Yeah, but I know that various tools don't like it when it's missing (including some git operations)

7 changes: 6 additions & 1 deletion merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}
case reflect.Map:
if dst.IsNil() && !src.IsNil() {
dst.Set(reflect.MakeMap(dst.Type()))
if dst.CanSet() {
dst.Set(reflect.MakeMap(dst.Type()))
} else {
dst = src
return
}
}

if src.Kind() != reflect.Map {
Expand Down

0 comments on commit 00a5190

Please sign in to comment.