From fa48576ef06dab552af6a8c62feb5dcb37f75cfb Mon Sep 17 00:00:00 2001 From: Milos Pejanovic Date: Mon, 20 Sep 2021 16:52:08 +0200 Subject: [PATCH] implement use case when null is set --- conflate_test.go | 19 ++++++++++++++++++- go.sum | 1 - merge.go | 5 +++++ testdata/test_not_nulled.yaml | 2 ++ testdata/test_nulled.yaml | 2 ++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 testdata/test_not_nulled.yaml create mode 100644 testdata/test_nulled.yaml diff --git a/conflate_test.go b/conflate_test.go index cf9c475..2c94b89 100644 --- a/conflate_test.go +++ b/conflate_test.go @@ -2,11 +2,12 @@ package conflate import ( gocontext "context" - "github.com/stretchr/testify/assert" "net/http" "os" "sync" "testing" + + "github.com/stretchr/testify/assert" ) type TestData struct { @@ -20,6 +21,11 @@ type TestData struct { All string `json:"all"` } +type TestNulledData struct { + Foo string `yaml:"foo"` + Bar string `yaml:"bar"` +} + func TestFromFiles(t *testing.T) { c, err := FromFiles("testdata/valid_parent.json") assert.Nil(t, err) @@ -318,3 +324,14 @@ func TestConflate_mergeDataError(t *testing.T) { assert.NotNil(t, err) assert.Contains(t, err.Error(), "Failed to merge") } + +func TestFromFilesNulled(t *testing.T) { + c, err := FromFiles("testdata/test_not_nulled.yaml", "testdata/test_nulled.yaml") + assert.Nil(t, err) + assert.NotNil(t, c) + var testData TestNulledData + err = c.Unmarshal(&testData) + assert.Nil(t, err) + assert.Equal(t, "foo", testData.Foo) + assert.Equal(t, "", testData.Bar) +} diff --git a/go.sum b/go.sum index adefc0f..9596753 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/merge.go b/merge.go index 0f39423..25d9077 100644 --- a/merge.go +++ b/merge.go @@ -64,6 +64,11 @@ func mergeMapRecursive(ctx context, toVal reflect.Value, fromVal reflect.Value, return makeContextError(ctx, "The destination value must be a map[string]interface{}") } for name, fromProp := range fromProps { + // merge in explicit nil values + if fromProp == nil { + toProps[name] = nil + continue + } if val := toProps[name]; val == nil { toProps[name] = fromProp } else { diff --git a/testdata/test_not_nulled.yaml b/testdata/test_not_nulled.yaml new file mode 100644 index 0000000..e7b26c8 --- /dev/null +++ b/testdata/test_not_nulled.yaml @@ -0,0 +1,2 @@ +foo: foo +bar: bar \ No newline at end of file diff --git a/testdata/test_nulled.yaml b/testdata/test_nulled.yaml new file mode 100644 index 0000000..596e0e3 --- /dev/null +++ b/testdata/test_nulled.yaml @@ -0,0 +1,2 @@ +foo: foo +bar: null \ No newline at end of file