Allow null values in Compact function

The function would previously panic when one or more null values were among the arguments.

The new behavior treats nulls as empty strings, therefore, it removes them.
This commit is contained in:
Saverio Miroddi 2019-07-11 19:31:22 +02:00
parent 5ac920223f
commit 983af7a183
2 changed files with 11 additions and 1 deletions

View File

@ -246,7 +246,7 @@ var CompactFunc = function.New(&function.Spec{
for it := listVal.ElementIterator(); it.Next(); {
_, v := it.Element()
if v.AsString() == "" {
if v.IsNull() || v.AsString() == "" {
continue
}
outputList = append(outputList, v)

View File

@ -572,6 +572,7 @@ func TestCompact(t *testing.T) {
cty.StringVal("test"),
cty.StringVal(""),
cty.StringVal("test"),
cty.NullVal(cty.String),
}),
cty.ListVal([]cty.Value{
cty.StringVal("test"),
@ -588,6 +589,14 @@ func TestCompact(t *testing.T) {
cty.ListValEmpty(cty.String),
false,
},
{
cty.ListVal([]cty.Value{
cty.NullVal(cty.String),
cty.NullVal(cty.String),
}),
cty.ListValEmpty(cty.String),
false,
},
{
cty.ListValEmpty(cty.String),
cty.ListValEmpty(cty.String),
@ -610,6 +619,7 @@ func TestCompact(t *testing.T) {
cty.StringVal("test"),
cty.UnknownVal(cty.String),
cty.StringVal(""),
cty.NullVal(cty.String),
}),
cty.UnknownVal(cty.List(cty.String)),
false,