expression

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProtoFieldToCELType added in v0.4.2

func ProtoFieldToCELType(fieldDesc protoreflect.FieldDescriptor, generic, forItems bool) *cel.Type

ProtoFieldToCELType resolves the CEL value type for the provided FieldDescriptor. If generic is true, the specific subtypes of map and repeated fields will be replaced with cel.DynType. If forItems is true, the type for the repeated list items is returned instead of the list type itself.

func ProtoKindToCELType added in v0.4.2

func ProtoKindToCELType(kind protoreflect.Kind) *cel.Type

ProtoKindToCELType maps a protoreflect.Kind to a compatible cel.Type.

func RequiredCELEnvOptions added in v0.4.2

func RequiredCELEnvOptions(fieldDesc protoreflect.FieldDescriptor) []cel.EnvOption

RequiredCELEnvOptions returns the options required to have expressions which rely on the provided descriptor.

Types

type ASTSet

type ASTSet struct {
	// contains filtered or unexported fields
}

ASTSet represents a collection of compiledAST and their associated cel.Env.

func CompileASTs

func CompileASTs[T Expression](
	expressions []T,
	env *cel.Env,
	envOpts ...cel.EnvOption,
) (set ASTSet, err error)

CompileASTs parses and type checks a set of expressions, producing a resulting ASTSet. The value can then be converted to a ProgramSet via ASTSet.ToProgramSet or ASTSet.ReduceResiduals. Use Compile instead if no cel.ProgramOption args need to be provided or residuals do not need to be computed.

func (ASTSet) Merge

func (set ASTSet) Merge(other ASTSet) ASTSet

Merge combines a set with another, producing a new ASTSet.

func (ASTSet) ReduceResiduals

func (set ASTSet) ReduceResiduals(opts ...cel.ProgramOption) (ProgramSet, error)

ReduceResiduals generates a ProgramSet, performing a partial evaluation of the ASTSet to optimize the expression. If the expression is optimized to either a true or empty string constant result, no compiledProgram is generated for it. The main usage of this is to elide tautological expressions from the final result.

func (ASTSet) ToProgramSet

func (set ASTSet) ToProgramSet(opts ...cel.ProgramOption) (out ProgramSet, err error)

ToProgramSet generates a ProgramSet from the specified ASTs.

type Expression

type Expression interface {
	GetId() string
	GetMessage() string
	GetExpression() string
}

Expression is the read-only interface of either validate.Constraint or private.Constraint which can be the source of a CEL expression.

type Now

type Now struct {
	// TS is the already resolved timestamp. If unset, the field is populated with
	// timestamppb.Now.
	TS *timestamppb.Timestamp
}

Now implements interpreter.Activation, providing a lazily produced timestamp for accessing the variable `now` that's constant within an evaluation.

func (*Now) Parent

func (n *Now) Parent() interpreter.Activation

func (*Now) ResolveName

func (n *Now) ResolveName(name string) (any, bool)

type NowPool

type NowPool sync.Pool

func (*NowPool) Get

func (p *NowPool) Get() *Now

func (*NowPool) Put

func (p *NowPool) Put(v *Now)

type ProgramSet

type ProgramSet []compiledProgram

ProgramSet is a list of compiledProgram expressions that are evaluated together with the same input value. All expressions in a ProgramSet may refer to a `this` variable.

func Compile

func Compile[T Expression](
	expressions []T,
	env *cel.Env,
	envOpts ...cel.EnvOption,
) (set ProgramSet, err error)

Compile produces a ProgramSet from the provided expressions in the given environment. If the generated cel.Program require cel.ProgramOption params, use CompileASTs instead with a subsequent call to ASTSet.ToProgramSet.

func (ProgramSet) Eval

func (s ProgramSet) Eval(val any, failFast bool) error

Eval applies the contained expressions to the provided `this` val, returning either *errors.ValidationError if the input is invalid or errors.RuntimeError if there is a type or range error. If failFast is true, execution stops at the first failed expression.

type Variable

type Variable struct {
	// Next is the parent activation
	Next interpreter.Activation
	// Name is the variable's name
	Name string
	// Val is the value for this variable
	Val any
}

Variable implements interpreter.Activation, providing a lightweight named variable to cel.Program executions.

func (*Variable) Parent

func (v *Variable) Parent() interpreter.Activation

func (*Variable) ResolveName

func (v *Variable) ResolveName(name string) (any, bool)

type VariablePool

type VariablePool sync.Pool

func (*VariablePool) Get

func (p *VariablePool) Get() *Variable

func (*VariablePool) Put

func (p *VariablePool) Put(v *Variable)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL