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