functions: KeysFunc

This commit is contained in:
Kristin Laemmert 2018-05-30 15:38:55 -07:00 committed by Martin Atkins
parent 21daabe680
commit 4d8c398f8e
3 changed files with 75 additions and 2 deletions

View File

@ -371,6 +371,30 @@ func flattener(finalList []cty.Value, flattenList cty.Value) []cty.Value {
return finalList
}
// KeysFunc contructs a function that takes a map and returns a sorted list of the map keys.
var KeysFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "inputMap",
Type: cty.Map(cty.DynamicPseudoType),
},
},
Type: function.StaticReturnType(cty.List(cty.String)),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var keys []cty.Value
for it := args[0].ElementIterator(); it.Next(); {
k, _ := it.Element()
fmt.Printf("appending %#v to %#v\n", k, keys)
keys = append(keys, k)
if err != nil {
return cty.ListValEmpty(cty.String), err
}
}
return cty.ListVal(keys), nil
},
})
// ListFunc contructs a function that takes an arbitrary number of arguments
// and returns a list containing those values in the same order.
//
@ -553,7 +577,7 @@ var MatchkeysFunc = function.New(&function.Spec{
},
})
// helper function to add an element to a list, if it does not already exsit
// helper function to add an element to a list, if it does not already exist
func appendIfMissing(slice []cty.Value, element cty.Value) ([]cty.Value, error) {
for _, ele := range slice {
eq, err := stdlib.Equal(ele, element)
@ -618,6 +642,11 @@ func Flatten(list cty.Value) (cty.Value, error) {
return FlattenFunc.Call([]cty.Value{list})
}
// Keys takes a map and returns a sorted list of the map keys.
func Keys(inputMap cty.Value) (cty.Value, error) {
return KeysFunc.Call([]cty.Value{inputMap})
}
// 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) {

View File

@ -839,6 +839,50 @@ func TestFlatten(t *testing.T) {
}
}
func TestKeys(t *testing.T) {
tests := []struct {
Map cty.Value
Want cty.Value
Err bool
}{
{
cty.MapVal(map[string]cty.Value{
"hello": cty.NumberIntVal(1),
"goodbye": cty.NumberIntVal(42),
}),
cty.ListVal([]cty.Value{
cty.StringVal("goodbye"),
cty.StringVal("hello"),
}),
false,
},
{ // Not a map
cty.StringVal("foo"),
cty.NilVal,
true,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("keys(%#v)", test.Map), func(t *testing.T) {
got, err := Keys(test.Map)
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 TestList(t *testing.T) {
tests := []struct {
Values []cty.Value

View File

@ -63,7 +63,7 @@ func (s *Scope) Functions() map[string]function.Function {
"join": funcs.JoinFunc,
"jsondecode": stdlib.JSONDecodeFunc,
"jsonencode": stdlib.JSONEncodeFunc,
"keys": unimplFunc, // TODO
"keys": funcs.KeysFunc,
"length": funcs.LengthFunc,
"list": funcs.ListFunc,
"log": funcs.LogFunc,