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

Method UpdateExpr

cel/optimizer.go:505–570  ·  view source on GitHub ↗

UpdateExpr updates the target expression with the updated content while preserving macro metadata. There are four scenarios during the update to consider: 1. target is not macro, updated is not macro 2. target is macro, updated is not macro 3. target is macro, updated is macro 4. target is not macr

(target, updated ast.Expr)

Source from the content-addressed store, hash-verified

503// updated to point to the new updated macro. Otherwise, other macros which pointed to
504// the target body must be replaced with copies of the updated expression body.
505func (opt *optimizerExprFactory) UpdateExpr(target, updated ast.Expr) {
506 // Update the expression
507 target.SetKindCase(updated)
508
509 // Early return if there's no macros present sa the source info reflects the
510 // macro set from the target and updated expressions.
511 if len(opt.sourceInfo.MacroCalls()) == 0 {
512 return
513 }
514 // Determine whether the target expression was a macro.
515 _, targetIsMacro := opt.sourceInfo.GetMacroCall(target.ID())
516
517 // Determine whether the updated expression was a macro.
518 updatedMacro, updatedIsMacro := opt.sourceInfo.GetMacroCall(updated.ID())
519
520 if updatedIsMacro {
521 // If the updated call was a macro, then updated id maps to target id,
522 // and the updated macro moves into the target id slot.
523 opt.sourceInfo.ClearMacroCall(updated.ID())
524 opt.sourceInfo.SetMacroCall(target.ID(), updatedMacro)
525 } else if targetIsMacro {
526 // Otherwise if the target expr was a macro, but is no longer, clear
527 // the macro reference.
528 opt.sourceInfo.ClearMacroCall(target.ID())
529 }
530
531 // Punch holes in the updated value where macros references exist.
532 macroExpr := opt.fac.CopyExpr(target)
533 macroRefVisitor := ast.NewExprVisitor(func(e ast.Expr) {
534 if _, exists := opt.sourceInfo.GetMacroCall(e.ID()); exists {
535 e.SetKindCase(nil)
536 }
537 })
538 ast.PostOrderVisit(macroExpr, macroRefVisitor)
539
540 // Update any references to the expression within a macro
541 macroVisitor := ast.NewExprVisitor(func(call ast.Expr) {
542 // Update the target expression to point to the macro expression which
543 // will be empty if the updated expression was a macro.
544 if call.ID() == target.ID() {
545 call.SetKindCase(opt.fac.CopyExpr(macroExpr))
546 }
547 // Update the macro call expression if it refers to the updated expression
548 // id which has since been remapped to the target id.
549 if call.ID() == updated.ID() {
550 // Either ensure the expression is a macro reference or a populated with
551 // the relevant sub-expression if the updated expr was not a macro.
552 if updatedIsMacro {
553 call.SetKindCase(nil)
554 } else {
555 call.SetKindCase(opt.fac.CopyExpr(macroExpr))
556 }
557 // Since SetKindCase does not renumber the id, ensure the references to
558 // the old 'updated' id are mapped to the target id.
559 call.RenumberIDs(func(id int64) int64 {
560 if id == updated.ID() {
561 return target.ID()
562 }

Callers 13

rewriteVariableNameMethod · 0.80
OptimizeMethod · 0.80
OptimizeMethod · 0.80
OptimizeMethod · 0.80
tryFoldMethod · 0.80
maybePruneBranchesFunction · 0.80
maybeShortcircuitLogicFunction · 0.80
pruneOptionalMapEntriesFunction · 0.80
inlineExprMethod · 0.80

Calls 10

NewExprVisitorFunction · 0.92
PostOrderVisitFunction · 0.92
GetMacroCallMethod · 0.80
SetKindCaseMethod · 0.65
IDMethod · 0.65
CopyExprMethod · 0.65
RenumberIDsMethod · 0.65
MacroCallsMethod · 0.45
ClearMacroCallMethod · 0.45
SetMacroCallMethod · 0.45

Tested by 1

OptimizeMethod · 0.64