@@ -29,7 +29,7 @@ import (
29
29
type Program interface {
30
30
// Eval returns the result of an evaluation of the Ast and environment against the input vars.
31
31
//
32
- // The vars value may either be an `interpreter. Activation` or a `map[string]any`.
32
+ // The vars value may either be an `Activation` or a `map[string]any`.
33
33
//
34
34
// If the `OptTrackState`, `OptTrackCost` or `OptExhaustiveEval` flags are used, the `details` response will
35
35
// be non-nil. Given this caveat on `details`, the return state from evaluation will be:
@@ -47,14 +47,39 @@ type Program interface {
47
47
// to support cancellation and timeouts. This method must be used in conjunction with the
48
48
// InterruptCheckFrequency() option for cancellation interrupts to be impact evaluation.
49
49
//
50
- // The vars value may either be an `interpreter. Activation` or `map[string]any`.
50
+ // The vars value may either be an `Activation` or `map[string]any`.
51
51
//
52
52
// The output contract for `ContextEval` is otherwise identical to the `Eval` method.
53
53
ContextEval (context.Context , any ) (ref.Val , * EvalDetails , error )
54
54
}
55
55
56
+ // Activation used to resolve identifiers by name and references by id.
57
+ //
58
+ // An Activation is the primary mechanism by which a caller supplies input into a CEL program.
59
+ type Activation = interpreter.Activation
60
+
61
+ // NewActivation returns an activation based on a map-based binding where the map keys are
62
+ // expected to be qualified names used with ResolveName calls.
63
+ //
64
+ // The input `bindings` may either be of type `Activation` or `map[string]any`.
65
+ //
66
+ // Lazy bindings may be supplied within the map-based input in either of the following forms:
67
+ // - func() any
68
+ // - func() ref.Val
69
+ //
70
+ // The output of the lazy binding will overwrite the variable reference in the internal map.
71
+ //
72
+ // Values which are not represented as ref.Val types on input may be adapted to a ref.Val using
73
+ // the types.Adapter configured in the environment.
74
+ func NewActivation (bindings any ) (Activation , error ) {
75
+ return interpreter .NewActivation (bindings )
76
+ }
77
+
78
+ // PartialActivation extends the Activation interface with a set of UnknownAttributePatterns.
79
+ type PartialActivation = interpreter.PartialActivation
80
+
56
81
// NoVars returns an empty Activation.
57
- func NoVars () interpreter. Activation {
82
+ func NoVars () Activation {
58
83
return interpreter .EmptyActivation ()
59
84
}
60
85
@@ -64,10 +89,9 @@ func NoVars() interpreter.Activation {
64
89
// This method relies on manually configured sets of missing attribute patterns. For a method which
65
90
// infers the missing variables from the input and the configured environment, use Env.PartialVars().
66
91
//
67
- // The `vars` value may either be an interpreter.Activation or any valid input to the
68
- // interpreter.NewActivation call.
92
+ // The `vars` value may either be an Activation or any valid input to the NewActivation call.
69
93
func PartialVars (vars any ,
70
- unknowns ... * interpreter.AttributePattern ) (interpreter. PartialActivation , error ) {
94
+ unknowns ... * interpreter.AttributePattern ) (PartialActivation , error ) {
71
95
return interpreter .NewPartialActivation (vars , unknowns ... )
72
96
}
73
97
@@ -120,7 +144,7 @@ func (ed *EvalDetails) ActualCost() *uint64 {
120
144
type prog struct {
121
145
* Env
122
146
evalOpts EvalOption
123
- defaultVars interpreter. Activation
147
+ defaultVars Activation
124
148
dispatcher interpreter.Dispatcher
125
149
interpreter interpreter.Interpreter
126
150
interruptCheckFrequency uint
@@ -285,9 +309,9 @@ func (p *prog) Eval(input any) (v ref.Val, det *EvalDetails, err error) {
285
309
}
286
310
}()
287
311
// Build a hierarchical activation if there are default vars set.
288
- var vars interpreter. Activation
312
+ var vars Activation
289
313
switch v := input .(type ) {
290
- case interpreter. Activation :
314
+ case Activation :
291
315
vars = v
292
316
case map [string ]any :
293
317
vars = activationPool .Setup (v )
@@ -315,9 +339,9 @@ func (p *prog) ContextEval(ctx context.Context, input any) (ref.Val, *EvalDetail
315
339
}
316
340
// Configure the input, making sure to wrap Activation inputs in the special ctxActivation which
317
341
// exposes the #interrupted variable and manages rate-limited checks of the ctx.Done() state.
318
- var vars interpreter. Activation
342
+ var vars Activation
319
343
switch v := input .(type ) {
320
- case interpreter. Activation :
344
+ case Activation :
321
345
vars = ctxActivationPool .Setup (v , ctx .Done (), p .interruptCheckFrequency )
322
346
defer ctxActivationPool .Put (vars )
323
347
case map [string ]any :
@@ -414,7 +438,7 @@ func (gen *progGen) ContextEval(ctx context.Context, input any) (ref.Val, *EvalD
414
438
}
415
439
416
440
type ctxEvalActivation struct {
417
- parent interpreter. Activation
441
+ parent Activation
418
442
interrupt <- chan struct {}
419
443
interruptCheckCount uint
420
444
interruptCheckFrequency uint
@@ -438,7 +462,7 @@ func (a *ctxEvalActivation) ResolveName(name string) (any, bool) {
438
462
return a .parent .ResolveName (name )
439
463
}
440
464
441
- func (a * ctxEvalActivation ) Parent () interpreter. Activation {
465
+ func (a * ctxEvalActivation ) Parent () Activation {
442
466
return a .parent
443
467
}
444
468
@@ -457,7 +481,7 @@ type ctxEvalActivationPool struct {
457
481
}
458
482
459
483
// Setup initializes a pooled Activation with the ability check for context.Context cancellation
460
- func (p * ctxEvalActivationPool ) Setup (vars interpreter. Activation , done <- chan struct {}, interruptCheckRate uint ) * ctxEvalActivation {
484
+ func (p * ctxEvalActivationPool ) Setup (vars Activation , done <- chan struct {}, interruptCheckRate uint ) * ctxEvalActivation {
461
485
a := p .Pool .Get ().(* ctxEvalActivation )
462
486
a .parent = vars
463
487
a .interrupt = done
@@ -506,8 +530,8 @@ func (a *evalActivation) ResolveName(name string) (any, bool) {
506
530
}
507
531
}
508
532
509
- // Parent implements the interpreter. Activation interface
510
- func (a * evalActivation ) Parent () interpreter. Activation {
533
+ // Parent implements the Activation interface
534
+ func (a * evalActivation ) Parent () Activation {
511
535
return nil
512
536
}
513
537
0 commit comments