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

Method matchVariable

cel/inlining.go:242–267  ·  view source on GitHub ↗

matchVariable matches simple identifiers, select expressions, and presence test expressions which match the (potentially) qualified variable name provided as input. Note, this function does not support inlining against select expressions which includes optional field selection. This may be a future

(varName string)

Source from the content-addressed store, hash-verified

240// Note, this function does not support inlining against select expressions which includes optional
241// field selection. This may be a future refinement.
242func (opt *inliningOptimizer) matchVariable(varName string) ast.ExprMatcher {
243 return func(e ast.NavigableExpr) bool {
244 name, found := maybeAsVariableName(e)
245 if !found || name != varName {
246 return false
247 }
248
249 // Determine whether the variable being referenced has been shadowed by a comprehension
250 p, hasParent := e.Parent()
251 for hasParent {
252 if p.Kind() != ast.ComprehensionKind {
253 p, hasParent = p.Parent()
254 continue
255 }
256 // If the inline variable name matches any of the comprehension variables at any scope,
257 // return false as the variable has been shadowed.
258 compre := p.AsComprehension()
259 if varName == compre.AccuVar() || varName == compre.IterVar() || varName == compre.IterVar2() {
260 return false
261 }
262 p, hasParent = p.Parent()
263 }
264
265 return true
266 }
267}
268
269func maybeAsVariableName(e ast.NavigableExpr) (string, bool) {
270 if e.Kind() == ast.IdentKind {

Callers 1

OptimizeMethod · 0.95

Calls 7

maybeAsVariableNameFunction · 0.85
ParentMethod · 0.65
KindMethod · 0.65
AsComprehensionMethod · 0.65
AccuVarMethod · 0.65
IterVarMethod · 0.65
IterVar2Method · 0.65

Tested by

no test coverage detected