Skip to content

Commit

Permalink
lang: Fix crash in lookup function
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 22, 2018
1 parent 1dc3e50 commit 4856d81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lang/funcs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions lang/funcs/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

1 comment on commit 4856d81

@mitchellh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

Please sign in to comment.