porting matchkeys

This commit is contained in:
Kristin Laemmert 2018-05-29 13:58:32 -07:00 committed by Martin Atkins
parent 3ac6e575f1
commit 4dd3ffc127
3 changed files with 251 additions and 3 deletions

View File

@ -232,7 +232,7 @@ var IndexFunc = function.New(&function.Spec{
} }
if args[0].LengthInt() == 0 { // Easy path if args[0].LengthInt() == 0 { // Easy path
return cty.NilVal, fmt.Errorf("Cannot search an empty list") return cty.NilVal, fmt.Errorf("cannot search an empty list")
} }
for it := args[0].ElementIterator(); it.Next(); { for it := args[0].ElementIterator(); it.Next(); {
@ -302,7 +302,7 @@ var ChunklistFunc = function.New(&function.Spec{
} }
if size < 0 { if size < 0 {
return cty.NilVal, fmt.Errorf("The size argument must be positive") return cty.NilVal, fmt.Errorf("the size argument must be positive")
} }
output := make([]cty.Value, 0) output := make([]cty.Value, 0)
@ -334,6 +334,72 @@ var ChunklistFunc = function.New(&function.Spec{
}, },
}) })
// 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.
var MatchkeysFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "values",
Type: cty.List(cty.DynamicPseudoType),
},
{
Name: "keys",
Type: cty.List(cty.DynamicPseudoType),
},
{
Name: "searchset",
Type: cty.List(cty.DynamicPseudoType),
},
},
Type: func(args []cty.Value) (cty.Type, error) {
if args[1].Type() != args[2].Type() {
return cty.NilType, fmt.Errorf("lists must be of the same type")
}
if args[0].LengthInt() != args[1].LengthInt() {
return cty.NilType, fmt.Errorf("length of keys and values should be equal")
}
return args[0].Type(), nil
},
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
output := make([]cty.Value, 0)
values := args[0]
keys := args[1]
searchset := args[2]
// if searchset is empty, return an empty list.
if searchset.LengthInt() == 0 {
return cty.ListValEmpty(*retType.ListElementType()), nil
}
i := 0
for it := keys.ElementIterator(); it.Next(); {
_, key := it.Element()
for iter := searchset.ElementIterator(); iter.Next(); {
_, search := iter.Element()
eq, err := stdlib.Equal(key, search)
if err != nil {
return cty.NilVal, err
}
if eq.True() {
v := values.Index(cty.NumberIntVal(int64(i)))
output = append(output, v)
break
}
}
i++
}
// if we haven't matched any key, then output is an empty list.
if len(output) == 0 {
return cty.ListValEmpty(*retType.ListElementType()), nil
}
return cty.ListVal(output), nil
},
})
// 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 exsit
func appendIfMissing(slice []cty.Value, element cty.Value) ([]cty.Value, error) { func appendIfMissing(slice []cty.Value, element cty.Value) ([]cty.Value, error) {
for _, ele := range slice { for _, ele := range slice {
@ -392,3 +458,9 @@ func Distinct(list cty.Value) (cty.Value, error) {
func Chunklist(list, size cty.Value) (cty.Value, error) { func Chunklist(list, size cty.Value) (cty.Value, error) {
return ChunklistFunc.Call([]cty.Value{list, size}) return ChunklistFunc.Call([]cty.Value{list, size})
} }
// 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) {
return MatchkeysFunc.Call([]cty.Value{values, keys, searchset})
}

View File

@ -786,3 +786,179 @@ func TestChunklist(t *testing.T) {
}) })
} }
} }
func TestMatchkeys(t *testing.T) {
tests := []struct {
Keys cty.Value
Values cty.Value
Searchset cty.Value
Want cty.Value
Err bool
}{
{ // normal usage
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref1"),
cty.StringVal("ref2"),
cty.StringVal("ref3"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref1"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
false,
},
{ // normal usage 2, check the order
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref1"),
cty.StringVal("ref2"),
cty.StringVal("ref3"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref2"),
cty.StringVal("ref1"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
}),
false,
},
{ // no matches
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref1"),
cty.StringVal("ref2"),
cty.StringVal("ref3"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref4"),
}),
cty.ListValEmpty(cty.String),
false,
},
{ // no matches 2
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("ref1"),
cty.StringVal("ref2"),
cty.StringVal("ref3"),
}),
cty.ListValEmpty(cty.String),
cty.ListValEmpty(cty.String),
false,
},
{ // zero case
cty.ListValEmpty(cty.String),
cty.ListValEmpty(cty.String),
cty.ListVal([]cty.Value{cty.StringVal("nope")}),
cty.ListValEmpty(cty.String),
false,
},
{ // complex values
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("a"),
}),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("a"),
}),
}),
false,
},
// errors
{ // different types
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(1),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.NilVal,
true,
},
{ // different types
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.NilVal,
true,
},
{ // lists of different length
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
}),
cty.NilVal,
true,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("matchkeys(%#v, %#v, %#v)", test.Keys, test.Values, test.Searchset), func(t *testing.T) {
got, err := Matchkeys(test.Keys, test.Values, test.Searchset)
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)
}
})
}
}

View File

@ -54,7 +54,7 @@ func (s *Scope) Functions() map[string]function.Function {
"chunklist": funcs.ChunklistFunc, "chunklist": funcs.ChunklistFunc,
"file": funcs.MakeFileFunc(s.BaseDir, false), "file": funcs.MakeFileFunc(s.BaseDir, false),
"filebase64": funcs.MakeFileFunc(s.BaseDir, true), "filebase64": funcs.MakeFileFunc(s.BaseDir, true),
"matchkeys": unimplFunc, // TODO "matchkeys": funcs.MatchkeysFunc,
"flatten": unimplFunc, // TODO "flatten": unimplFunc, // TODO
"floor": funcs.FloorFunc, "floor": funcs.FloorFunc,
"format": stdlib.FormatFunc, "format": stdlib.FormatFunc,