Skip to content

Commit

Permalink
Fixes mergo.Merge skips maps with nil values in it nqd#9
Browse files Browse the repository at this point in the history
  • Loading branch information
davitt committed Sep 28, 2021
1 parent 439a2df commit 6b8e554
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func unflatten(flat map[string]interface{}, opts *Options) (nested map[string]in

for k, v := range flat {
temp := uf(k, v, opts).(map[string]interface{})
err = mergo.Merge(&nested, temp)
err = mergo.Merge(&nested, temp, func(c *mergo.Config) {c.Overwrite = true})
if err != nil {
return
}
Expand Down
16 changes: 16 additions & 0 deletions flat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ func TestUnflatten(t *testing.T) {
"travis": "true",
},
},
// keys with nil values
{
map[string]interface{}{
"foo.bar": map[string]interface{}{"t": nil},
"foo": map[string]interface{}{"k": nil},
},
nil,
map[string]interface{}{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"t": nil,
},
"k": nil,
},
},
},
// todo
// overwrite true
// {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/nqd/flat

go 1.12

require github.com/imdario/mergo v0.3.6
require github.com/imdario/mergo v0.3.12
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 6b8e554

Please sign in to comment.