Skip to content

Commit

Permalink
Merge pull request #68 from fabriziosestito/feat/lazy-variables
Browse files Browse the repository at this point in the history
feat: variables lazy evaluation
  • Loading branch information
fabriziosestito authored Jul 12, 2024
2 parents a36055f + cefa602 commit 14bb4b9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/kubewarden/cel-policy/internal/cel"
"github.com/kubewarden/cel-policy/internal/settings"
kubewarden "github.com/kubewarden/policy-sdk-go"
Expand Down Expand Up @@ -83,16 +84,19 @@ func evalVariables(compiler *cel.Compiler, vars map[string]interface{}, variable
return err
}

val, err := compiler.EvalCELExpression(vars, ast)
if err != nil {
if err := compiler.AddVariable(variable.Name, ast.OutputType()); err != nil {
return err
}
vars[fmt.Sprintf("variables.%s", variable.Name)] = func() ref.Val {
val, err := compiler.EvalCELExpression(vars, ast)
if err != nil {
return types.WrapErr(err)
}

if err := compiler.AddVariable(variable.Name, ast.OutputType()); err != nil {
return err
return val
}
vars[fmt.Sprintf("variables.%s", variable.Name)] = val
}

return nil
}

Expand Down

0 comments on commit 14bb4b9

Please sign in to comment.