port compact function

This commit is contained in:
Kristin Laemmert 2018-05-24 15:20:22 -07:00 committed by Martin Atkins
parent 1901d5d184
commit e697e7d733
4 changed files with 31 additions and 9 deletions

View File

@ -176,8 +176,6 @@ var CompactFunc = function.New(&function.Spec{
it := args[0].ElementIterator() it := args[0].ElementIterator()
for it.Next() { for it.Next() {
_, v := it.Element() _, v := it.Element()
fmt.Println("ohai!")
fmt.Println(v.AsString())
if v.AsString() == "" { if v.AsString() == "" {
continue continue
} }
@ -208,5 +206,5 @@ func CoalesceList(args ...cty.Value) (cty.Value, error) {
// Compact takes a list of strings and returns a new list // Compact takes a list of strings and returns a new list
// with any empty string elements removed. // with any empty string elements removed.
func Compact(list cty.Value) (cty.Value, error) { func Compact(list cty.Value) (cty.Value, error) {
return CoalesceListFunc.Call([]cty.Value{list}) return CompactFunc.Call([]cty.Value{list})
} }

View File

@ -339,6 +339,30 @@ func TestCompact(t *testing.T) {
}), }),
false, false,
}, },
{
cty.ListVal([]cty.Value{
cty.StringVal("test"),
cty.StringVal("test"),
cty.StringVal(""),
}),
cty.ListVal([]cty.Value{
cty.StringVal("test"),
cty.StringVal("test"),
}),
false,
},
{ // errrors on list of lists
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("test"),
}),
cty.ListVal([]cty.Value{
cty.StringVal(""),
}),
}),
cty.NilVal,
true,
},
} }
for _, test := range tests { for _, test := range tests {

View File

@ -44,7 +44,7 @@ func (s *Scope) Functions() map[string]function.Function {
"cidrsubnet": funcs.CidrSubnetFunc, "cidrsubnet": funcs.CidrSubnetFunc,
"coalesce": stdlib.CoalesceFunc, "coalesce": stdlib.CoalesceFunc,
"coalescelist": funcs.CoalesceListFunc, "coalescelist": funcs.CoalesceListFunc,
"compact": unimplFunc, // TODO "compact": funcs.CompactFunc,
"concat": stdlib.ConcatFunc, "concat": stdlib.ConcatFunc,
"contains": unimplFunc, // TODO "contains": unimplFunc, // TODO
"csvdecode": stdlib.CSVDecodeFunc, "csvdecode": stdlib.CSVDecodeFunc,