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

Method Optimize

ext/sets.go:147–167  ·  view source on GitHub ↗
(ctx *cel.OptimizerContext, a *ast.AST)

Source from the content-addressed store, hash-verified

145}
146
147func (setsLib) Optimize(ctx *cel.OptimizerContext, a *ast.AST) *ast.AST {
148 root := ast.NavigateAST(a)
149 matches := ast.MatchDescendants(root, matchInConstantList(a))
150 for _, match := range matches {
151 call := match.AsCall()
152 listArg := call.Args()[1]
153 entries := make([]ast.EntryExpr, len(listArg.AsList().Elements()))
154 for i, elem := range listArg.AsList().Elements() {
155 var entry ast.EntryExpr
156 if r, found := a.ReferenceMap()[elem.ID()]; found && r.Value != nil {
157 entry = ctx.NewMapEntry(ctx.NewLiteral(r.Value), ctx.NewLiteral(types.True), false)
158 } else {
159 entry = ctx.NewMapEntry(elem, ctx.NewLiteral(types.True), false)
160 }
161 entries[i] = entry
162 }
163 mapArg := ctx.NewMap(entries)
164 ctx.UpdateExpr(listArg, mapArg)
165 }
166 return a
167}
168
169func matchInConstantList(a *ast.AST) ast.ExprMatcher {
170 return func(e ast.NavigableExpr) bool {

Callers

nothing calls this directly

Calls 13

NavigateASTFunction · 0.92
MatchDescendantsFunction · 0.92
matchInConstantListFunction · 0.85
ReferenceMapMethod · 0.80
UpdateExprMethod · 0.80
AsCallMethod · 0.65
ArgsMethod · 0.65
ElementsMethod · 0.65
AsListMethod · 0.65
IDMethod · 0.65
NewMapEntryMethod · 0.65
NewLiteralMethod · 0.65

Tested by

no test coverage detected