From 4b122dacaebc1c98b13278df438ed3fe0d938f8c Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 24 Jun 2020 21:06:30 -0500 Subject: [PATCH] be consistant when handling null props, remove them all --- lang/funcs/collection.go | 6 ++++++ lang/funcs/collection_test.go | 21 +++++++++++++++++++ .../configuration/functions/deepmerge.html.md | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 187f4eb69966..6885fc53af89 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -192,6 +192,12 @@ func recursiveMerge(newValue cty.Value, existingValue cty.Value) cty.Value { mergedMap[key] = recursiveMerge(newValue, mergedMap[key]) } + // strip out any null properties + for key, value := range mergedMap { + if value.IsNull() { + delete(mergedMap, key) + } + } return wrapAsObjectOrMap(mergedMap) } diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index 252814611a23..8518cf5729b3 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -14,6 +14,27 @@ func TestDeepMerge(t *testing.T) { Want cty.Value Err bool }{ + { // remove props if set to null + []cty.Value{ + cty.MapVal(map[string]cty.Value{ + "a": cty.StringVal("a"), + "b": cty.StringVal("b"), + "c": cty.NullVal(cty.String), + "d": cty.StringVal("d"), + }), + cty.MapVal(map[string]cty.Value{ + "b": cty.NullVal(cty.String), + "c": cty.StringVal("c"), + "e": cty.NullVal(cty.String), + }), + }, + cty.MapVal(map[string]cty.Value{ + "a": cty.StringVal("a"), + "c": cty.StringVal("c"), + "d": cty.StringVal("d"), + }), + false, + }, { []cty.Value{ cty.ObjectVal(map[string]cty.Value{ diff --git a/website/docs/configuration/functions/deepmerge.html.md b/website/docs/configuration/functions/deepmerge.html.md index aeb1e95f3859..a4882512176b 100644 --- a/website/docs/configuration/functions/deepmerge.html.md +++ b/website/docs/configuration/functions/deepmerge.html.md @@ -23,6 +23,10 @@ or object that contains a merged set of elements from all arguments. The behavior is exactly the same as `merge`, but it will recurse in to objects, and supports partial updates of nested objects. +## Null Values +Any properties that are set to a `null` value will be removed from the final object/map. If you wish +to keep these properties, make sure to fill them with a default value (ex: `{}`) + ## Examples ```