From 4856d81300261334f3498edc11fd499efc9f1066 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 22 Oct 2018 12:58:47 +0200 Subject: [PATCH 1/2] lang: Fix crash in lookup function --- lang/funcs/collection.go | 7 ++++++- lang/funcs/collection_test.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 742b84c9e..264ed122d 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -512,9 +512,14 @@ var LookupFunc = function.New(&function.Spec{ } ty := args[0].Type() - key := args[1].AsString() + switch { case ty.IsObjectType(): + if !args[1].IsKnown() { + return args[2].Type(), nil + } + + key := args[1].AsString() if ty.HasAttribute(key) { return args[0].GetAttr(key).Type(), nil } else if len(args) == 3 { diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index b04d01d43..e0c3e33f8 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -1255,6 +1255,25 @@ func TestLookup(t *testing.T) { cty.UnknownVal(cty.String), false, }, + { + []cty.Value{ + simpleMap, + cty.UnknownVal(cty.String), + }, + cty.UnknownVal(cty.String), + false, + }, + { + []cty.Value{ + cty.ObjectVal(map[string]cty.Value{ + "foo": cty.StringVal("a"), + "bar": cty.StringVal("b"), + }), + cty.UnknownVal(cty.String), + }, + cty.UnknownVal(cty.String), + false, + }, } for _, test := range tests { From b41cda7d7a27d0e8ca43906e0b33765035ec5f34 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 23 Oct 2018 12:42:46 +0200 Subject: [PATCH 2/2] Return cty.DynamicPseudoType instead --- lang/funcs/collection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 264ed122d..cf0958f44 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -516,7 +516,7 @@ var LookupFunc = function.New(&function.Spec{ switch { case ty.IsObjectType(): if !args[1].IsKnown() { - return args[2].Type(), nil + return cty.DynamicPseudoType, nil } key := args[1].AsString()