Skip to content

Commit

Permalink
be consistant when handling null props, remove them all
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Sep 3, 2020
1 parent 12bfc5f commit 4b122da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lang/funcs/collection.go
Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions lang/funcs/collection_test.go
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions website/docs/configuration/functions/deepmerge.html.md
Expand Up @@ -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

```
Expand Down

0 comments on commit 4b122da

Please sign in to comment.