diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 6b958f78e..d299b4b7a 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -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 } diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index f16b18eb6..dd112388f 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -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 {