Skip to content

Add support for function callbacks as the target of EvaluateExpr#246

Merged
wata727 merged 1 commit intomasterfrom
eval_expr_callback
Mar 26, 2023
Merged

Add support for function callbacks as the target of EvaluateExpr#246
wata727 merged 1 commit intomasterfrom
eval_expr_callback

Conversation

@wata727
Copy link
Copy Markdown
Member

@wata727 wata727 commented Mar 25, 2023

See also #196 #236

I initially deprecated the EnsureNoError function and thought that the caller should use errors.Is() to determine the error. However, during several works, I came to the conclusion that this would likely cause new confusion.

In this PR, I will make changes to support function callbacks as the target for EvaluateExpr. The evaluated value will be passed as an argument to the callback and executed. In cases where the value cannot be represented as an argument, such as unknown values, NULL, or sensitive values, the callback execution will be aborted instead of returning an error.

In other words, all of the following implementations are equivalent:

var val string
err := runner.EvaluateExpr(expr, &val, nil)
err = runner.EnsureNoError(err, func () error {
  // Test values
})
if err != nil {
  return err
}
var val string
err := runner.EvaluateExpr(expr, &val, nil)
if err != nil {
  if errors.Is(err, tflint.ErrUnknownValue) || errors.Is(err, tflint.ErrNullValue) || errors.Is(err, tflint.Sensitive) {
    return nil
  }
  return err
}
// Test values
err := runner.EvaluateExpr(expr, func (val string), error {
  // Test values
}, nil)

If you are using the EnsureNoError function, you need to switch to the function callback approach to keep the current behavior. If you need to change the behavior based on the type of error, use errors.Is() to check the type of error instead of using EnsureNoError.

@wata727 wata727 merged commit 8f09608 into master Mar 26, 2023
@wata727 wata727 deleted the eval_expr_callback branch March 26, 2023 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant