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

@ -10,7 +10,7 @@ import (
"github.com/zclconf/go-cty/cty/gocty" "github.com/zclconf/go-cty/cty/gocty"
) )
// CidrHostFunc contructs a function that calculates a full host IP address // CidrHostFunc contructs a function that calculates a full host IP address
// within a given IP network address prefix. // within a given IP network address prefix.
var CidrHostFunc = function.New(&function.Spec{ var CidrHostFunc = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{
@ -43,7 +43,7 @@ var CidrHostFunc = function.New(&function.Spec{
}, },
}) })
// CidrNetmaskFunc contructs a function that converts an IPv4 address prefix given // CidrNetmaskFunc contructs a function that converts an IPv4 address prefix given
// in CIDR notation into a subnet mask address. // in CIDR notation into a subnet mask address.
var CidrNetmaskFunc = function.New(&function.Spec{ var CidrNetmaskFunc = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{
@ -63,7 +63,7 @@ var CidrNetmaskFunc = function.New(&function.Spec{
}, },
}) })
// CidrSubnetFunc contructs a function that calculates a subnet address within // CidrSubnetFunc contructs a function that calculates a subnet address within
// a given IP network address prefix. // a given IP network address prefix.
var CidrSubnetFunc = function.New(&function.Spec{ var CidrSubnetFunc = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{

View File

@ -107,7 +107,7 @@ var LengthFunc = function.New(&function.Spec{
}, },
}) })
// CoalesceListFunc contructs a function that takes any number of list arguments // CoalesceListFunc contructs a function that takes any number of list arguments
// and returns the first one that isn't empty. // and returns the first one that isn't empty.
var CoalesceListFunc = function.New(&function.Spec{ var CoalesceListFunc = function.New(&function.Spec{
Params: []function.Parameter{}, Params: []function.Parameter{},
@ -159,7 +159,7 @@ var CoalesceListFunc = function.New(&function.Spec{
}, },
}) })
// CompactFunc contructs a function that takes a list of strings and returns a new list // CompactFunc contructs a function that takes a list of strings and returns a new list
// with any empty string elements removed. // with any empty string elements removed.
var CompactFunc = function.New(&function.Spec{ var CompactFunc = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{
@ -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,