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

Function constantCallMatcher

cel/folding.go:615–668  ·  view source on GitHub ↗

constantCallMatcher identifies strict and non-strict calls which can be folded.

(e ast.NavigableExpr)

Source from the content-addressed store, hash-verified

613
614// constantCallMatcher identifies strict and non-strict calls which can be folded.
615func constantCallMatcher(e ast.NavigableExpr) bool {
616 call := e.AsCall()
617 children := e.Children()
618 fnName := call.FunctionName()
619 if fnName == operators.LogicalAnd {
620 for _, child := range children {
621 if child.Kind() == ast.LiteralKind {
622 return true
623 }
624 }
625 }
626 if fnName == operators.LogicalOr {
627 for _, child := range children {
628 if child.Kind() == ast.LiteralKind {
629 return true
630 }
631 }
632 }
633 if fnName == operators.Conditional {
634 cond := children[0]
635 if cond.Kind() == ast.LiteralKind && cond.AsLiteral().Type() == types.BoolType {
636 return true
637 }
638 }
639 if fnName == operators.Equals || fnName == operators.NotEquals {
640 if hasComprehensionVar(e) {
641 return false
642 }
643 if isExprConstantOfKind(children[0], types.BoolType) || isExprConstantOfKind(children[1], types.BoolType) {
644 return true
645 }
646 }
647 if fnName == operators.In {
648 if hasComprehensionVar(e) {
649 return false
650 }
651 haystack := children[1]
652 if isCollectionEmpty(haystack) || collectionContainsNeedle(children[0], haystack) {
653 return true
654 }
655 }
656 if fnName == operators.Add {
657 if len(children) == 2 && children[0].Kind() == ast.ListKind && children[1].Kind() == ast.ListKind {
658 return true
659 }
660 }
661 // convert all other calls with constant arguments
662 for _, child := range children {
663 if !constantMatcher(child) {
664 return false
665 }
666 }
667 return true
668}
669
670func isCollectionEmpty(e ast.Expr) bool {
671 switch e.Kind() {

Callers 1

constantExprMatcherMethod · 0.85

Calls 10

hasComprehensionVarFunction · 0.85
isExprConstantOfKindFunction · 0.85
isCollectionEmptyFunction · 0.85
collectionContainsNeedleFunction · 0.85
AsCallMethod · 0.65
ChildrenMethod · 0.65
FunctionNameMethod · 0.65
KindMethod · 0.65
TypeMethod · 0.65
AsLiteralMethod · 0.65

Tested by

no test coverage detected