Skip to content

Commit 3d438f7

Browse files
authored
Merge pull request #85 from terraform-linters/is_null_expr
tflint: Add `IsNullExpr` API
2 parents 827cf11 + 09b2270 commit 3d438f7

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

helper/runner.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ func (r *Runner) EvaluateExprOnRootCtx(expr hcl.Expression, ret interface{}, wan
204204
return r.EvaluateExpr(expr, ret, wantType)
205205
}
206206

207+
// IsNullExpr checks whether the passed expression is null or not.
208+
// Note that it does not eval the expression for simplify the implementation.
209+
func (r *Runner) IsNullExpr(expr hcl.Expression) (bool, error) {
210+
val, diags := expr.Value(nil)
211+
if diags.HasErrors() {
212+
return false, diags
213+
}
214+
return val.IsNull(), nil
215+
}
216+
207217
// EmitIssueOnExpr adds an issue to the runner itself.
208218
func (r *Runner) EmitIssueOnExpr(rule tflint.Rule, message string, expr hcl.Expression) error {
209219
r.Issues = append(r.Issues, &Issue{

tflint/client/client.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,23 @@ func (c *Client) EvaluateExprOnRootCtx(expr hcl.Expression, ret interface{}, wan
302302
return nil
303303
}
304304

305+
// IsNullExpr calls the server-side IsNullExpr method with the passed expression.
306+
func (c *Client) IsNullExpr(expr hcl.Expression) (bool, error) {
307+
var response IsNullExprResponse
308+
309+
src, err := ioutil.ReadFile(expr.Range().Filename)
310+
if err != nil {
311+
return false, err
312+
}
313+
req := &IsNullExprRequest{}
314+
req.Expr, req.Range = encodeExpr(src, expr)
315+
if err := c.rpcClient.Call("Plugin.IsNullExpr", req, &response); err != nil {
316+
return false, err
317+
}
318+
319+
return response.Ret, response.Err
320+
}
321+
305322
// EmitIssueOnExpr calls the server-side EmitIssue method with the passed expression.
306323
func (c *Client) EmitIssueOnExpr(rule tflint.Rule, message string, expr hcl.Expression) error {
307324
req := &EmitIssueRequest{

tflint/client/rpc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ type EvalExprResponse struct {
105105
Err error
106106
}
107107

108+
// IsNullExprRequest is a request to the server-side IsNullExpr method.
109+
type IsNullExprRequest struct {
110+
Expr []byte
111+
Range hcl.Range
112+
}
113+
114+
// IsNullExprResponse is a response to the server-side IsNullExpr method.
115+
type IsNullExprResponse struct {
116+
Ret bool
117+
Err error
118+
}
119+
108120
// EmitIssueRequest is a request to the server-side EmitIssue method.
109121
type EmitIssueRequest struct {
110122
Rule *Rule

tflint/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type Runner interface {
7272
// Its main use is to evaluate the provider block obtained by the RootProvider method.
7373
EvaluateExprOnRootCtx(expr hcl.Expression, ret interface{}, wantType *cty.Type) error
7474

75+
// IsNullExpr checks whether the passed expression is null or not.
76+
// This returns an error when the passed expression is invalid, occurs evaluation errors, etc.
77+
IsNullExpr(expr hcl.Expression) (bool, error)
78+
7579
// EmitIssue sends an issue with an expression to TFLint. You need to pass the message of the issue and the expression.
7680
EmitIssueOnExpr(rule Rule, message string, expr hcl.Expression) error
7781

0 commit comments

Comments
 (0)