From d349049cc323f52eaac48facf257d811b538f1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E7=8E=AE=E6=96=87?= Date: Mon, 13 May 2024 15:24:07 +0800 Subject: [PATCH] merge should preserve zero value in dest --- dict.go | 4 ++-- dict_test.go | 7 +++++++ go.mod | 2 +- go.sum | 9 +++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dict.go b/dict.go index ade88969..ecff1ad3 100644 --- a/dict.go +++ b/dict.go @@ -89,7 +89,7 @@ func dict(v ...interface{}) map[string]interface{} { func merge(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} { for _, src := range srcs { - if err := mergo.Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithoutDereference); err != nil { // Swallow errors inside of a template. return "" } @@ -99,7 +99,7 @@ func merge(dst map[string]interface{}, srcs ...map[string]interface{}) interface func mustMerge(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) { for _, src := range srcs { - if err := mergo.Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithoutDereference); err != nil { return nil, err } } diff --git a/dict_test.go b/dict_test.go index c829daa5..2ecc1388 100644 --- a/dict_test.go +++ b/dict_test.go @@ -157,7 +157,9 @@ func TestMerge(t *testing.T) { "j": "jay", "k": map[string]interface{}{ "l": false, + "m": true, }, + "z": 10, }, "dst": map[string]interface{}{ "a": "one", @@ -167,9 +169,12 @@ func TestMerge(t *testing.T) { }, "g": []int{8, 9}, "i": "eye", + "j": nil, "k": map[string]interface{}{ "l": true, + "m": false, }, + "z": 0, }, } tpl := `{{merge .dst .src1 .src2}}` @@ -191,7 +196,9 @@ func TestMerge(t *testing.T) { "j": "jay", // overridden and merged "k": map[string]interface{}{ "l": true, // overridden + "m": false, }, + "z": 0, // zero value should be preserved } assert.Equal(t, expected, dict["dst"]) } diff --git a/go.mod b/go.mod index 494916f1..463e4ffa 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Masterminds/semver/v3 v3.2.0 github.com/google/uuid v1.1.1 github.com/huandu/xstrings v1.3.3 - github.com/imdario/mergo v0.3.11 + github.com/imdario/mergo v0.3.16 github.com/mitchellh/copystructure v1.0.0 github.com/shopspring/decimal v1.2.0 github.com/spf13/cast v1.3.1 diff --git a/go.sum b/go.sum index 11a67119..f97b4b3c 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4= github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= -github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= +github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= @@ -56,6 +56,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=