Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hclsyntax: address multiple issues with sequences (...) #386

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
}

switch {
case expandVal.Type().Equals(cty.DynamicPseudoType):
Copy link
Member

Choose a reason for hiding this comment

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

If passing null as the first argument isn't valid, we should have the same null check here as below. Otherwise we have a known value turning into an unknown value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, fixed!

return cty.DynamicVal, diags
case expandVal.Type().IsTupleType() || expandVal.Type().IsListType() || expandVal.Type().IsSetType():
if expandVal.IsNull() {
diags = append(diags, &hcl.Diagnostic{
Expand Down Expand Up @@ -406,22 +408,39 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
} else {
param = varParam
}
argExpr := e.Args[i]

// TODO: we should also unpick a PathError here and show the
// path to the deep value where the error was detected.
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: argExpr.StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: argExpr,
EvalContext: ctx,
})
// this can happen if an argument is (incorrectly) null.
if i > len(e.Args)-1 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: args[len(params)].StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: e,
EvalContext: ctx,
})
} else {
argExpr := e.Args[i]

// TODO: we should also unpick a PathError here and show the
// path to the deep value where the error was detected.
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: argExpr.StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: argExpr,
EvalContext: ctx,
})
}

default:
diags = append(diags, &hcl.Diagnostic{
Expand Down
25 changes: 25 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,31 @@ upper(
cty.DynamicVal,
1, // too many function arguments
},
{
`concat([1, null]...)`,
&hcl.EvalContext{
Functions: map[string]function.Function{
"concat": stdlib.ConcatFunc,
},
},
cty.DynamicVal,
1, // argument cannot be null
},
{
`concat(var.unknownlist...)`,
&hcl.EvalContext{
Functions: map[string]function.Function{
"concat": stdlib.ConcatFunc,
},
Variables: map[string]cty.Value{
"var": cty.ObjectVal(map[string]cty.Value{
"unknownlist": cty.UnknownVal(cty.DynamicPseudoType),
}),
},
},
cty.DynamicVal,
0,
},
{
`[]`,
nil,
Expand Down