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