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

Method Eval

cel/program.go:473–536  ·  view source on GitHub ↗

Eval implements the Program interface method.

(input any)

Source from the content-addressed store, hash-verified

471
472// Eval implements the Program interface method.
473func (p *prog) Eval(input any) (out ref.Val, det *EvalDetails, err error) {
474 if p == nil {
475 return nil, nil, errors.New("program is nil")
476 }
477 // Asynchronous calls cannot be resolved by a single-pass evaluation. Reject before doing any
478 // work (this also covers ContextEval, which delegates here); ConcurrentEval does not call Eval.
479 if p.hasAsync {
480 return nil, nil, errAsyncRequiresConcurrentEval
481 }
482 // Build a hierarchical activation if there are default vars set.
483 var frame *interpreter.ExecutionFrame
484 var mustClose bool
485 if f, ok := input.(*interpreter.ExecutionFrame); ok {
486 frame = f
487 } else {
488 frame, err = p.newExecutionFrame(input)
489 if err != nil {
490 return nil, nil, err
491 }
492 mustClose = true
493 }
494 // Configure error recovery and details capture for evaluation.
495 defer func() {
496 det = captureCostTracker(det, frame)
497 if r := recover(); r != nil {
498 switch t := r.(type) {
499 case interpreter.EvalCancelledError:
500 err = t
501 case cost.LimitExceededError:
502 err = interpreter.EvalCancelledError{
503 Cause: interpreter.CostLimitExceeded,
504 Message: t.Message,
505 }
506 default:
507 err = fmt.Errorf("internal error: %v", r)
508 }
509 }
510 if mustClose {
511 frame.Close()
512 }
513 }()
514
515 if p.observable != nil {
516 det = &EvalDetails{}
517 out = p.observable.ObserveExec(frame, func(observed any) {
518 switch o := observed.(type) {
519 case interpreter.EvalState:
520 det.state = o
521 case *types.MemoryTracker:
522 det.memTracker = o
523 }
524 })
525 } else {
526 out = p.interpretable.Exec(frame)
527 }
528
529 // The output of an internal Eval may have a value (`v`) that is a types.Err. This step
530 // translates the CEL value to a Go error response. This interface does not quite match the

Callers 1

ContextEvalMethod · 0.95

Calls 7

newExecutionFrameMethod · 0.95
CloseMethod · 0.95
IsErrorFunction · 0.92
captureCostTrackerFunction · 0.85
NewMethod · 0.80
ObserveExecMethod · 0.80
ExecMethod · 0.65

Tested by

no test coverage detected