Skip to content

Commit

Permalink
Look through interfaces and pointers when determining emptiness
Browse files Browse the repository at this point in the history
When dealing with map[string]interface{}, the common case for loading
arbitrary data from yaml/json, empty strings are not overwritten because
they appear as a non-empty interface containing an empty string. This
change causes non-nil interfaces to be examined for emptiness.
  • Loading branch information
novas0x2a committed May 22, 2018
1 parent 96a98fa commit df0700a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mergo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ func isEmptyValue(v reflect.Value) bool {
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr, reflect.Func:
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return true
}
return isEmptyValue(v.Elem())
case reflect.Func:
return v.IsNil()
case reflect.Invalid:
return true
Expand Down
18 changes: 18 additions & 0 deletions pr80_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mergo

import (
"testing"
)

type mapInterface map[string]interface{}

func TestMergeMapsEmptyString(t *testing.T) {
a := mapInterface{"s": ""}
b := mapInterface{"s": "foo"}
if err := Merge(&a, b); err != nil {
t.Fatal(err)
}
if a["s"] != "foo" {
t.Fatalf("b not merged in properly: a.s.Value(%s) != expected(%s)", a["s"], "foo")
}
}

0 comments on commit df0700a

Please sign in to comment.