functions: ListFunc
This commit is contained in:
parent
529c2c3cc9
commit
da02e0da4d
|
@ -371,6 +371,50 @@ func flattener(finalList []cty.Value, flattenList cty.Value) []cty.Value {
|
|||
return finalList
|
||||
}
|
||||
|
||||
// ListFunc contructs a function that takes an arbitrary number of arguments
|
||||
// and returns a list containing those values in the same order.
|
||||
//
|
||||
// This function is deprecated in Terraform v0.12
|
||||
var ListFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{},
|
||||
VarParam: &function.Parameter{
|
||||
Name: "vals",
|
||||
Type: cty.DynamicPseudoType,
|
||||
AllowUnknown: true,
|
||||
AllowDynamicType: true,
|
||||
AllowNull: true,
|
||||
},
|
||||
Type: func(args []cty.Value) (ret cty.Type, err error) {
|
||||
if len(args) == 0 {
|
||||
return cty.NilType, fmt.Errorf("at least one argument is required")
|
||||
}
|
||||
|
||||
argTypes := make([]cty.Type, len(args))
|
||||
|
||||
for i, arg := range args {
|
||||
argTypes[i] = arg.Type()
|
||||
}
|
||||
|
||||
retType, _ := convert.UnifyUnsafe(argTypes)
|
||||
if retType == cty.NilType {
|
||||
return cty.NilType, fmt.Errorf("all arguments must have the same type")
|
||||
}
|
||||
|
||||
return cty.List(retType), nil
|
||||
},
|
||||
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
||||
newList := make([]cty.Value, 0, len(args))
|
||||
|
||||
for _, arg := range args {
|
||||
// We already know this will succeed because of the checks in our Type func above
|
||||
arg, _ = convert.Convert(arg, retType.ElementType())
|
||||
newList = append(newList, arg)
|
||||
}
|
||||
|
||||
return cty.ListVal(newList), nil
|
||||
},
|
||||
})
|
||||
|
||||
// MatchkeysFunc contructs a function that constructs a new list by taking a
|
||||
// subset of elements from one list whose indexes match the corresponding
|
||||
// indexes of values in another list.
|
||||
|
@ -507,6 +551,12 @@ func Flatten(list cty.Value) (cty.Value, error) {
|
|||
return FlattenFunc.Call([]cty.Value{list})
|
||||
}
|
||||
|
||||
// List takes any number of list arguments and returns a list containing those
|
||||
// values in the same order.
|
||||
func List(args ...cty.Value) (cty.Value, error) {
|
||||
return ListFunc.Call(args)
|
||||
}
|
||||
|
||||
// Matchkeys constructs a new list by taking a subset of elements from one list
|
||||
// whose indexes match the corresponding indexes of values in another list.
|
||||
func Matchkeys(values, keys, searchset cty.Value) (cty.Value, error) {
|
||||
|
|
|
@ -839,6 +839,72 @@ func TestFlatten(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
tests := []struct {
|
||||
Values []cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
[]cty.Value{
|
||||
cty.NilVal,
|
||||
},
|
||||
cty.NilVal,
|
||||
true,
|
||||
},
|
||||
{
|
||||
[]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
},
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
},
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("World"),
|
||||
}),
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.NumberIntVal(42),
|
||||
},
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.StringVal("Hello"),
|
||||
cty.StringVal("42"),
|
||||
}),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("list(%#v)", test.Values), func(t *testing.T) {
|
||||
got, err := List(test.Values...)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchkeys(t *testing.T) {
|
||||
tests := []struct {
|
||||
Keys cty.Value
|
||||
|
|
|
@ -65,7 +65,7 @@ func (s *Scope) Functions() map[string]function.Function {
|
|||
"jsonencode": stdlib.JSONEncodeFunc,
|
||||
"keys": unimplFunc, // TODO
|
||||
"length": funcs.LengthFunc,
|
||||
"list": unimplFunc, // TODO
|
||||
"list": funcs.ListFunc,
|
||||
"log": funcs.LogFunc,
|
||||
"lookup": unimplFunc, // TODO
|
||||
"lower": stdlib.LowerFunc,
|
||||
|
|
Loading…
Reference in New Issue