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

View File

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