MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Optimize

Method Optimize

policy/composer.go:159–181  ·  view source on GitHub ↗

Optimize implements an AST optimizer for CEL which composes an expression graph into a single expression value.

(ctx *cel.OptimizerContext, a *ast.AST)

Source from the content-addressed store, hash-verified

157// Optimize implements an AST optimizer for CEL which composes an expression graph into a single
158// expression value.
159func (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
183func (opt *ruleComposerImpl) optimizeRule(ctx *cel.OptimizerContext, r *CompiledRule) ast.Expr {
184 // Visitor to rewrite variables-prefixed identifiers with index names.

Callers

nothing calls this directly

Calls 9

optimizeRuleMethod · 0.95
VariableFunction · 0.92
NewASTMethod · 0.80
ExtendEnvMethod · 0.80
ReportErrorAtIDMethod · 0.65
IDMethod · 0.65
NewCallMethod · 0.65
NewListMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected