diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 50e4a3ee3..9b537f5e0 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -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) { diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index 163b66215..6a0126cc1 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -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 diff --git a/lang/functions.go b/lang/functions.go index 877cb320c..2ffe1a0f3 100644 --- a/lang/functions.go +++ b/lang/functions.go @@ -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,