type ConstantFoldingOption … // MaxConstantFoldIterations limits the number of times literals may be folding during optimization. // // Defaults to 100 if not set. func MaxConstantFoldIterations(limit int) ConstantFoldingOption { … } // NewConstantFoldingOptimizer creates an optimizer which inlines constant scalar an aggregate // literal values within function calls and select statements with their evaluated result. func NewConstantFoldingOptimizer(opts ...ConstantFoldingOption) (ASTOptimizer, error) { … } type constantFoldingOptimizer … // Optimize queries the expression graph for scalar and aggregate literal expressions within call and // select statements and then evaluates them and replaces the call site with the literal result. // // Note: only values which can be represented as literals in CEL syntax are supported. func (opt *constantFoldingOptimizer) Optimize(ctx *OptimizerContext, a *ast.AST) *ast.AST { … } // tryFold attempts to evaluate a sub-expression to a literal. // // If the evaluation succeeds, the input expr value will be modified to become a literal, otherwise // the method will return an error. func tryFold(ctx *OptimizerContext, a *ast.AST, expr ast.Expr) error { … } // maybePruneBranches inspects the non-strict call expression to determine whether // a branch can be removed. Evaluation will naturally prune logical and / or calls, // but conditional will not be pruned cleanly, so this is one small area where the // constant folding step reimplements a portion of the evaluator. func maybePruneBranches(ctx *OptimizerContext, expr ast.NavigableExpr) bool { … } func maybeShortcircuitLogic(ctx *OptimizerContext, function string, args []ast.Expr, expr ast.NavigableExpr) bool { … } // pruneOptionalElements works from the bottom up to resolve optional elements within // aggregate literals. // // Note, many aggregate literals will be resolved as arguments to functions or select // statements, so this method exists to handle the case where the literal could not be // fully resolved or exists outside of a call, select, or comprehension context. func pruneOptionalElements(ctx *OptimizerContext, root ast.NavigableExpr) { … } func pruneOptionalListElements(ctx *OptimizerContext, e ast.Expr) { … } func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) { … } func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { … } // adaptLiteral converts a runtime CEL value to its equivalent literal expression. // // For strongly typed values, the type-provider will be used to reconstruct the fields // which are present in the literal and their equivalent initialization values. func adaptLiteral(ctx *OptimizerContext, val ref.Val) (ast.Expr, error) { … } // constantExprMatcher matches calls, select statements, and comprehensions whose arguments // are all constant scalar or aggregate literal values. // // Only comprehensions which are not nested are included as possible constant folds, and only // if all variables referenced in the comprehension stack exist are only iteration or // accumulation variables. func constantExprMatcher(e ast.NavigableExpr) bool { … } // constantCallMatcher identifies strict and non-strict calls which can be folded. func constantCallMatcher(e ast.NavigableExpr) bool { … } func isNestedComprehension(e ast.NavigableExpr) bool { … } func aggregateLiteralMatcher(e ast.NavigableExpr) bool { … } var constantMatcher … const defaultMaxConstantFoldIterations …