Eval implements the Program interface method.
(input any)
| 369 | |
| 370 | // Eval implements the Program interface method. |
| 371 | func (p *prog) Eval(input any) (out ref.Val, det *EvalDetails, err error) { |
| 372 | // Configure error recovery for unexpected panics during evaluation. Note, the use of named |
| 373 | // return values makes it possible to modify the error response during the recovery |
| 374 | // function. |
| 375 | defer func() { |
| 376 | if r := recover(); r != nil { |
| 377 | switch t := r.(type) { |
| 378 | case interpreter.EvalCancelledError: |
| 379 | err = t |
| 380 | default: |
| 381 | err = fmt.Errorf("internal error: %v", r) |
| 382 | } |
| 383 | } |
| 384 | }() |
| 385 | // Asynchronous calls cannot be resolved by a single-pass evaluation. Reject before doing any |
| 386 | // work (this also covers ContextEval, which delegates here); ConcurrentEval does not call Eval. |
| 387 | if p.hasAsync { |
| 388 | return nil, nil, errAsyncRequiresConcurrentEval |
| 389 | } |
| 390 | // Build a hierarchical activation if there are default vars set. |
| 391 | var frame *interpreter.ExecutionFrame |
| 392 | if f, ok := input.(*interpreter.ExecutionFrame); ok { |
| 393 | frame = f |
| 394 | } else { |
| 395 | frame, err = p.newExecutionFrame(input) |
| 396 | if err != nil { |
| 397 | return nil, nil, err |
| 398 | } |
| 399 | defer frame.Close() |
| 400 | } |
| 401 | if p.observable != nil { |
| 402 | det = &EvalDetails{} |
| 403 | out = p.observable.ObserveExec(frame, func(observed any) { |
| 404 | switch o := observed.(type) { |
| 405 | case interpreter.EvalState: |
| 406 | det.state = o |
| 407 | case *interpreter.CostTracker: |
| 408 | det.costTracker = o |
| 409 | } |
| 410 | }) |
| 411 | } else { |
| 412 | out = p.interpretable.Exec(frame) |
| 413 | } |
| 414 | // The output of an internal Eval may have a value (`v`) that is a types.Err. This step |
| 415 | // translates the CEL value to a Go error response. This interface does not quite match the |
| 416 | // RPC signature which allows for multiple errors to be returned, but should be sufficient. |
| 417 | if types.IsError(out) { |
| 418 | err = out.(*types.Err) |
| 419 | } |
| 420 | return |
| 421 | } |
| 422 | |
| 423 | // ContextEval implements the Program interface. |
| 424 | func (p *prog) ContextEval(ctx context.Context, input any) (ref.Val, *EvalDetails, error) { |
no test coverage detected