isOptimizableMapInsert mirrors cel-cpp's IsOptimizableMapInsert. Returns whether this comprehension appears to be a macro implementation for map transformations.
(fold ast.ComprehensionExpr)
| 778 | // Returns whether this comprehension appears to be a macro implementation for map |
| 779 | // transformations. |
| 780 | func isOptimizableMapInsert(fold ast.ComprehensionExpr) bool { |
| 781 | if fold.IterVar() == "" || fold.IterVar2() == "" { |
| 782 | return false |
| 783 | } |
| 784 | accuVar := fold.AccuVar() |
| 785 | if accuVar == "" || !isIdentNamed(fold.Result(), accuVar) { |
| 786 | return false |
| 787 | } |
| 788 | if fold.AccuInit().Kind() != ast.MapKind || fold.AccuInit().AsMap().Size() != 0 { |
| 789 | return false |
| 790 | } |
| 791 | step := fold.LoopStep() |
| 792 | if step.Kind() != ast.CallKind { |
| 793 | return false |
| 794 | } |
| 795 | call := step.AsCall() |
| 796 | if call.FunctionName() == operators.Conditional && len(call.Args()) == 3 { |
| 797 | branch := call.Args()[1] |
| 798 | if branch.Kind() != ast.CallKind { |
| 799 | return false |
| 800 | } |
| 801 | call = branch.AsCall() |
| 802 | } |
| 803 | args := call.Args() |
| 804 | return call.FunctionName() == mapInsertFunction && |
| 805 | (len(args) == 2 || len(args) == 3) && |
| 806 | isIdentNamed(args[0], accuVar) |
| 807 | } |
| 808 | |
| 809 | // isIdentNamed reports whether the expression is an identifier with the given name. |
| 810 | func isIdentNamed(e ast.Expr, name string) bool { |
no test coverage detected