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

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

Merged
merged 1 commit into from
Mar 26, 2023

Conversation

wata727
Copy link
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