allow sets and tuples in contains function

Sets are no longer going to be automatically converted, so we need to
handle those in contains.
This commit is contained in:
James Bardin 2019-05-01 18:13:06 -04:00
parent 19bddee11b
commit 3ab496d4b1
2 changed files with 28 additions and 2 deletions

View File

@ -268,7 +268,7 @@ var ContainsFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "list",
Type: cty.List(cty.DynamicPseudoType),
Type: cty.DynamicPseudoType,
},
{
Name: "value",
@ -277,8 +277,14 @@ var ContainsFunc = function.New(&function.Spec{
},
Type: function.StaticReturnType(cty.Bool),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
arg := args[0]
ty := arg.Type()
_, err = Index(args[0], args[1])
if !ty.IsListType() && !ty.IsTupleType() && !ty.IsSetType() {
return cty.NilVal, errors.New("argument must be list, tuple, or set")
}
_, err = Index(cty.TupleVal(arg.AsValueSlice()), args[1])
if err != nil {
return cty.False, nil
}

View File

@ -721,6 +721,26 @@ func TestContains(t *testing.T) {
cty.BoolVal(true),
false,
},
{ // set val
cty.SetVal([]cty.Value{
cty.StringVal("quick"),
cty.StringVal("brown"),
cty.StringVal("fox"),
}),
cty.StringVal("quick"),
cty.BoolVal(true),
false,
},
{ // tuple val
cty.TupleVal([]cty.Value{
cty.StringVal("quick"),
cty.StringVal("brown"),
cty.NumberIntVal(3),
}),
cty.NumberIntVal(3),
cty.BoolVal(true),
false,
},
}
for _, test := range tests {