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

Function constantCallMatcher

cel/folding.go:577–640  ·  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

575
576// constantCallMatcher identifies strict and non-strict calls which can be folded.
577func constantCallMatcher(e ast.NavigableExpr) bool {
578 call := e.AsCall()
579 children := e.Children()
580 fnName := call.FunctionName()
581 if fnName == operators.LogicalAnd {
582 for _, child := range children {
583 if child.Kind() == ast.LiteralKind {
584 return true
585 }
586 }
587 }
588 if fnName == operators.LogicalOr {
589 for _, child := range children {
590 if child.Kind() == ast.LiteralKind {
591 return true
592 }
593 }
594 }
595 if fnName == operators.Conditional {
596 cond := children[0]
597 if cond.Kind() == ast.LiteralKind && cond.AsLiteral().Type() == types.BoolType {
598 return true
599 }
600 }
601 if fnName == operators.Equals || fnName == operators.NotEquals {
602 if hasComprehensionVar(e) {
603 return false
604 }
605 if isExprConstantOfKind(children[0], types.BoolType) || isExprConstantOfKind(children[1], types.BoolType) {
606 return true
607 }
608 }
609 if fnName == operators.In {
610 if hasComprehensionVar(e) {
611 return false
612 }
613 haystack := children[1]
614 if haystack.Kind() == ast.ListKind && haystack.AsList().Size() == 0 {
615 return true
616 }
617 needle := children[0]
618 if (needle.Kind() == ast.LiteralKind || isSelfEqualIdent(needle)) && haystack.Kind() == ast.ListKind {
619 needleIsLit := needle.Kind() == ast.LiteralKind
620 needleLitVal := needle.AsLiteral()
621 needleIdentVal := needle.AsIdent()
622 list := haystack.AsList()
623 for _, elem := range list.Elements() {
624 if needleIsLit && elem.Kind() == ast.LiteralKind && elem.AsLiteral().Equal(needleLitVal) == types.True {
625 return true
626 }
627 if !needleIsLit && elem.Kind() == ast.IdentKind && elem.AsIdent() == needleIdentVal {
628 return true
629 }
630 }
631 }
632 }
633 // convert all other calls with constant arguments
634 for _, child := range children {

Callers 1

constantExprMatcherMethod · 0.85

Calls 14

hasComprehensionVarFunction · 0.85
isExprConstantOfKindFunction · 0.85
isSelfEqualIdentFunction · 0.85
AsCallMethod · 0.65
ChildrenMethod · 0.65
FunctionNameMethod · 0.65
KindMethod · 0.65
TypeMethod · 0.65
AsLiteralMethod · 0.65
SizeMethod · 0.65
AsListMethod · 0.65
AsIdentMethod · 0.65

Tested by

no test coverage detected