From 983af7a183f39af9ea812095d146a6676699a917 Mon Sep 17 00:00:00 2001 From: Saverio Miroddi Date: Thu, 11 Jul 2019 19:31:22 +0200 Subject: [PATCH] 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. --- lang/funcs/collection.go | 2 +- lang/funcs/collection_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index fd0de9ea5f61..eac17cc10cd8 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -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) diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index ba79ee33b3a9..88dd306f7807 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -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,