Skip to content

Commit

Permalink
decoder: Implement reference origins for conditional expression
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 22, 2023
1 parent e3e809e commit 0eea44e
Show file tree
Hide file tree
Showing 2 changed files with 499 additions and 1 deletion.
44 changes: 43 additions & 1 deletion decoder/expr_any_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support for-in-if expression https://github.com/hashicorp/terraform-ls/issues/527
// TODO: Support conditional expression https://github.com/hashicorp/terraform-ls/issues/528
// TODO: Support complex index expressions https://github.com/hashicorp/terraform-ls/issues/531
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

Expand All @@ -129,6 +128,10 @@ func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool
return origins
}

if origins, ok := a.refOriginsForConditionalExpr(ctx, allowSelfRefs); ok {
return origins
}

// attempt to get accurate constraint for the origins
// if we recognise the given expression
funcExpr := functionExpr{
Expand Down Expand Up @@ -280,3 +283,42 @@ func (a Any) refOriginsForTemplateExpr(ctx context.Context, allowSelfRefs bool)

return origins, false
}

func (a Any) refOriginsForConditionalExpr(ctx context.Context, allowSelfRefs bool) (reference.Origins, bool) {
origins := make(reference.Origins, 0)

// There is currently no way of decoding conditional expressions in JSON
// so we just collect them using the fallback logic assuming "any"
// constraint and focus on collecting expressions in HCL with more
// accurate constraints below.

switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
condExpr := newExpression(a.pathCtx, eType.Condition, schema.AnyExpression{
OfType: cty.Bool,
})
if expr, ok := condExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}

trueExpr := newExpression(a.pathCtx, eType.TrueResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := trueExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}

falseExpr := newExpression(a.pathCtx, eType.FalseResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := falseExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}

return origins, true
}

return origins, false
}
Loading

0 comments on commit 0eea44e

Please sign in to comment.