Skip to content

Commit

Permalink
stdlib/merge: return an empty object when all inputs are null
Browse files Browse the repository at this point in the history
It seems intuitive for merge() to return an empty object when the inputs
are null (or omitted entirely) - returning an error would likely break
many terraform configurations, and returning an empty object rather than
null seems more consistent with the documented behavior (though it is
not explicit!).

I dug through the commit history and compared terraform's documentation,
post pre- and post-hcl2, and did not find any clues as to why it was
returning null rather than an empty object. I feel like this is an
acceptable breaking change, but am certainly open to other opinions.
  • Loading branch information
mildwonkey committed Dec 11, 2020
1 parent 4a53886 commit a7d3a90
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
8 changes: 0 additions & 8 deletions cty/function/stdlib/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,25 +758,17 @@ var MergeFunc = function.New(&function.Spec{
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
outputMap := make(map[string]cty.Value)

// if all inputs are null, return a null value rather than an object
// with null attributes
allNull := true
for _, arg := range args {
if arg.IsNull() {
continue
} else {
allNull = false
}

for it := arg.ElementIterator(); it.Next(); {
k, v := it.Element()
outputMap[k.AsString()] = v
}
}

switch {
case allNull:
return cty.NullVal(retType), nil
case retType.IsMapType():
if len(outputMap) == 0 {
return cty.MapValEmpty(retType.ElementType()), nil
Expand Down
4 changes: 2 additions & 2 deletions cty/function/stdlib/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ func TestMerge(t *testing.T) {
}),
false,
},
{ // handle null map
{ // all inputs are null
[]cty.Value{
cty.NullVal(cty.Map(cty.String)),
cty.NullVal(cty.Object(map[string]cty.Type{
"a": cty.List(cty.String),
})),
},
cty.NullVal(cty.EmptyObject),
cty.EmptyObjectVal,
false,
},
{ // handle null object
Expand Down

0 comments on commit a7d3a90

Please sign in to comment.