lang/funcs: fix error when `matchkeys` encountered a variable

`matchkeys` was returning a (false) error if the searchset was a
variable, since then the type of the keylist and searchset parameters
would not match.

This does slightly change the behavior: previously matchkeys would
produce an error if the parameters were not of the same type, for e.g.
if searchset was a list of strings and keylist was a list of integers.
  This no longer produces an error.
This commit is contained in:
Kristin Laemmert 2019-06-03 18:00:53 -04:00
parent ab9e98da3c
commit f2a14d7c18
2 changed files with 11 additions and 5 deletions

View File

@ -800,7 +800,13 @@ var MatchkeysFunc = function.New(&function.Spec{
}, },
}, },
Type: func(args []cty.Value) (cty.Type, error) { Type: func(args []cty.Value) (cty.Type, error) {
if !args[1].Type().Equals(args[2].Type()) { argTypes := make([]cty.Type, 2)
for i := 0; i < 2; i++ {
argTypes[i] = args[i+1].Type()
}
ty, _ := convert.UnifyUnsafe(argTypes)
if ty == cty.NilType {
return cty.NilType, errors.New("lists must be of the same type") return cty.NilType, errors.New("lists must be of the same type")
} }

View File

@ -1883,8 +1883,7 @@ func TestMatchkeys(t *testing.T) {
cty.UnknownVal(cty.List(cty.String)), cty.UnknownVal(cty.List(cty.String)),
false, false,
}, },
// errors { // different types that can be unified
{ // different types
cty.ListVal([]cty.Value{ cty.ListVal([]cty.Value{
cty.StringVal("a"), cty.StringVal("a"),
}), }),
@ -1894,9 +1893,10 @@ func TestMatchkeys(t *testing.T) {
cty.ListVal([]cty.Value{ cty.ListVal([]cty.Value{
cty.StringVal("a"), cty.StringVal("a"),
}), }),
cty.NilVal, cty.ListValEmpty(cty.String),
true, false,
}, },
// errors
{ // different types { // different types
cty.ListVal([]cty.Value{ cty.ListVal([]cty.Value{
cty.StringVal("a"), cty.StringVal("a"),