Skip to content

Commit c053251

Browse files
Introduce cel package aliases for Activation (#1123)
1 parent e086729 commit c053251

File tree

7 files changed

+65
-44
lines changed

7 files changed

+65
-44
lines changed

cel/env.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -502,31 +502,30 @@ func (e *Env) TypeProvider() ref.TypeProvider {
502502
return &interopLegacyTypeProvider{Provider: e.provider}
503503
}
504504

505-
// UnknownVars returns an interpreter.PartialActivation which marks all variables declared in the
506-
// Env as unknown AttributePattern values.
505+
// UnknownVars returns a PartialActivation which marks all variables declared in the Env as
506+
// unknown AttributePattern values.
507507
//
508-
// Note, the UnknownVars will behave the same as an interpreter.EmptyActivation unless the
509-
// PartialAttributes option is provided as a ProgramOption.
510-
func (e *Env) UnknownVars() interpreter.PartialActivation {
508+
// Note, the UnknownVars will behave the same as an cel.NoVars() unless the PartialAttributes
509+
// option is provided as a ProgramOption.
510+
func (e *Env) UnknownVars() PartialActivation {
511511
act := interpreter.EmptyActivation()
512512
part, _ := PartialVars(act, e.computeUnknownVars(act)...)
513513
return part
514514
}
515515

516-
// PartialVars returns an interpreter.PartialActivation where all variables not in the input variable
516+
// PartialVars returns a PartialActivation where all variables not in the input variable
517517
// set, but which have been configured in the environment, are marked as unknown.
518518
//
519-
// The `vars` value may either be an interpreter.Activation or any valid input to the
520-
// interpreter.NewActivation call.
519+
// The `vars` value may either be an Activation or any valid input to the cel.NewActivation call.
521520
//
522521
// Note, this is equivalent to calling cel.PartialVars and manually configuring the set of unknown
523522
// variables. For more advanced use cases of partial state where portions of an object graph, rather
524523
// than top-level variables, are missing the PartialVars() method may be a more suitable choice.
525524
//
526-
// Note, the PartialVars will behave the same as an interpreter.EmptyActivation unless the
527-
// PartialAttributes option is provided as a ProgramOption.
528-
func (e *Env) PartialVars(vars any) (interpreter.PartialActivation, error) {
529-
act, err := interpreter.NewActivation(vars)
525+
// Note, the PartialVars will behave the same as cel.NoVars() unless the PartialAttributes
526+
// option is provided as a ProgramOption.
527+
func (e *Env) PartialVars(vars any) (PartialActivation, error) {
528+
act, err := NewActivation(vars)
530529
if err != nil {
531530
return nil, err
532531
}
@@ -708,7 +707,7 @@ func (e *Env) maybeApplyFeature(feature int, option EnvOption) (*Env, error) {
708707

709708
// computeUnknownVars determines a set of missing variables based on the input activation and the
710709
// environment's configured declaration set.
711-
func (e *Env) computeUnknownVars(vars interpreter.Activation) []*interpreter.AttributePattern {
710+
func (e *Env) computeUnknownVars(vars Activation) []*interpreter.AttributePattern {
712711
var unknownPatterns []*interpreter.AttributePattern
713712
for _, v := range e.variables {
714713
varName := v.Name()

cel/options.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ func Functions(funcs ...*functions.Overload) ProgramOption {
401401
// variables with the same name provided to the Eval() call. If Globals is used in a Library with
402402
// a Lib EnvOption, vars may shadow variables provided by previously added libraries.
403403
//
404-
// The vars value may either be an `interpreter.Activation` instance or a `map[string]any`.
404+
// The vars value may either be an `cel.Activation` instance or a `map[string]any`.
405405
func Globals(vars any) ProgramOption {
406406
return func(p *prog) (*prog, error) {
407-
defaultVars, err := interpreter.NewActivation(vars)
407+
defaultVars, err := NewActivation(vars)
408408
if err != nil {
409409
return nil, err
410410
}
@@ -588,7 +588,7 @@ func DeclareContextProto(descriptor protoreflect.MessageDescriptor) EnvOption {
588588
//
589589
// Consider using with `DeclareContextProto` to simplify variable type declarations and publishing when using
590590
// protocol buffers.
591-
func ContextProtoVars(ctx proto.Message) (interpreter.Activation, error) {
591+
func ContextProtoVars(ctx proto.Message) (Activation, error) {
592592
if ctx == nil || !ctx.ProtoReflect().IsValid() {
593593
return interpreter.EmptyActivation(), nil
594594
}
@@ -612,7 +612,7 @@ func ContextProtoVars(ctx proto.Message) (interpreter.Activation, error) {
612612
}
613613
vars[field.TextName()] = fieldVal
614614
}
615-
return interpreter.NewActivation(vars)
615+
return NewActivation(vars)
616616
}
617617

618618
// EnableMacroCallTracking ensures that call expressions which are replaced by macros

cel/program.go

+40-16
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
type Program interface {
3030
// Eval returns the result of an evaluation of the Ast and environment against the input vars.
3131
//
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`.
3333
//
3434
// If the `OptTrackState`, `OptTrackCost` or `OptExhaustiveEval` flags are used, the `details` response will
3535
// be non-nil. Given this caveat on `details`, the return state from evaluation will be:
@@ -47,14 +47,39 @@ type Program interface {
4747
// to support cancellation and timeouts. This method must be used in conjunction with the
4848
// InterruptCheckFrequency() option for cancellation interrupts to be impact evaluation.
4949
//
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`.
5151
//
5252
// The output contract for `ContextEval` is otherwise identical to the `Eval` method.
5353
ContextEval(context.Context, any) (ref.Val, *EvalDetails, error)
5454
}
5555

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+
5681
// NoVars returns an empty Activation.
57-
func NoVars() interpreter.Activation {
82+
func NoVars() Activation {
5883
return interpreter.EmptyActivation()
5984
}
6085

@@ -64,10 +89,9 @@ func NoVars() interpreter.Activation {
6489
// This method relies on manually configured sets of missing attribute patterns. For a method which
6590
// infers the missing variables from the input and the configured environment, use Env.PartialVars().
6691
//
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.
6993
func PartialVars(vars any,
70-
unknowns ...*interpreter.AttributePattern) (interpreter.PartialActivation, error) {
94+
unknowns ...*interpreter.AttributePattern) (PartialActivation, error) {
7195
return interpreter.NewPartialActivation(vars, unknowns...)
7296
}
7397

@@ -120,7 +144,7 @@ func (ed *EvalDetails) ActualCost() *uint64 {
120144
type prog struct {
121145
*Env
122146
evalOpts EvalOption
123-
defaultVars interpreter.Activation
147+
defaultVars Activation
124148
dispatcher interpreter.Dispatcher
125149
interpreter interpreter.Interpreter
126150
interruptCheckFrequency uint
@@ -285,9 +309,9 @@ func (p *prog) Eval(input any) (v ref.Val, det *EvalDetails, err error) {
285309
}
286310
}()
287311
// Build a hierarchical activation if there are default vars set.
288-
var vars interpreter.Activation
312+
var vars Activation
289313
switch v := input.(type) {
290-
case interpreter.Activation:
314+
case Activation:
291315
vars = v
292316
case map[string]any:
293317
vars = activationPool.Setup(v)
@@ -315,9 +339,9 @@ func (p *prog) ContextEval(ctx context.Context, input any) (ref.Val, *EvalDetail
315339
}
316340
// Configure the input, making sure to wrap Activation inputs in the special ctxActivation which
317341
// exposes the #interrupted variable and manages rate-limited checks of the ctx.Done() state.
318-
var vars interpreter.Activation
342+
var vars Activation
319343
switch v := input.(type) {
320-
case interpreter.Activation:
344+
case Activation:
321345
vars = ctxActivationPool.Setup(v, ctx.Done(), p.interruptCheckFrequency)
322346
defer ctxActivationPool.Put(vars)
323347
case map[string]any:
@@ -414,7 +438,7 @@ func (gen *progGen) ContextEval(ctx context.Context, input any) (ref.Val, *EvalD
414438
}
415439

416440
type ctxEvalActivation struct {
417-
parent interpreter.Activation
441+
parent Activation
418442
interrupt <-chan struct{}
419443
interruptCheckCount uint
420444
interruptCheckFrequency uint
@@ -438,7 +462,7 @@ func (a *ctxEvalActivation) ResolveName(name string) (any, bool) {
438462
return a.parent.ResolveName(name)
439463
}
440464

441-
func (a *ctxEvalActivation) Parent() interpreter.Activation {
465+
func (a *ctxEvalActivation) Parent() Activation {
442466
return a.parent
443467
}
444468

@@ -457,7 +481,7 @@ type ctxEvalActivationPool struct {
457481
}
458482

459483
// 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 {
461485
a := p.Pool.Get().(*ctxEvalActivation)
462486
a.parent = vars
463487
a.interrupt = done
@@ -506,8 +530,8 @@ func (a *evalActivation) ResolveName(name string) (any, bool) {
506530
}
507531
}
508532

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 {
511535
return nil
512536
}
513537

ext/bindings.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (b *dynamicBlock) ID() int64 {
224224
}
225225

226226
// Eval implements the Interpretable interface method.
227-
func (b *dynamicBlock) Eval(activation interpreter.Activation) ref.Val {
227+
func (b *dynamicBlock) Eval(activation cel.Activation) ref.Val {
228228
sa := b.slotActivationPool.Get().(*dynamicSlotActivation)
229229
sa.Activation = activation
230230
defer b.clearSlots(sa)
@@ -242,7 +242,7 @@ type slotVal struct {
242242
}
243243

244244
type dynamicSlotActivation struct {
245-
interpreter.Activation
245+
cel.Activation
246246
slotExprs []interpreter.Interpretable
247247
slotCount int
248248
slotVals []*slotVal
@@ -295,13 +295,13 @@ func (b *constantBlock) ID() int64 {
295295

296296
// Eval implements the interpreter.Interpretable interface method, and will proxy @index prefixed variable
297297
// lookups into a set of constant slots determined from the plan step.
298-
func (b *constantBlock) Eval(activation interpreter.Activation) ref.Val {
298+
func (b *constantBlock) Eval(activation cel.Activation) ref.Val {
299299
vars := constantSlotActivation{Activation: activation, slots: b.slots, slotCount: b.slotCount}
300300
return b.expr.Eval(vars)
301301
}
302302

303303
type constantSlotActivation struct {
304-
interpreter.Activation
304+
cel.Activation
305305
slots traits.Lister
306306
slotCount int
307307
}

repl/BUILD.bazel

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ go_library(
3434
"//common/types:go_default_library",
3535
"//common/types/ref:go_default_library",
3636
"//ext:go_default_library",
37-
"//interpreter:go_default_library",
3837
"//repl/parser:go_default_library",
3938
"@com_github_antlr4_go_antlr_v4//:go_default_library",
4039
"@dev_cel_expr//conformance/proto2:go_default_library",
@@ -46,7 +45,7 @@ go_library(
4645
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
4746
"@org_golang_google_protobuf//reflect/protodesc:go_default_library",
4847
"@org_golang_google_protobuf//types/descriptorpb:go_default_library",
49-
],
48+
],
5049
)
5150

5251
go_test(

repl/evaluator.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/google/cel-go/common/types"
2929
"github.com/google/cel-go/common/types/ref"
3030
"github.com/google/cel-go/ext"
31-
"github.com/google/cel-go/interpreter"
3231

3332
"google.golang.org/protobuf/encoding/prototext"
3433
"google.golang.org/protobuf/proto"
@@ -666,7 +665,7 @@ func (e *Evaluator) Status() string {
666665
// applyContext evaluates the let expressions in the context to build an activation for the given expression.
667666
// returns the environment for compiling and planning the top level CEL expression and an activation with the
668667
// values of the let expressions.
669-
func (e *Evaluator) applyContext() (*cel.Env, interpreter.Activation, error) {
668+
func (e *Evaluator) applyContext() (*cel.Env, cel.Activation, error) {
670669
var vars = make(map[string]any)
671670

672671
for _, el := range e.ctx.letVars {
@@ -683,7 +682,7 @@ func (e *Evaluator) applyContext() (*cel.Env, interpreter.Activation, error) {
683682
}
684683
}
685684

686-
act, err := interpreter.NewActivation(vars)
685+
act, err := cel.NewActivation(vars)
687686
if err != nil {
688687
return nil, nil, err
689688
}

test/bench/bench.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Case struct {
3333
// Options indicate additional pieces of configuration such as CEL libraries, variables, and functions.
3434
Options []cel.EnvOption
3535

36-
// In is expected to be a map[string]any or interpreter.Activation instance representing the input to the expression.
36+
// In is expected to be a map[string]any or cel.Activation instance representing the input to the expression.
3737
In any
3838

3939
// Out is the expected CEL valued output.
@@ -48,7 +48,7 @@ type DynamicEnvCase struct {
4848
// Options indicate additional pieces of configuration such as CEL libraries, variables, and functions.
4949
Options func(b *testing.B) *cel.Env
5050

51-
// In is expected to be a map[string]any or interpreter.Activation instance representing the input to the expression.
51+
// In is expected to be a map[string]any or cel.Activation instance representing the input to the expression.
5252
In any
5353

5454
// Out is the expected CEL valued output.

0 commit comments

Comments
 (0)