Optimize implements an AST optimizer for CEL which composes an expression graph into a single expression value.
(ctx *cel.OptimizerContext, a *ast.AST)
| 157 | // Optimize implements an AST optimizer for CEL which composes an expression graph into a single |
| 158 | // expression value. |
| 159 | func (opt *ruleComposerImpl) Optimize(ctx *cel.OptimizerContext, a *ast.AST) *ast.AST { |
| 160 | // The input to optimize is a dummy expression which is completely replaced according |
| 161 | // to the configuration of the rule composition graph. |
| 162 | ruleExpr := opt.optimizeRule(ctx, opt.rule) |
| 163 | |
| 164 | // If there were no variables, return the expression. |
| 165 | if len(opt.varIndices) == 0 { |
| 166 | return ctx.NewAST(ruleExpr) |
| 167 | } |
| 168 | |
| 169 | // Otherwise populate the cel.@block with the variable declarations and wrap the expression |
| 170 | // in the block. |
| 171 | varExprs := make([]ast.Expr, len(opt.varIndices)) |
| 172 | for i, vi := range opt.varIndices { |
| 173 | varExprs[i] = vi.expr |
| 174 | err := ctx.ExtendEnv(cel.Variable(vi.indexVar, vi.celType)) |
| 175 | if err != nil { |
| 176 | ctx.ReportErrorAtID(ruleExpr.ID(), "%s", err.Error()) |
| 177 | } |
| 178 | } |
| 179 | blockExpr := ctx.NewCall("cel.@block", ctx.NewList(varExprs, []int32{}), ruleExpr) |
| 180 | return ctx.NewAST(blockExpr) |
| 181 | } |
| 182 | |
| 183 | func (opt *ruleComposerImpl) optimizeRule(ctx *cel.OptimizerContext, r *CompiledRule) ast.Expr { |
| 184 | // Visitor to rewrite variables-prefixed identifiers with index names. |
nothing calls this directly
no test coverage detected