Movatterモバイル変換


[0]ホーム

URL:


cel

package
v0.26.1Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License:Apache-2.0, BSD-3-ClauseImports:37Imported by:704

Details

Repository

github.com/google/cel-go

Links

Documentation

Overview

Package cel defines the top-level interface for the Common Expression Language (CEL).

CEL is a non-Turing complete expression language designed to parse, check, and evaluateexpressions against user-defined environments.

Example
package mainimport ("fmt""log""github.com/google/cel-go/cel""github.com/google/cel-go/common/types""github.com/google/cel-go/common/types/ref")func main() {// Create the CEL environment with declarations for the input attributes and the extension functions.// In many cases the desired functionality will be present in a built-in function.e, err := cel.NewEnv(// Variable identifiers used within this expression.cel.Variable("i", cel.StringType),cel.Variable("you", cel.StringType),// Function to generate a greeting from one person to another: i.greet(you)cel.Function("greet",cel.MemberOverload("string_greet_string", []*cel.Type{cel.StringType, cel.StringType}, cel.StringType,cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {return types.String(fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs))}),),),)if err != nil {log.Fatalf("environment creation error: %s\n", err)}// Compile the expression.ast, iss := e.Compile("i.greet(you)")if iss.Err() != nil {log.Fatalln(iss.Err())}// Create the program.prg, err := e.Program(ast)if err != nil {log.Fatalf("program creation error: %s\n", err)}// Evaluate the program against some inputs. Note: the details return is not used.out, _, err := prg.Eval(map[string]any{// Native values are converted to CEL values under the covers."i": "CEL",// Values may also be lazily supplied."you": func() ref.Val { return types.String("world") },})if err != nil {log.Fatalf("runtime error: %s\n", err)}fmt.Println(out)}
Output:Hello world! Nice to meet you, I'm CEL.

Example (GlobalOverload)
package mainimport ("fmt""log""github.com/google/cel-go/cel""github.com/google/cel-go/common/types""github.com/google/cel-go/common/types/ref")func main() {// The GlobalOverload example demonstrates how to define global overload function.// Create the CEL environment with declarations for the input attributes and// the desired extension functions. In many cases the desired functionality will// be present in a built-in function.e, err := cel.NewEnv(// Identifiers used within this expression.cel.Variable("i", cel.StringType),cel.Variable("you", cel.StringType),// Function to generate shake_hands between two people.//    shake_hands(i,you)cel.Function("shake_hands",cel.Overload("shake_hands_string_string", []*cel.Type{cel.StringType, cel.StringType}, cel.StringType,cel.BinaryBinding(func(arg1, arg2 ref.Val) ref.Val {return types.String(fmt.Sprintf("%v and %v are shaking hands.\n", arg1, arg2))}),),),)if err != nil {log.Fatalf("environment creation error: %s\n", err)}// Compile the expression.ast, iss := e.Compile(`shake_hands(i,you)`)if iss.Err() != nil {log.Fatalln(iss.Err())}// Create the program.prg, err := e.Program(ast)if err != nil {log.Fatalf("program creation error: %s\n", err)}// Evaluate the program against some inputs. Note: the details return is not used.out, _, err := prg.Eval(map[string]any{"i":   "CEL","you": func() ref.Val { return types.String("world") },})if err != nil {log.Fatalf("runtime error: %s\n", err)}fmt.Println(out)}
Output:CEL and world are shaking hands.

Example (StatefulOverload)
package mainimport ("context""fmt""log""github.com/google/cel-go/cel""github.com/google/cel-go/common/types""github.com/google/cel-go/common/types/ref")func main() {// makeFetch produces a consistent function signature with a different function// implementation depending on the provided context.makeFetch := func(ctx any) cel.EnvOption {fn := func(arg ref.Val) ref.Val {return types.NewErr("stateful context not bound")}if ctx != nil {fn = func(resource ref.Val) ref.Val {return types.DefaultTypeAdapter.NativeToValue(ctx.(context.Context).Value(contextString(string(resource.(types.String)))),)}}return cel.Function("fetch",cel.Overload("fetch_string",[]*cel.Type{cel.StringType}, cel.StringType,cel.UnaryBinding(fn),),)}// The base environment declares the fetch function with a dummy binding that errors// if it is invoked without being replaced by a subsequent call to `baseEnv.Extend`baseEnv, err := cel.NewEnv(// Identifiers used within this expression.cel.Variable("resource", cel.StringType),// Function to fetch a resource.//    fetch(resource)makeFetch(nil),)if err != nil {log.Fatalf("environment creation error: %s\n", err)}ast, iss := baseEnv.Compile("fetch('my-resource') == 'my-value'")if iss.Err() != nil {log.Fatalf("Compile() failed: %v", iss.Err())}// The runtime environment extends the base environment with a contextual binding for// the 'fetch' function.ctx := context.WithValue(context.TODO(), contextString("my-resource"), "my-value")runtimeEnv, err := baseEnv.Extend(makeFetch(ctx))if err != nil {log.Fatalf("baseEnv.Extend() failed with error: %s\n", err)}prg, err := runtimeEnv.Program(ast)if err != nil {log.Fatalf("runtimeEnv.Program() error: %s\n", err)}out, _, err := prg.Eval(cel.NoVars())if err != nil {log.Fatalf("runtime error: %s\n", err)}fmt.Println(out)}type contextString string
Output:true

Index

Examples

Constants

View Source
const (// DynKind represents a dynamic type. This kind only exists at type-check time.DynKindKind =types.DynKind// AnyKind represents a google.protobuf.Any type. This kind only exists at type-check time.AnyKind =types.AnyKind// BoolKind represents a boolean type.BoolKind =types.BoolKind// BytesKind represents a bytes type.BytesKind =types.BytesKind// DoubleKind represents a double type.DoubleKind =types.DoubleKind// DurationKind represents a CEL duration type.DurationKind =types.DurationKind// IntKind represents an integer type.IntKind =types.IntKind// ListKind represents a list type.ListKind =types.ListKind// MapKind represents a map type.MapKind =types.MapKind// NullTypeKind represents a null type.NullTypeKind =types.NullTypeKind// OpaqueKind represents an abstract type which has no accessible fields.OpaqueKind =types.OpaqueKind// StringKind represents a string type.StringKind =types.StringKind// StructKind represents a structured object with typed fields.StructKind =types.StructKind// TimestampKind represents a a CEL time type.TimestampKind =types.TimestampKind// TypeKind represents the CEL type.TypeKind =types.TypeKind// TypeParamKind represents a parameterized type whose type name will be resolved at type-check time, if possible.TypeParamKind =types.TypeParamKind// UintKind represents a uint type.UintKind =types.UintKind)
View Source
const (// HomogeneousAggregateLiteralExemptFunctions is the ValidatorConfig key used to configure// the set of function names which are exempt from homogeneous type checks. The expected type// is a string list of function names.//// As an example, the `<string>.format([args])` call expects the input arguments list to be// comprised of a variety of types which correspond to the types expected by the format control// clauses; however, all other uses of a mixed element type list, would be unexpected.HomogeneousAggregateLiteralExemptFunctions = homogeneousValidatorName + ".exempt")

Variables

View Source
var (// AnyType represents the google.protobuf.Any type.AnyType =types.AnyType// BoolType represents the bool type.BoolType =types.BoolType// BytesType represents the bytes type.BytesType =types.BytesType// DoubleType represents the double type.DoubleType =types.DoubleType// DurationType represents the CEL duration type.DurationType =types.DurationType// DynType represents a dynamic CEL type whose type will be determined at runtime from context.DynType =types.DynType// IntType represents the int type.IntType =types.IntType// NullType represents the type of a null value.NullType =types.NullType// StringType represents the string type.StringType =types.StringType// TimestampType represents the time type.TimestampType =types.TimestampType// TypeType represents a CEL typeTypeType =types.TypeType// UintType represents a uint type.UintType =types.UintType// ListType creates an instances of a list type value with the provided element type.ListType =types.NewListType// MapType creates an instance of a map type value with the provided key and value types.MapType =types.NewMapType// NullableType creates an instance of a nullable type with the provided wrapped type.//// Note: only primitive types are supported as wrapped types.NullableType =types.NewNullableType// OptionalType creates an abstract parameterized type instance corresponding to CEL's notion of optional.OptionalType =types.NewOptionalType// OpaqueType creates an abstract parameterized type with a given name.OpaqueType =types.NewOpaqueType// ObjectType creates a type references to an externally defined type, e.g. a protobuf message type.ObjectType =types.NewObjectType// TypeParamType creates a parameterized type instance.TypeParamType =types.NewTypeParamType)
View Source
var (// HasMacro expands "has(m.f)" which tests the presence of a field, avoiding the need to// specify the field as a string.HasMacro =parser.HasMacro// AllMacro expands "range.all(var, predicate)" into a comprehension which ensures that all// elements in the range satisfy the predicate.AllMacro =parser.AllMacro// ExistsMacro expands "range.exists(var, predicate)" into a comprehension which ensures that// some element in the range satisfies the predicate.ExistsMacro =parser.ExistsMacro// ExistsOneMacro expands "range.exists_one(var, predicate)", which is true if for exactly one// element in range the predicate holds.ExistsOneMacro =parser.ExistsOneMacro// MapMacro expands "range.map(var, function)" into a comprehension which applies the function// to each element in the range to produce a new list.MapMacro =parser.MapMacro// MapFilterMacro expands "range.map(var, predicate, function)" into a comprehension which// first filters the elements in the range by the predicate, then applies the transform function// to produce a new list.MapFilterMacro =parser.MapFilterMacro// FilterMacro expands "range.filter(var, predicate)" into a comprehension which filters// elements in the range, producing a new list from the elements that satisfy the predicate.FilterMacro =parser.FilterMacro// StandardMacros provides an alias to all the CEL macros defined in the standard environment.StandardMacros = []Macro{HasMacro,AllMacro,ExistsMacro,ExistsOneMacro,MapMacro,MapFilterMacro,FilterMacro,}// NoMacros provides an alias to an empty list of macrosNoMacros = []Macro{})

Functions

funcAlphaProtoAsValueadded inv0.22.0

func AlphaProtoAsValue(adaptertypes.Adapter, v *exprpb.Value) (ref.Val,error)

AlphaProtoAsValue converts between google.api.expr.v1alpha1.Value and ref.Val.

funcAstToCheckedExpr

func AstToCheckedExpr(a *Ast) (*exprpb.CheckedExpr,error)

AstToCheckedExpr converts an Ast to an protobuf CheckedExpr value.

If the Ast.IsChecked() returns false, this conversion method will return an error.

funcAstToParsedExpr

func AstToParsedExpr(a *Ast) (*exprpb.ParsedExpr,error)

AstToParsedExpr converts an Ast to an protobuf ParsedExpr value.

funcAstToStringadded inv0.3.0

func AstToString(a *Ast) (string,error)

AstToString converts an Ast back to a string if possible.

Note, the conversion may not be an exact replica of the original expression, but will producea string that is semantically equivalent and whose textual representation is stable.

funcExprToStringadded inv0.25.0

func ExprToString(east.Expr, info *ast.SourceInfo) (string,error)

ExprToString converts an AST Expr node back to a string using macro call tracking metadata fromsource info if any macros are encountered within the expression.

funcExprValueAsAlphaProtoadded inv0.25.1

func ExprValueAsAlphaProto(resref.Val) (*exprpb.ExprValue,error)

ExprValueAsAlphaProto converts between ref.Val and google.api.expr.v1alpha1.ExprValue.The result ExprValue is the serialized proto form.

funcExprValueAsProtoadded inv0.25.1

func ExprValueAsProto(resref.Val) (*celpb.ExprValue,error)

ExprValueAsProto converts between ref.Val and cel.expr.ExprValue.The result ExprValue is the serialized proto form.

funcFormatCELTypeadded inv0.17.0

func FormatCELType(t *Type)string

FormatCELType formats a cel.Type value to a string representation.

The type formatting is identical to FormatType.

funcFormatTypedeprecatedadded inv0.7.1

func FormatType(t *exprpb.Type)string

FormatType converts a type message into a string representation.

Deprecated: prefer FormatCELType

funcProtoAsValueadded inv0.22.0

func ProtoAsValue(adaptertypes.Adapter, v *celpb.Value) (ref.Val,error)

ProtoAsValue converts between cel.expr.Value and ref.Val.

funcRefValToExprValueadded inv0.25.1

func RefValToExprValue(resref.Val) (*exprpb.ExprValue,error)

RefValToExprValue converts between ref.Val and google.api.expr.v1alpha1.ExprValue.The result ExprValue is the serialized proto form.

funcRefValueToValueadded inv0.10.0

func RefValueToValue(resref.Val) (*exprpb.Value,error)

RefValueToValue converts between ref.Val and google.api.expr.v1alpha1.Value.The result Value is the serialized proto form. The ref.Val must not be error or unknown.

funcTypeToExprTypeadded inv0.12.0

func TypeToExprType(t *Type) (*exprpb.Type,error)

TypeToExprType converts a CEL-native type representation to a protobuf CEL Type representation.

funcValueAsAlphaProtoadded inv0.22.0

func ValueAsAlphaProto(resref.Val) (*exprpb.Value,error)

ValueAsAlphaProto converts between ref.Val and google.api.expr.v1alpha1.Value.The result Value is the serialized proto form. The ref.Val must not be error or unknown.

funcValueAsProtoadded inv0.22.0

func ValueAsProto(resref.Val) (*celpb.Value,error)

ValueAsProto converts between ref.Val and cel.expr.Value.The result Value is the serialized proto form. The ref.Val must not be error or unknown.

funcValueToRefValueadded inv0.10.0

func ValueToRefValue(adaptertypes.Adapter, v *exprpb.Value) (ref.Val,error)

ValueToRefValue converts between google.api.expr.v1alpha1.Value and ref.Val.

Types

typeASTOptimizeradded inv0.18.0

type ASTOptimizer interface {// Optimize optimizes a type-checked AST within an Environment and accumulates any issues.Optimize(*OptimizerContext, *ast.AST) *ast.AST}

ASTOptimizer applies an optimization over an AST and returns the optimized result.

funcNewConstantFoldingOptimizeradded inv0.18.0

func NewConstantFoldingOptimizer(opts ...ConstantFoldingOption) (ASTOptimizer,error)

NewConstantFoldingOptimizer creates an optimizer which inlines constant scalar an aggregateliteral values within function calls and select statements with their evaluated result.

funcNewInliningOptimizeradded inv0.18.0

func NewInliningOptimizer(inlineVars ...*InlineVariable)ASTOptimizer

NewInliningOptimizer creates and optimizer which replaces variables with expression definitions.

If a variable occurs one time, the variable is replaced by the inline definition. If thevariable occurs more than once, the variable occurences are replaced by a cel.bind() call.

typeASTValidatoradded inv0.17.0

type ASTValidator interface {// Name returns the name of the validator. Names must be unique.Name()string// Validate validates a given Ast within an Environment and collects a set of potential issues.//// The ValidatorConfig is generated from the set of ASTValidatorConfigurer instances prior to// the invocation of the Validate call. The expectation is that the validator configuration// is created in sequence and immutable once provided to the Validate call.//// See individual validators for more information on their configuration keys and configuration// properties.Validate(*Env,ValidatorConfig, *ast.AST, *Issues)}

ASTValidator defines a singleton interface for validating a type-checked Ast against an environment.

Note: the Issues argument is mutable in the sense that it is intended to collect errors which will bereported to the caller.

funcValidateComprehensionNestingLimitadded inv0.17.0

func ValidateComprehensionNestingLimit(limitint)ASTValidator

ValidateComprehensionNestingLimit ensures that comprehension nesting does not exceed the specified limit.

This validator can be useful for preventing arbitrarily nested comprehensions which can take high polynomialtime to complete.

Note, this limit does not apply to comprehensions with an empty iteration range, as these comprehensions haveno actual looping cost. The cel.bind() utilizes the comprehension structure to perform local variableassignments and supplies an empty iteration range, so they won't count against the nesting limit either.

funcValidateDurationLiteralsadded inv0.17.0

func ValidateDurationLiterals()ASTValidator

ValidateDurationLiterals ensures that duration literal arguments are valid immediately after type-check.

funcValidateHomogeneousAggregateLiteralsadded inv0.17.0

func ValidateHomogeneousAggregateLiterals()ASTValidator

ValidateHomogeneousAggregateLiterals checks that all list and map literals entries have the same types, i.e.no mixed list element types or mixed map key or map value types.

Note: the string format call relies on a mixed element type list for ease of use, so this check skips allliterals which occur within string format calls.

funcValidateRegexLiteralsadded inv0.17.0

func ValidateRegexLiterals()ASTValidator

ValidateRegexLiterals ensures that regex patterns are validated after type-check.

funcValidateTimestampLiteralsadded inv0.17.0

func ValidateTimestampLiterals()ASTValidator

ValidateTimestampLiterals ensures that timestamp literal arguments are valid immediately after type-check.

typeASTValidatorConfigureradded inv0.17.0

type ASTValidatorConfigurer interface {Configure(MutableValidatorConfig)error}

ASTValidatorConfigurer indicates that this object, currently expected to be an ASTValidator,participates in validator configuration settings.

This interface may be split from the expectation of being an ASTValidator instance in the future.

typeASTValidatorFactoryadded inv0.24.0

type ASTValidatorFactory func(*env.Validator) (ASTValidator,error)

ASTValidatorFactory creates an ASTValidator as configured by the input map

typeActivationadded inv0.24.0

type Activation =interpreter.Activation

Activation used to resolve identifiers by name and references by id.

An Activation is the primary mechanism by which a caller supplies input into a CEL program.

funcContextProtoVarsadded inv0.17.0

func ContextProtoVars(ctxproto.Message) (Activation,error)

ContextProtoVars uses the fields of the input proto.Messages as top-level variables within an Activation.

Consider using with `DeclareContextProto` to simplify variable type declarations and publishing when usingprotocol buffers.

funcNewActivationadded inv0.24.0

func NewActivation(bindingsany) (Activation,error)

NewActivation returns an activation based on a map-based binding where the map keys areexpected to be qualified names used with ResolveName calls.

The input `bindings` may either be of type `Activation` or `map[string]any`.

Lazy bindings may be supplied within the map-based input in either of the following forms:- func() any- func() ref.Val

The output of the lazy binding will overwrite the variable reference in the internal map.

Values which are not represented as ref.Val types on input may be adapted to a ref.Val usingthe types.Adapter configured in the environment.

funcNoVars

func NoVars()Activation

NoVars returns an empty Activation.

typeAst

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

Ast representing the checked or unchecked expression, its source, and related metadata such assource position information.

funcCheckedExprToAst

func CheckedExprToAst(checkedExpr *exprpb.CheckedExpr) *Ast

CheckedExprToAst converts a checked expression proto message to an Ast.

funcCheckedExprToAstWithSourceadded inv0.9.0

func CheckedExprToAstWithSource(checkedExpr *exprpb.CheckedExpr, srcSource) (*Ast,error)

CheckedExprToAstWithSource converts a checked expression proto message to an Ast,using the provided Source as the textual contents.

In general the source is not necessary unless the AST has been modified between the`Parse` and `Check` calls as an `Ast` created from the `Parse` step will carry the sourcethrough future calls.

Prefer CheckedExprToAst if loading expressions from storage.

funcParsedExprToAst

func ParsedExprToAst(parsedExpr *exprpb.ParsedExpr) *Ast

ParsedExprToAst converts a parsed expression proto message to an Ast.

funcParsedExprToAstWithSourceadded inv0.9.0

func ParsedExprToAstWithSource(parsedExpr *exprpb.ParsedExpr, srcSource) *Ast

ParsedExprToAstWithSource converts a parsed expression proto message to an Ast,using the provided Source as the textual contents.

In general you only need this if you need to recheck a previously checkedexpression, or if you need to separately check a subset of an expression.

Prefer ParsedExprToAst if loading expressions from storage.

func (*Ast)Exprdeprecated

func (ast *Ast) Expr() *exprpb.Expr

Expr returns the proto serializable instance of the parsed/checked expression.

Deprecated: prefer cel.AstToCheckedExpr() or cel.AstToParsedExpr() and call GetExpr()the result instead.

func (*Ast)IsChecked

func (ast *Ast) IsChecked()bool

IsChecked returns whether the Ast value has been successfully type-checked.

func (*Ast)NativeRepadded inv0.18.2

func (ast *Ast) NativeRep() *celast.AST

NativeRep converts the AST to a Go-native representation.

func (*Ast)OutputTypeadded inv0.12.0

func (ast *Ast) OutputType() *Type

OutputType returns the output type of the expression if the Ast has been type-checked, elsereturns cel.DynType as the parse step cannot infer types.

func (*Ast)ResultTypedeprecated

func (ast *Ast) ResultType() *exprpb.Type

ResultType returns the output type of the expression if the Ast has been type-checked, elsereturns chkdecls.Dyn as the parse step cannot infer the type.

Deprecated: use OutputType

func (*Ast)Source

func (ast *Ast) Source()Source

Source returns a view of the input used to create the Ast. This source may be complete orconstructed from the SourceInfo.

func (*Ast)SourceInfo

func (ast *Ast) SourceInfo() *exprpb.SourceInfo

SourceInfo returns character offset and newline position information about expression elements.

typeAttributePatternTypeadded inv0.25.0

type AttributePatternType =interpreter.AttributePattern

AttributePatternType represents a top-level variable with an optional set of qualifier patterns.

See the interpreter.AttributePattern and interpreter.AttributeQualifierPattern for more infoabout how to create and manipulate AttributePattern values.

funcAttributePatternadded inv0.4.0

func AttributePattern(varNamestring) *AttributePatternType

AttributePattern returns an AttributePattern that matches a top-level variable. The pattern ismutable, and its methods support the specification of one or more qualifier patterns.

For example, the AttributePattern(`a`).QualString(`b`) represents a variable access `a` with astring field or index qualification `b`. This pattern will match Attributes `a`, and `a.b`,but not `a.c`.

When using a CEL expression within a container, e.g. a package or namespace, the variable namein the pattern must match the qualified name produced during the variable namespace resolution.For example, when variable `a` is declared within an expression whose container is `ns.app`, thefully qualified variable name may be `ns.app.a`, `ns.a`, or `a` per the CEL namespace resolutionrules. Pick the fully qualified variable name that makes sense within the container as theAttributePattern `varName` argument.

typeConfigOptionFactoryadded inv0.24.0

type ConfigOptionFactory func(any) (EnvOption,bool)

ConfigOptionFactory declares a signature which accepts a configuration element, e.g. env.Extensionand optionally produces an EnvOption in response.

If there are multiple ConfigOptionFactory values which could apply to the same configuration nodethe first one that returns an EnvOption and a `true` response will be used, and the config nodewill not be passed along to any other option factory.

Only the *env.Extension type is provided at this time, but validators, optimizers, and other tuningparameters may be supported in the future.

typeConfigurableASTValidatoradded inv0.24.0

type ConfigurableASTValidator interface {// ToConfig converts the internal configuration of an ASTValidator into an env.Validator instance// which minimally must include the validator name, but may also include a map[string]any config// object to be serialized to YAML. The string keys represent the configuration parameter name,// and the any value must mirror the internally supported type associated with the config key.//// Note: only primitive CEL types are supported by CEL validators at this time.ToConfig() *env.Validator}

ConfigurableASTValidator supports conversion of an object to an `env.Validator` instance used forYAML serialization.

typeConstantFoldingOptionadded inv0.18.0

type ConstantFoldingOption func(opt *constantFoldingOptimizer) (*constantFoldingOptimizer,error)

ConstantFoldingOption defines a functional option for configuring constant folding.

funcFoldKnownValuesadded inv0.25.1

func FoldKnownValues(knownValuesActivation)ConstantFoldingOption

FoldKnownValues adds an Activation which provides known values for the folding evaluator

Any values the activation provides will be used by the constant folder and turned intoliterals in the AST.

Defaults to the NoVars() Activation

funcMaxConstantFoldIterationsadded inv0.18.0

func MaxConstantFoldIterations(limitint)ConstantFoldingOption

MaxConstantFoldIterations limits the number of times literals may be folding during optimization.

Defaults to 100 if not set.

typeEnv

type Env struct {Container *containers.Container// contains filtered or unexported fields}

Env encapsulates the context necessary to perform parsing, type checking, or generation ofevaluable programs for different expressions.

funcNewCustomEnvadded inv0.4.0

func NewCustomEnv(opts ...EnvOption) (*Env,error)

NewCustomEnv creates a custom program environment which is not automatically configured with thestandard library of functions and macros documented in the CEL spec.

The purpose for using a custom environment might be for subsetting the standard library producedby the cel.StdLib() function. Subsetting CEL is a core aspect of its design that allows users tolimit the compute and memory impact of a CEL program by controlling the functions and macrosthat may appear in a given expression.

See the EnvOption helper functions for the options that can be used to configure theenvironment.

funcNewEnv

func NewEnv(opts ...EnvOption) (*Env,error)

NewEnv creates a program environment configured with the standard library of CEL functions andmacros. The Env value returned can parse and check any CEL program which builds upon the corefeatures documented in the CEL specification.

See the EnvOption helper functions for the options that can be used to configure theenvironment.

func (*Env)CELTypeAdapteradded inv0.17.0

func (e *Env) CELTypeAdapter()types.Adapter

CELTypeAdapter returns the `types.Adapter` configured for the environment.

func (*Env)CELTypeProvideradded inv0.17.0

func (e *Env) CELTypeProvider()types.Provider

CELTypeProvider returns the `types.Provider` configured for the environment.

func (*Env)Check

func (e *Env) Check(ast *Ast) (*Ast, *Issues)

Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues.If any `ASTValidators` are configured on the environment, they will be applied after a validtype-check result. If any issues are detected, the validators will provide them on theoutput Issues object.

Either checking or validation has failed if the returned Issues value and its Issues.Err()value are non-nil. Issues should be inspected if they are non-nil, but may not represent afatal error.

It is possible to have both non-nil Ast and Issues values returned from this call: however,the mere presence of an Ast does not imply that it is valid for use.

func (*Env)Compileadded inv0.4.0

func (e *Env) Compile(txtstring) (*Ast, *Issues)

Compile combines the Parse and Check phases CEL program compilation to produce an Ast andassociated issues.

If an error is encountered during parsing the Compile step will not continue with the Checkphase. If non-error issues are encountered during Parse, they may be combined with any issuesdiscovered during Check.

Note, for parse-only uses of CEL use Parse.

func (*Env)CompileSourceadded inv0.4.0

func (e *Env) CompileSource(srcSource) (*Ast, *Issues)

CompileSource combines the Parse and Check phases CEL program compilation to produce an Ast andassociated issues.

If an error is encountered during parsing the CompileSource step will not continue with theCheck phase. If non-error issues are encountered during Parse, they may be combined with anyissues discovered during Check.

Note, for parse-only uses of CEL use Parse.

func (*Env)EstimateCostadded inv0.10.0

func (e *Env) EstimateCost(ast *Ast, estimatorchecker.CostEstimator, opts ...checker.CostOption) (checker.CostEstimate,error)

EstimateCost estimates the cost of a type checked CEL expression using the length estimates of input data andextension functions provided by estimator.

func (*Env)Extendadded inv0.3.2

func (e *Env) Extend(opts ...EnvOption) (*Env,error)

Extend the current environment with additional options to produce a new Env.

Note, the extended Env value should not share memory with the original. It is possible, however,that a CustomTypeAdapter or CustomTypeProvider options could provide values which are mutable.To ensure separation of state between extended environments either make sure the TypeAdapter andTypeProvider are immutable, or that their underlying implementations are based on theref.TypeRegistry which provides a Copy method which will be invoked by this method.

func (*Env)Functionsadded inv0.21.0

func (e *Env) Functions() map[string]*decls.FunctionDecl

Functions returns a shallow copy of the Functions, keyed by function name, that have been configured in the environment.

func (*Env)HasFeatureadded inv0.5.1

func (e *Env) HasFeature(flagint)bool

HasFeature checks whether the environment enables the given featureflag, as enumerated in options.go.

func (*Env)HasFunctionadded inv0.21.0

func (e *Env) HasFunction(functionNamestring)bool

HasFunction returns whether a specific function has been configured in the environment

func (*Env)HasLibraryadded inv0.13.0

func (e *Env) HasLibrary(libNamestring)bool

HasLibrary returns whether a specific SingletonLibrary has been configured in the environment.

func (*Env)HasValidatoradded inv0.17.0

func (e *Env) HasValidator(namestring)bool

HasValidator returns whether a specific ASTValidator has been configured in the environment.

func (*Env)Librariesadded inv0.17.5

func (e *Env) Libraries() []string

Libraries returns a list of SingletonLibrary that have been configured in the environment.

func (*Env)Macrosadded inv0.25.0

func (e *Env) Macros() []Macro

Macros returns a shallow copy of macros associated with the environment.

func (*Env)Parse

func (e *Env) Parse(txtstring) (*Ast, *Issues)

Parse parses the input expression value `txt` to a Ast and/or a set of Issues.

This form of Parse creates a Source value for the input `txt` and forwards to theParseSource method.

func (*Env)ParseSourceadded inv0.4.0

func (e *Env) ParseSource(srcSource) (*Ast, *Issues)

ParseSource parses the input source to an Ast and/or set of Issues.

Parsing has failed if the returned Issues value and its Issues.Err() value is non-nil.Issues should be inspected if they are non-nil, but may not represent a fatal error.

It is possible to have both non-nil Ast and Issues values returned from this call; however,the mere presence of an Ast does not imply that it is valid for use.

func (*Env)PartialVarsadded inv0.17.0

func (e *Env) PartialVars(varsany) (PartialActivation,error)

PartialVars returns a PartialActivation where all variables not in the input variableset, but which have been configured in the environment, are marked as unknown.

The `vars` value may either be an Activation or any valid input to the cel.NewActivation call.

Note, this is equivalent to calling cel.PartialVars and manually configuring the set of unknownvariables. For more advanced use cases of partial state where portions of an object graph, ratherthan top-level variables, are missing the PartialVars() method may be a more suitable choice.

Note, the PartialVars will behave the same as cel.NoVars() unless the PartialAttributesoption is provided as a ProgramOption.

func (*Env)PlanProgramadded inv0.22.0

func (e *Env) PlanProgram(a *celast.AST, opts ...ProgramOption) (Program,error)

PlanProgram generates an evaluable instance of the AST in the go-native representation withinthe environment (Env).

func (*Env)Program

func (e *Env) Program(ast *Ast, opts ...ProgramOption) (Program,error)

Program generates an evaluable instance of the Ast within the environment (Env).

func (*Env)ResidualAstadded inv0.4.0

func (e *Env) ResidualAst(a *Ast, details *EvalDetails) (*Ast,error)

ResidualAst takes an Ast and its EvalDetails to produce a new Ast which only contains theattribute references which are unknown.

Residual expressions are beneficial in a few scenarios:

- Optimizing constant expression evaluations away.- Indexing and pruning expressions based on known input arguments.- Surfacing additional requirements that are needed in order to complete an evaluation.- Sharing the evaluation of an expression across multiple machines/nodes.

For example, if an expression targets a 'resource' and 'request' attribute and the possiblevalues for the resource are known, a PartialActivation could mark the 'request' as an unknowninterpreter.AttributePattern and the resulting ResidualAst would be reduced to only the partsof the expression that reference the 'request'.

Note, the expression ids within the residual AST generated through this method have nocorrelation to the expression ids of the original AST.

See the PartialVars helper for how to construct a PartialActivation.

TODO: Consider adding an option to generate a Program.Residual to avoid round-tripping to anAst format and then Program again.

func (*Env)ToConfigadded inv0.24.0

func (e *Env) ToConfig(namestring) (*env.Config,error)

ToConfig produces a YAML-serializable env.Config object from the given environment.

The serialized configuration value is intended to represent a baseline set of configoptions which could be used as input to an EnvOption to configure the majority of theenvironment from a file.

Note: validators, features, flags, and safe-guard settings are not yet supported bythe serialize method. Since optimizers are a separate construct from the environmentand the standard expression components (parse, check, evalute), they are also notsupported by the serialize method.

func (*Env)TypeAdapterdeprecatedadded inv0.2.0

func (e *Env) TypeAdapter()ref.TypeAdapter

TypeAdapter returns the `ref.TypeAdapter` configured for the environment.

Deprecated: use CELTypeAdapter()

func (*Env)TypeProviderdeprecatedadded inv0.2.0

func (e *Env) TypeProvider()ref.TypeProvider

TypeProvider returns the `ref.TypeProvider` configured for the environment.

Deprecated: use CELTypeProvider()

func (*Env)UnknownVarsadded inv0.4.0

func (e *Env) UnknownVars()PartialActivation

UnknownVars returns a PartialActivation which marks all variables declared in the Env asunknown AttributePattern values.

Note, the UnknownVars will behave the same as an cel.NoVars() unless the PartialAttributesoption is provided as a ProgramOption.

func (*Env)Validatorsadded inv0.24.0

func (e *Env) Validators() []ASTValidator

Validators returns the set of ASTValidators configured on the environment.

func (*Env)Variablesadded inv0.24.0

func (e *Env) Variables() []*decls.VariableDecl

Variables returns a shallow copy of the variables associated with the environment.

typeEnvOption

type EnvOption func(e *Env) (*Env,error)

EnvOption is a functional interface for configuring the environment.

funcASTValidatorsadded inv0.17.0

func ASTValidators(validators ...ASTValidator)EnvOption

ASTValidators configures a set of ASTValidator instances into the target environment.

Validators are applied in the order in which the are specified and are treated as singletons.The same ASTValidator with a given name will not be applied more than once.

funcAbbrevsadded inv0.6.0

func Abbrevs(qualifiedNames ...string)EnvOption

Abbrevs configures a set of simple names as abbreviations for fully-qualified names.

An abbreviation (abbrev for short) is a simple name that expands to a fully-qualified name.Abbreviations can be useful when working with variables, functions, and especially types frommultiple namespaces:

// CEL object constructionqual.pkg.version.ObjTypeName{   field: alt.container.ver.FieldTypeName{value: ...}}

Only one the qualified names above may be used as the CEL container, so at least one of thesereferences must be a long qualified name within an otherwise short CEL program. Using thefollowing abbreviations, the program becomes much simpler:

// CEL Go optionAbbrevs("qual.pkg.version.ObjTypeName", "alt.container.ver.FieldTypeName")// Simplified Object constructionObjTypeName{field: FieldTypeName{value: ...}}

There are a few rules for the qualified names and the simple abbreviations generated from them:- Qualified names must be dot-delimited, e.g. `package.subpkg.name`.- The last element in the qualified name is the abbreviation.- Abbreviations must not collide with each other.- The abbreviation must not collide with unqualified names in use.

Abbreviations are distinct from container-based references in the following important ways:- Abbreviations must expand to a fully-qualified name.- Expanded abbreviations do not participate in namespace resolution.- Abbreviation expansion is done instead of the container search for a matching identifier.- Containers follow C++ namespace resolution rules with searches from the most qualified name

to the least qualified name.

- Container references within the CEL program may be relative, and are resolved to fully

qualified names at either type-check time or program plan time, whichever comes first.

If there is ever a case where an identifier could be in both the container and as anabbreviation, the abbreviation wins as this will ensure that the meaning of a program ispreserved between compilations even as the container evolves.

funcAlphaProtoAsDeclarationadded inv0.22.0

func AlphaProtoAsDeclaration(d *exprpb.Decl) (EnvOption,error)

AlphaProtoAsDeclaration converts a v1alpha1.Decl value describing a variable or function into an EnvOption.

funcClearMacros

func ClearMacros()EnvOption

ClearMacros options clears all parser macros.

Clearing macros will ensure CEL expressions can only contain linear evaluation paths, ascomprehensions such as `all` and `exists` are enabled only via macros.

funcConstantadded inv0.17.0

func Constant(namestring, t *Type, vref.Val)EnvOption

Constant creates an instances of an identifier declaration with a variable name, type, and value.

funcContainer

func Container(namestring)EnvOption

Container sets the container for resolving variable names. Defaults to an empty container.

If all references within an expression are relative to a protocol buffer package, thenspecifying a container of `google.type` would make it possible to write expressions such as`Expr{expression: 'a < b'}` instead of having to write `google.type.Expr{...}`.

funcCostEstimatorOptionsadded inv0.17.7

func CostEstimatorOptions(costOpts ...checker.CostOption)EnvOption

CostEstimatorOptions configure type-check time options for estimating expression cost.

funcCrossTypeNumericComparisonsadded inv0.10.0

func CrossTypeNumericComparisons(enabledbool)EnvOption

CrossTypeNumericComparisons makes it possible to compare across numeric types, e.g. double < int

funcCustomTypeAdapteradded inv0.2.0

func CustomTypeAdapter(adaptertypes.Adapter)EnvOption

CustomTypeAdapter swaps the default types.Adapter implementation with a custom one.

Note: This option must be specified before the Types and TypeDescs options when used together.

funcCustomTypeProvider

func CustomTypeProvider(providerany)EnvOption

CustomTypeProvider replaces the types.Provider implementation with a custom one.

The `provider` variable type may either be types.Provider or ref.TypeProvider (deprecated)

Note: This option must be specified before the Types and TypeDescs options when used together.

funcDeclarationsdeprecated

func Declarations(decls ...*exprpb.Decl)EnvOption

Declarations option extends the declaration set configured in the environment.

Note: Declarations will by default be appended to the pre-existing declaration set configuredfor the environment. The NewEnv call builds on top of the standard CEL declarations. For apurely custom set of declarations use NewCustomEnv.

Deprecated: use FunctionDecls and VariableDecls or FromConfig instead.

funcDeclareContextProtoadded inv0.8.0

func DeclareContextProto(descriptorprotoreflect.MessageDescriptor)EnvOption

DeclareContextProto returns an option to extend CEL environment with declarations from the given context proto.Each field of the proto defines a variable of the same name in the environment.https://github.com/google/cel-spec/blob/master/doc/langdef.md#evaluation-environment

funcDefaultUTCTimeZoneadded inv0.12.0

func DefaultUTCTimeZone(enabledbool)EnvOption

DefaultUTCTimeZone ensures that time-based operations use the UTC timezone rather than theinput time's local timezone.

funcEagerlyValidateDeclarationsadded inv0.11.1

func EagerlyValidateDeclarations(enabledbool)EnvOption

EagerlyValidateDeclarations ensures that any collisions between configured declarations are caughtat the time of the `NewEnv` call.

Eagerly validating declarations is also useful for bootstrapping a base `cel.Env` value.Calls to base `Env.Extend()` will be significantly faster when declarations are eagerly validatedas declarations will be collision-checked at most once and only incrementally by way of `Extend`

Disabled by default as not all environments are used for type-checking.

funcEnableErrorOnBadPresenceTestadded inv0.21.0

func EnableErrorOnBadPresenceTest(valuebool)EnvOption

EnableErrorOnBadPresenceTest enables error generation when a presence test or optional fieldselection is performed on a primitive type.

funcEnableHiddenAccumulatorNameadded inv0.23.0

func EnableHiddenAccumulatorName(enabledbool)EnvOption

EnableHiddenAccumulatorName sets the parser to use the identifier '@result' for accumulatorswhich is not normally accessible from CEL source.

funcEnableIdentifierEscapeSyntaxadded inv0.23.0

func EnableIdentifierEscapeSyntax()EnvOption

EnableIdentifierEscapeSyntax enables identifier escaping (`) syntax forfields.

funcEnableMacroCallTrackingadded inv0.10.0

func EnableMacroCallTracking()EnvOption

EnableMacroCallTracking ensures that call expressions which are replaced by macrosare tracked in the `SourceInfo` of parsed and checked expressions.

funcExprDeclToDeclarationadded inv0.12.0

func ExprDeclToDeclaration(d *exprpb.Decl) (EnvOption,error)

ExprDeclToDeclaration converts a protobuf CEL declaration to a CEL-native declaration, either a Variable or Function.

funcExtendedValidationsadded inv0.17.0

func ExtendedValidations()EnvOption

ExtendedValidations collects a set of common AST validations which reduce the likelihood of runtime errors.

- Validate duration and timestamp literals- Ensure regex strings are valid- Disable mixed type list and map literals

funcFromConfigadded inv0.24.0

func FromConfig(config *env.Config, optFactories ...ConfigOptionFactory)EnvOption

FromConfig produces and applies a set of EnvOption values derived from an env.Config object.

For configuration elements which refer to features outside of the `cel` package, an optional set ofConfigOptionFactory values may be passed in to support the conversion from static configuration toconfigured cel.Env value.

Note: disabling the standard library will clear the EnvOptions values previously set for theenvironment with the exception of propagating types and adapters over to the new environment.

Note: to support custom types referenced in the configuration file, you must ensure that one ofthe following options appears before the FromConfig option: Types, TypeDescs, or CustomTypeProvideras the type provider configured at the time when the config is processed is the one used to derivetype references from the configuration.

funcFunctionadded inv0.12.0

func Function(namestring, opts ...FunctionOpt)EnvOption

Function defines a function and overloads with optional singleton or per-overload bindings.

Using Function is roughly equivalent to calling Declarations() to declare the function signaturesand Functions() to define the function bindings, if they have been defined. Specifying thesame function name more than once will result in the aggregation of the function overloads. If anysignatures conflict between the existing and new function definition an error will be raised.However, if the signatures are identical and the overload ids are the same, the redefinition willbe considered a no-op.

One key difference with using Function() is that each FunctionDecl provided will handle dynamicdispatch based on the type-signatures of the overloads provided which means overload resolution atruntime is handled out of the box rather than via a custom binding for overload resolution viaFunctions():

- Overloads are searched in the order they are declared- Dynamic dispatch for lists and maps is limited by inspection of the list and map contents

at runtime. Empty lists and maps will result in a 'default dispatch'

- In the event that a default dispatch occurs, the first overload provided is the one invoked

If you intend to use overloads which differentiate based on the key or element type of a list ormap, consider using a generic function instead: e.g. func(list(T)) or func(map(K, V)) as thiswill allow your implementation to determine how best to handle dispatch and the default behaviorfor empty lists and maps whose contents cannot be inspected.

For functions which use parameterized opaque types (abstract types), consider using a singletonfunction which is capable of inspecting the contents of the type and resolving the appropriateoverload as CEL can only make inferences by type-name regarding such types.

funcFunctionDeclsadded inv0.24.0

func FunctionDecls(funcs ...*decls.FunctionDecl)EnvOption

FunctionDecls provides one or more fully formed function declarations to be added to the environment.

funcHomogeneousAggregateLiteralsadded inv0.2.0

func HomogeneousAggregateLiterals()EnvOption

HomogeneousAggregateLiterals disables mixed type list and map literal values.

Note, it is still possible to have heterogeneous aggregates when provided as variables to theexpression, as well as via conversion of well-known dynamic types, or with uncheckedexpressions.

funcLibadded inv0.4.0

func Lib(lLibrary)EnvOption

Lib creates an EnvOption out of a Library, allowing libraries to be provided as functional args,and to be linked to each other.

funcMacros

func Macros(macros ...Macro)EnvOption

Macros option extends the macro set configured in the environment.

Note: This option must be specified after ClearMacros if used together.

funcOptionalTypesadded inv0.13.0

func OptionalTypes(opts ...OptionalTypesOption)EnvOption

OptionalTypes enable support for optional syntax and types in CEL.

The optional value type makes it possible to express whether variables havebeen provided, whether a result has been computed, and in the future whetheran object field path, map key value, or list index has a value.

Syntax Changes

OptionalTypes are unlike other CEL extensions because they modify the CELsyntax itself, notably through the use of a `?` preceding a field name orindex value.

## Field Selection

The optional syntax in field selection is denoted as `obj.?field`. In otherwords, if a field is set, return `optional.of(obj.field)“, else`optional.none()`. The optional field selection is viral in the sense thatafter the first optional selection all subsequent selections or indicesare treated as optional, i.e. the following expressions are equivalent:

obj.?field.subfieldobj.?field.?subfield

## Indexing

Similar to field selection, the optional syntax can be used in indexexpressions on maps and lists:

list[?0]map[?key]

## Optional Field Setting

When creating map or message literals, if a field may be optionally setbased on its presence, then placing a `?` before the field name or keywill ensure the type on the right-hand side must be optional(T) where Tis the type of the field or key-value.

The following returns a map with the key expression set only if thesubfield is present, otherwise an empty map is created:

{?key: obj.?field.subfield}

## Optional Element Setting

When creating list literals, an element in the list may be optionally addedwhen the element expression is preceded by a `?`:

[a, ?b, ?c] // return a list with either [a], [a, b], [a, b, c], or [a, c]

Optional.Of

Create an optional(T) value of a given value with type T.

optional.of(10)

Optional.OfNonZeroValue

Create an optional(T) value of a given value with type T if it is not azero-value. A zero-value the default empty value for any given CEL type,including empty protobuf message types. If the value is empty, the resultof this call will be optional.none().

optional.ofNonZeroValue([1, 2, 3]) // optional(list(int))optional.ofNonZeroValue([]) // optional.none()optional.ofNonZeroValue(0)  // optional.none()optional.ofNonZeroValue("") // optional.none()

Optional.None

Create an empty optional value.

HasValue

Determine whether the optional contains a value.

optional.of(b'hello').hasValue() // trueoptional.ofNonZeroValue({}).hasValue() // false

Value

Get the value contained by the optional. If the optional does not have avalue, the result will be a CEL error.

optional.of(b'hello').value() // b'hello'optional.ofNonZeroValue({}).value() // error

Or

If the value on the left-hand side is optional.none(), the optional valueon the right hand side is returned. If the value on the left-hand set isvalued, then it is returned. This operation is short-circuiting and willonly evaluate as many links in the `or` chain as are needed to return anon-empty optional value.

obj.?field.or(m[?key])l[?index].or(obj.?field.subfield).or(obj.?other)

OrValue

Either return the value contained within the optional on the left-hand sideor return the alternative value on the right hand side.

m[?key].orValue("none")

OptMap

Apply a transformation to the optional's underlying value if it is not emptyand return an optional typed result based on the transformation. Thetransformation expression type must return a type T which is wrapped intoan optional.

msg.?elements.optMap(e, e.size()).orValue(0)

OptFlatMap

Introduced in version: 1

Apply a transformation to the optional's underlying value if it is not emptyand return the result. The transform expression must return an optional(T)rather than type T. This can be useful when dealing with zero values andconditionally generating an empty or non-empty result in ways which cannotbe expressed with `optMap`.

msg.?elements.optFlatMap(e, e[?0]) // return the first element if present.

First

Introduced in version: 2

Returns an optional with the first value from the right hand list, oroptional.None.

[1, 2, 3].first().value() == 1

Last

Introduced in version: 2

Returns an optional with the last value from the right hand list, oroptional.None.

[1, 2, 3].last().value() == 3

This is syntactic sugar for msg.elements[msg.elements.size()-1].

Unwrap / UnwrapOpt

Introduced in version: 2

Returns a list of all the values that are not none in the input list of optional values.Can be used as optional.unwrap(List[T]) or with postfix notation: List[T].unwrapOpt()

optional.unwrap([optional.of(42), optional.none()]) == [42][optional.of(42), optional.none()].unwrapOpt() == [42]

funcParserExpressionSizeLimitadded inv0.16.0

func ParserExpressionSizeLimit(limitint)EnvOption

ParserExpressionSizeLimit adjusts the number of code points the expression parser is allowed to parse.Defaults defined in the parser package.

funcParserRecursionLimitadded inv0.13.0

func ParserRecursionLimit(limitint)EnvOption

ParserRecursionLimit adjusts the AST depth the parser will tolerate.Defaults defined in the parser package.

funcProtoAsDeclarationadded inv0.22.0

func ProtoAsDeclaration(d *celpb.Decl) (EnvOption,error)

ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.

funcStdLibadded inv0.4.0

func StdLib(opts ...StdLibOption)EnvOption

StdLib returns an EnvOption for the standard library of CEL functions and macros.

funcTypeDescsadded inv0.2.0

func TypeDescs(descs ...any)EnvOption

TypeDescs adds type declarations from any protoreflect.FileDescriptor, protoregistry.Files,google.protobuf.FileDescriptorProto or google.protobuf.FileDescriptorSet provided.

Note that messages instantiated from these descriptors will be *dynamicpb.Message valuesrather than the concrete message type.

TypeDescs are hermetic to a single Env object, but may be copied to other Env values viaextension or by re-using the same EnvOption with another NewEnv() call.

funcTypes

func Types(addTypes ...any)EnvOption

Types adds one or more type declarations to the environment, allowing for construction oftype-literals whose definitions are included in the common expression built-in set.

The input types may either be instances of `proto.Message` or `ref.Type`. Any other typeprovided to this option will result in an error.

Well-known protobuf types within the `google.protobuf.*` package are included in the standardenvironment by default.

Note: This option must be specified after the CustomTypeProvider option when used together.

funcVariableadded inv0.12.0

func Variable(namestring, t *Type)EnvOption

Variable creates an instance of a variable declaration with a variable name and type.

funcVariableDeclsadded inv0.24.0

func VariableDecls(vars ...*decls.VariableDecl)EnvOption

VariableDecls configures a set of fully defined cel.VariableDecl instances in the environment.

funcVariableWithDocadded inv0.25.0

func VariableWithDoc(namestring, t *Type, docstring)EnvOption

VariableWithDoc creates an instance of a variable declaration with a variable name, type, and doc string.

typeErroradded inv0.17.0

type Error =common.Error

Error type which references an expression id, a location within source, and a message.

funcExistsMacroExpanderadded inv0.12.0

func ExistsMacroExpander(mehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

ExistsMacroExpander expands the input call arguments into a comprehension that returns true if any of theelements in the range match the predicate expressions:<iterRange>.exists(<iterVar>, <predicate>)

funcExistsOneMacroExpanderadded inv0.12.0

func ExistsOneMacroExpander(mehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

ExistsOneMacroExpander expands the input call arguments into a comprehension that returns true if exactlyone of the elements in the range match the predicate expressions:<iterRange>.exists_one(<iterVar>, <predicate>)

funcFilterMacroExpanderadded inv0.12.0

func FilterMacroExpander(mehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

FilterMacroExpander expands the input call arguments into a comprehension which produces a list which containsonly elements which match the provided predicate expression:<iterRange>.filter(<iterVar>, <predicate>)

funcHasMacroExpanderadded inv0.12.0

func HasMacroExpander(mehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

HasMacroExpander expands the input call arguments into a presence test, e.g. has(<operand>.field)

funcMapMacroExpanderadded inv0.12.0

func MapMacroExpander(mehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

MapMacroExpander expands the input call arguments into a comprehension that transforms each element in theinput to produce an output list.

There are two call patterns supported by map:

<iterRange>.map(<iterVar>, <transform>)<iterRange>.map(<iterVar>, <predicate>, <transform>)

In the second form only iterVar values which return true when provided to the predicate expressionare transformed.

typeEvalDetails

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

EvalDetails holds additional information observed during the Eval() call.

func (*EvalDetails)ActualCostadded inv0.10.0

func (ed *EvalDetails) ActualCost() *uint64

ActualCost returns the tracked cost through the course of execution when `CostTracking` is enabled.Otherwise, returns nil if the cost was not enabled.

func (*EvalDetails)State

State of the evaluation, non-nil if the OptTrackState or OptExhaustiveEval is specifiedwithin EvalOptions.

typeEvalOption

type EvalOptionint

EvalOption indicates an evaluation option that may affect the evaluation behavior or informationin the output result.

const (// OptTrackState will cause the runtime to return an immutable EvalState value in the Result.OptTrackStateEvalOption = 1 <<iota// OptExhaustiveEval causes the runtime to disable short-circuits and track state.OptExhaustiveEvalEvalOption = 1<<iota |OptTrackState// OptOptimize precomputes functions and operators with constants as arguments at program// creation time. It also pre-compiles regex pattern constants passed to 'matches', reports any compilation errors// at program creation and uses the compiled regex pattern for all 'matches' function invocations.// This flag is useful when the expression will be evaluated repeatedly against// a series of different inputs.OptOptimizeEvalOption = 1 <<iota// OptPartialEval enables the evaluation of a partial state where the input data that may be// known to be missing, either as top-level variables, or somewhere within a variable's object// member graph.//// By itself, OptPartialEval does not change evaluation behavior unless the input to the// Program Eval() call is created via PartialVars().OptPartialEvalEvalOption = 1 <<iota// OptTrackCost enables the runtime cost calculation while validation and return cost within evalDetails// cost calculation is available via func ActualCost()OptTrackCostEvalOption = 1 <<iota// OptCheckStringFormat enables compile-time checking of string.format calls for syntax/cardinality.//// Deprecated: use ext.StringsValidateFormatCalls() as this option is now a no-op.OptCheckStringFormatEvalOption = 1 <<iota)

typeFunctionOptadded inv0.12.0

type FunctionOpt =decls.FunctionOpt

FunctionOpt defines a functional option for configuring a function declaration.

funcDisableDeclarationadded inv0.17.0

func DisableDeclaration(valuebool)FunctionOpt

DisableDeclaration disables the function signatures, effectively removing them from the type-checkenvironment while preserving the runtime bindings.

funcFunctionDocsadded inv0.25.0

func FunctionDocs(docs ...string)FunctionOpt

FunctionDocs provides a general usage documentation for the function.

Use OverloadExamples to provide example usage instructions for specific overloads.

funcMemberOverloadadded inv0.12.0

func MemberOverload(overloadIDstring, args []*Type, resultType *Type, opts ...OverloadOpt)FunctionOpt

MemberOverload defines a new receiver-style overload (or member function) with an overload id, argument types,and result type. Through the use of OverloadOpt options, the overload may also be configured with a binding,an operand trait, and to be non-strict.

Note: function bindings should be commonly configured with Overload instances whereas operand traits andstrict-ness should be rare occurrences.

funcOverloadadded inv0.12.0

func Overload(overloadIDstring, args []*Type, resultType *Type, opts ...OverloadOpt)FunctionOpt

Overload defines a new global overload with an overload id, argument types, and result type. Through theuse of OverloadOpt options, the overload may also be configured with a binding, an operand trait, and tobe non-strict.

Note: function bindings should be commonly configured with Overload instances whereas operand traits andstrict-ness should be rare occurrences.

funcSingletonBinaryBindingadded inv0.13.0

func SingletonBinaryBinding(fnfunctions.BinaryOp, traits ...int)FunctionOpt

SingletonBinaryBinding creates a singleton function definition to be used with all function overloads.

Note, this approach works well if operand is expected to have a specific trait which it implements,e.g. traits.ContainerType. Otherwise, prefer per-overload function bindings.

funcSingletonBinaryImpldeprecatedadded inv0.12.0

func SingletonBinaryImpl(fnfunctions.BinaryOp, traits ...int)FunctionOpt

SingletonBinaryImpl creates a singleton function definition to be used with all function overloads.

Note, this approach works well if operand is expected to have a specific trait which it implements,e.g. traits.ContainerType. Otherwise, prefer per-overload function bindings.

Deprecated: use SingletonBinaryBinding

funcSingletonFunctionBindingadded inv0.13.0

func SingletonFunctionBinding(fnfunctions.FunctionOp, traits ...int)FunctionOpt

SingletonFunctionBinding creates a singleton function definition to be used with all function overloads.

Note, this approach works well if operand is expected to have a specific trait which it implements,e.g. traits.ContainerType. Otherwise, prefer per-overload function bindings.

funcSingletonFunctionImpldeprecatedadded inv0.12.0

func SingletonFunctionImpl(fnfunctions.FunctionOp, traits ...int)FunctionOpt

SingletonFunctionImpl creates a singleton function definition to be used with all function overloads.

Note, this approach works well if operand is expected to have a specific trait which it implements,e.g. traits.ContainerType. Otherwise, prefer per-overload function bindings.

Deprecated: use SingletonFunctionBinding

funcSingletonUnaryBindingadded inv0.12.0

func SingletonUnaryBinding(fnfunctions.UnaryOp, traits ...int)FunctionOpt

SingletonUnaryBinding creates a singleton function definition to be used for all function overloads.

Note, this approach works well if operand is expected to have a specific trait which it implements,e.g. traits.ContainerType. Otherwise, prefer per-overload function bindings.

typeInlineVariableadded inv0.18.0

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

InlineVariable holds a variable name to be matched and an AST representingthe expression graph which should be used to replace it.

funcNewInlineVariableadded inv0.18.0

func NewInlineVariable(namestring, definition *Ast) *InlineVariable

NewInlineVariable declares a variable name to be replaced by a checked expression.

funcNewInlineVariableWithAliasadded inv0.18.0

func NewInlineVariableWithAlias(name, aliasstring, definition *Ast) *InlineVariable

NewInlineVariableWithAlias declares a variable name to be replaced by a checked expression.If the variable occurs more than once, the provided alias will be used to replace the expressionswhere the variable name occurs.

func (*InlineVariable)Aliasadded inv0.18.0

func (v *InlineVariable) Alias()string

Alias returns the alias to use when performing cel.bind() calls during inlining.

func (*InlineVariable)Expradded inv0.18.0

func (v *InlineVariable) Expr()ast.Expr

Expr returns the inlined expression value.

func (*InlineVariable)Nameadded inv0.18.0

func (v *InlineVariable) Name()string

Name returns the qualified variable or field selection to replace.

func (*InlineVariable)Typeadded inv0.18.0

func (v *InlineVariable) Type() *Type

Type indicates the inlined expression type.

typeIssues

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

Issues defines methods for inspecting the error details of parse and check calls.

Note: in the future, non-fatal warnings and notices may be inspectable via the Issues struct.

funcNewIssuesadded inv0.4.0

func NewIssues(errs *common.Errors) *Issues

NewIssues returns an Issues struct from a common.Errors object.

funcNewIssuesWithSourceInfoadded inv0.17.0

func NewIssuesWithSourceInfo(errs *common.Errors, info *celast.SourceInfo) *Issues

NewIssuesWithSourceInfo returns an Issues struct from a common.Errors object with SourceInfo metatatawhich can be used with the `ReportErrorAtID` method for additional error reports within the contextinformation that's inferred from an expression id.

func (*Issues)Appendadded inv0.4.0

func (i *Issues) Append(other *Issues) *Issues

Append collects the issues from another Issues struct into a new Issues object.

func (*Issues)Err

func (i *Issues) Err()error

Err returns an error value if the issues list contains one or more errors.

func (*Issues)Errors

func (i *Issues) Errors() []*Error

Errors returns the collection of errors encountered in more granular detail.

func (*Issues)ReportErrorAtIDadded inv0.17.0

func (i *Issues) ReportErrorAtID(idint64, messagestring, args ...any)

ReportErrorAtID reports an error message with an optional set of formatting arguments.

The source metadata for the expression at `id`, if present, is attached to the error report.To ensure that source metadata is attached to error reports, use NewIssuesWithSourceInfo.

func (*Issues)Stringadded inv0.4.0

func (i *Issues) String()string

String converts the issues to a suitable display string.

typeKindadded inv0.12.0

type Kind =types.Kind

Kind indicates a CEL type's kind which is used to differentiate quickly between simple and complex types.

typeLibraryadded inv0.4.0

type Library interface {// CompileOptions returns a collection of functional options for configuring the Parse / Check// environment.CompileOptions() []EnvOption// ProgramOptions returns a collection of functional options which should be included in every// Program generated from the Env.Program() call.ProgramOptions() []ProgramOption}

Library provides a collection of EnvOption and ProgramOption values used to configure a CELenvironment for a particular use case or with a related set of functionality.

Note, the ProgramOption values provided by a library are expected to be static and not varybetween calls to Env.Program(). If there is a need for such dynamic configuration, prefer toconfigure these options outside the Library and within the Env.Program() call directly.

typeLibraryAliaseradded inv0.24.0

type LibraryAliaser interface {LibraryAlias()string}

LibraryAliaser generates a simple named alias for the library, for use during environment serialization.

typeLibrarySubsetteradded inv0.24.0

type LibrarySubsetter interface {LibrarySubset() *env.LibrarySubset}

LibrarySubsetter provides the subset description associated with the library, nil if not subset.

typeLibraryVersioneradded inv0.24.0

type LibraryVersioner interface {LibraryVersion()uint32}

LibraryVersioner provides a version number for the library.

If not implemented, the library version will be flagged as 'latest' during environment serialization.

typeMacroadded inv0.12.0

type Macro =parser.Macro

Macro describes a function signature to match and the MacroExpander to apply.

Note: when a Macro should apply to multiple overloads (based on arg count) of a given function,a Macro should be created per arg-count or as a var arg macro.

funcGlobalMacroadded inv0.17.2

func GlobalMacro(functionstring, argCountint, factoryMacroFactory, opts ...MacroOpt)Macro

GlobalMacro creates a Macro for a global function with the specified arg count.

funcGlobalVarArgMacroadded inv0.17.2

func GlobalVarArgMacro(functionstring, factoryMacroFactory, opts ...MacroOpt)Macro

GlobalVarArgMacro creates a Macro for a global function with a variable arg count.

funcNewGlobalMacrodeprecatedadded inv0.12.0

func NewGlobalMacro(functionstring, argCountint, expanderMacroExpander)Macro

NewGlobalMacro creates a Macro for a global function with the specified arg count.

Deprecated: use GlobalMacro

funcNewGlobalVarArgMacrodeprecatedadded inv0.12.0

func NewGlobalVarArgMacro(functionstring, expanderMacroExpander)Macro

NewGlobalVarArgMacro creates a Macro for a global function with a variable arg count.

Deprecated: use GlobalVarArgMacro

funcNewReceiverMacrodeprecatedadded inv0.12.0

func NewReceiverMacro(functionstring, argCountint, expanderMacroExpander)Macro

NewReceiverMacro creates a Macro for a receiver function matching the specified arg count.

Deprecated: use ReceiverMacro

funcNewReceiverVarArgMacrodeprecatedadded inv0.12.0

func NewReceiverVarArgMacro(functionstring, expanderMacroExpander)Macro

NewReceiverVarArgMacro creates a Macro for a receiver function matching a variable arg count.

Deprecated: use ReceiverVarArgMacro

funcReceiverMacroadded inv0.17.2

func ReceiverMacro(functionstring, argCountint, factoryMacroFactory, opts ...MacroOpt)Macro

ReceiverMacro creates a Macro for a receiver function matching the specified arg count.

funcReceiverVarArgMacroadded inv0.17.2

func ReceiverVarArgMacro(functionstring, factoryMacroFactory, opts ...MacroOpt)Macro

ReceiverVarArgMacro creates a Macro for a receiver function matching a variable arg count.

typeMacroExpanderadded inv0.12.0

type MacroExpander func(ehMacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error)

MacroExpander converts a call and its associated arguments into a protobuf Expr representation.

If the MacroExpander determines within the implementation that an expansion is not needed it may returna nil Expr value to indicate a non-match. However, if an expansion is to be performed, but the argumentsare not well-formed, the result of the expansion will be an error.

The MacroExpander accepts as arguments a MacroExprHelper as well as the arguments used in the function calland produces as output an Expr ast node.

Note: when the Macro.IsReceiverStyle() method returns true, the target argument will be nil.

typeMacroExprFactoryadded inv0.17.2

type MacroExprFactory =parser.ExprHelper

MacroExprFactory assists with the creation of Expr values in a manner which is consistentthe internal semantics and id generation behaviors of the parser and checker libraries.

typeMacroExprHelperadded inv0.12.0

type MacroExprHelper interface {// Copy the input expression with a brand new set of identifiers.Copy(*exprpb.Expr) *exprpb.Expr// LiteralBool creates an Expr value for a bool literal.LiteralBool(valuebool) *exprpb.Expr// LiteralBytes creates an Expr value for a byte literal.LiteralBytes(value []byte) *exprpb.Expr// LiteralDouble creates an Expr value for double literal.LiteralDouble(valuefloat64) *exprpb.Expr// LiteralInt creates an Expr value for an int literal.LiteralInt(valueint64) *exprpb.Expr// LiteralString creates am Expr value for a string literal.LiteralString(valuestring) *exprpb.Expr// LiteralUint creates an Expr value for a uint literal.LiteralUint(valueuint64) *exprpb.Expr// NewList creates a CreateList instruction where the list is comprised of the optional set// of elements provided as arguments.NewList(elems ...*exprpb.Expr) *exprpb.Expr// NewMap creates a CreateStruct instruction for a map where the map is comprised of the// optional set of key, value entries.NewMap(entries ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr// NewMapEntry creates a Map Entry for the key, value pair.NewMapEntry(key *exprpb.Expr, val *exprpb.Expr, optionalbool) *exprpb.Expr_CreateStruct_Entry// NewObject creates a CreateStruct instruction for an object with a given type name and// optional set of field initializers.NewObject(typeNamestring, fieldInits ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr// NewObjectFieldInit creates a new Object field initializer from the field name and value.NewObjectFieldInit(fieldstring, init *exprpb.Expr, optionalbool) *exprpb.Expr_CreateStruct_Entry// Fold creates a fold comprehension instruction.//// - iterVar is the iteration variable name.// - iterRange represents the expression that resolves to a list or map where the elements or//   keys (respectively) will be iterated over.// - accuVar is the accumulation variable name, typically parser.AccumulatorName.// - accuInit is the initial expression whose value will be set for the accuVar prior to//   folding.// - condition is the expression to test to determine whether to continue folding.// - step is the expression to evaluation at the conclusion of a single fold iteration.// - result is the computation to evaluate at the conclusion of the fold.//// The accuVar should not shadow variable names that you would like to reference within the// environment in the step and condition expressions. Presently, the name __result__ is commonly// used by built-in macros but this may change in the future.Fold(iterVarstring,iterRange *exprpb.Expr,accuVarstring,accuInit *exprpb.Expr,condition *exprpb.Expr,step *exprpb.Expr,result *exprpb.Expr) *exprpb.Expr// Ident creates an identifier Expr value.Ident(namestring) *exprpb.Expr// AccuIdent returns an accumulator identifier for use with comprehension results.AccuIdent() *exprpb.Expr// GlobalCall creates a function call Expr value for a global (free) function.GlobalCall(functionstring, args ...*exprpb.Expr) *exprpb.Expr// ReceiverCall creates a function call Expr value for a receiver-style function.ReceiverCall(functionstring, target *exprpb.Expr, args ...*exprpb.Expr) *exprpb.Expr// PresenceTest creates a Select TestOnly Expr value for modelling has() semantics.PresenceTest(operand *exprpb.Expr, fieldstring) *exprpb.Expr// Select create a field traversal Expr value.Select(operand *exprpb.Expr, fieldstring) *exprpb.Expr// OffsetLocation returns the Location of the expression identifier.OffsetLocation(exprIDint64)common.Location// NewError associates an error message with a given expression id.NewError(exprIDint64, messagestring) *Error}

MacroExprHelper exposes helper methods for creating new expressions within a CEL abstract syntax tree.ExprHelper assists with the manipulation of proto-based Expr values in a manner which isconsistent with the source position and expression id generation code leveraged by boththe parser and type-checker.

typeMacroFactoryadded inv0.17.2

type MacroFactory =parser.MacroExpander

MacroFactory defines an expansion function which converts a call and its arguments to a cel.Expr value.

typeMacroOptadded inv0.25.0

type MacroOpt =parser.MacroOpt

MacroOpt defines a functional option for configuring macro behavior.

funcMacroDocsadded inv0.25.0

func MacroDocs(docs ...string)MacroOpt

MacroDocs configures a list of strings into a multiline description for the macro.

funcMacroExamplesadded inv0.25.0

func MacroExamples(examples ...string)MacroOpt

MacroExamples configures a list of examples, either as a string or common.MultilineString,into an example set to be provided with the macro Documentation() call.

typeMutableValidatorConfigadded inv0.17.0

type MutableValidatorConfig interface {ValidatorConfigSet(namestring, valueany)error}

MutableValidatorConfig provides mutation methods for querying and updating validator configurationsettings.

typeOptimizerContextadded inv0.18.0

type OptimizerContext struct {*Env*Issues// contains filtered or unexported fields}

OptimizerContext embeds Env and Issues instances to make it easy to type-check and evaluatesubexpressions and report any errors encountered along the way. The context also embeds theoptimizerExprFactory which can be used to generate new sub-expressions with expression idsconsistent with the expectations of a parsed expression.

func (OptimizerContext)ClearMacroCalladded inv0.20.1

func (opt OptimizerContext) ClearMacroCall(idint64)

ClearMacroCall clears the macro at the given expression id.

func (OptimizerContext)CopyASTadded inv0.18.2

func (opt OptimizerContext) CopyAST(a *ast.AST) (ast.Expr, *ast.SourceInfo)

CopyAST creates a renumbered copy of `Expr` and `SourceInfo` values of the input AST, where therenumbering uses the same scheme as the core optimizer logic ensuring there are no collisionsbetween copies.

Use this method before attempting to merge the expression from AST into another.

func (OptimizerContext)CopyASTAndMetadataadded inv0.20.1

func (opt OptimizerContext) CopyASTAndMetadata(a *ast.AST)ast.Expr

CopyASTAndMetadata copies the input AST and propagates the macro metadata into the AST beingoptimized.

func (*OptimizerContext)ExtendEnvadded inv0.22.0

func (opt *OptimizerContext) ExtendEnv(opts ...EnvOption)error

ExtendEnv auguments the context's environment with the additional options.

func (OptimizerContext)MacroCallsadded inv0.21.0

func (opt OptimizerContext) MacroCalls() map[int64]ast.Expr

MacroCalls returns the map of macro calls currently in the context.

func (OptimizerContext)NewASTadded inv0.20.1

func (opt OptimizerContext) NewAST(exprast.Expr) *ast.AST

NewAST creates an AST from the current expression using the tracked source info whichis modified and managed by the OptimizerContext.

func (OptimizerContext)NewBindMacroadded inv0.18.0

func (opt OptimizerContext) NewBindMacro(macroIDint64, varNamestring, varInit, remainingast.Expr) (astExpr, macroExprast.Expr)

NewBindMacro creates an AST expression representing the expanded bind() macro, and a macro expressionrepresenting the unexpanded call signature to be inserted into the source info macro call metadata.

func (OptimizerContext)NewCalladded inv0.18.0

func (opt OptimizerContext) NewCall(functionstring, args ...ast.Expr)ast.Expr

NewCall creates a global function call invocation expression.

Example:

countByField(list, fieldName)- function: countByField- args: [list, fieldName]

func (OptimizerContext)NewHasMacroadded inv0.18.2

func (opt OptimizerContext) NewHasMacro(macroIDint64, sast.Expr) (astExpr, macroExprast.Expr)

NewHasMacro generates a test-only select expression to be included within an AST and an unexpandedhas() macro call signature to be inserted into the source info macro call metadata.

func (OptimizerContext)NewIdentadded inv0.18.0

func (opt OptimizerContext) NewIdent(namestring)ast.Expr

NewIdent creates a new identifier expression.

Examples:

- simple_var_name- qualified.subpackage.var_name

func (OptimizerContext)NewListadded inv0.18.0

func (opt OptimizerContext) NewList(elems []ast.Expr, optIndices []int32)ast.Expr

NewList creates a list expression with a set of optional indices.

Examples:

[a, b]- elems: [a, b]- optIndices: []

[a, ?b, ?c]- elems: [a, b, c]- optIndices: [1, 2]

func (OptimizerContext)NewLiteraladded inv0.18.0

func (opt OptimizerContext) NewLiteral(valueref.Val)ast.Expr

NewLiteral creates a new literal expression value.

The range of valid values for a literal generated during optimization is different than for expressionsgenerated via parsing / type-checking, as the ref.Val may be _any_ CEL value so long as the value canbe converted back to a literal-like form.

func (OptimizerContext)NewMapadded inv0.18.0

func (opt OptimizerContext) NewMap(entries []ast.EntryExpr)ast.Expr

NewMap creates a map from a set of entry expressions which contain a key and value expression.

func (OptimizerContext)NewMapEntryadded inv0.18.0

func (opt OptimizerContext) NewMapEntry(key, valueast.Expr, isOptionalbool)ast.EntryExpr

NewMapEntry creates a map entry with a key and value expression and a flag to indicate whether theentry is optional.

Examples:

{a: b}- key: a- value: b- optional: false

{?a: ?b}- key: a- value: b- optional: true

func (OptimizerContext)NewMemberCalladded inv0.18.0

func (opt OptimizerContext) NewMemberCall(functionstring, targetast.Expr, args ...ast.Expr)ast.Expr

NewMemberCall creates a member function call invocation expression where 'target' is the receiver of the call.

Example:

list.countByField(fieldName)- function: countByField- target: list- args: [fieldName]

func (OptimizerContext)NewSelectadded inv0.18.0

func (opt OptimizerContext) NewSelect(operandast.Expr, fieldstring)ast.Expr

NewSelect creates a select expression where a field value is selected from an operand.

Example:

msg.field_name- operand: msg- field: field_name

func (OptimizerContext)NewStructadded inv0.18.0

func (opt OptimizerContext) NewStruct(typeNamestring, fields []ast.EntryExpr)ast.Expr

NewStruct creates a new typed struct value with an set of field initializations.

Example:

pkg.TypeName{field: value}- typeName: pkg.TypeName- fields: [{field: value}]

func (OptimizerContext)NewStructFieldadded inv0.18.0

func (opt OptimizerContext) NewStructField(fieldstring, valueast.Expr, isOptionalbool)ast.EntryExpr

NewStructField creates a struct field initialization.

Examples:

{count: 3u}- field: count- value: 3u- optional: false

{?count: x}- field: count- value: x- optional: true

func (OptimizerContext)SetMacroCalladded inv0.20.1

func (opt OptimizerContext) SetMacroCall(idint64, exprast.Expr)

SetMacroCall sets the macro call metadata for the given macro id within the tracked source infometadata.

func (OptimizerContext)UpdateExpradded inv0.18.2

func (opt OptimizerContext) UpdateExpr(target, updatedast.Expr)

UpdateExpr updates the target expression with the updated content while preserving macro metadata.

There are four scenarios during the update to consider:1. target is not macro, updated is not macro2. target is macro, updated is not macro3. target is macro, updated is macro4. target is not macro, updated is macro

When the target is a macro already, it may either be updated to a new macro functionbody if the update is also a macro, or it may be removed altogether if the update isa macro.

When the update is a macro, then the target references within other macros must beupdated to point to the new updated macro. Otherwise, other macros which pointed tothe target body must be replaced with copies of the updated expression body.

typeOptionalTypesOptionadded inv0.17.0

type OptionalTypesOption func(*optionalLib) *optionalLib

OptionalTypesOption is a functional interface for configuring the strings library.

funcOptionalTypesVersionadded inv0.17.0

func OptionalTypesVersion(versionuint32)OptionalTypesOption

OptionalTypesVersion configures the version of the optional type library.

The version limits which functions are available. Only functions introducedbelow or equal to the given version included in the library. If this optionis not set, all functions are available.

See the library documentation to determine which version a function was introduced.If the documentation does not state which version a function was introduced, it canbe assumed to be introduced at version 0, when the library was first created.

typeOverloadOptadded inv0.12.0

type OverloadOpt =decls.OverloadOpt

OverloadOpt is a functional option for configuring a function overload.

funcBinaryBindingadded inv0.12.0

func BinaryBinding(bindingfunctions.BinaryOp)OverloadOpt

BinaryBinding provides the implementation of a binary overload. The provided function is protected by a runtimetype-guard which ensures runtime type agreement between the overload signature and runtime argument types.

funcFunctionBindingadded inv0.12.0

func FunctionBinding(bindingfunctions.FunctionOp)OverloadOpt

FunctionBinding provides the implementation of a variadic overload. The provided function is protected by a runtimetype-guard which ensures runtime type agreement between the overload signature and runtime argument types.

funcLateFunctionBindingadded inv0.25.0

func LateFunctionBinding()OverloadOpt

LateFunctionBinding indicates that the function has a binding which is not known at compile time.This is useful for functions which have side-effects or are not deterministically computable.

funcOverloadExamplesadded inv0.25.0

func OverloadExamples(docs ...string)OverloadOpt

OverloadExamples configures an example of how to invoke the overload.

funcOverloadIsNonStrictadded inv0.12.0

func OverloadIsNonStrict()OverloadOpt

OverloadIsNonStrict enables the function to be called with error and unknown argument values.

Note: do not use this option unless absoluately necessary as it should be an uncommon feature.

funcOverloadOperandTraitadded inv0.12.0

func OverloadOperandTrait(traitint)OverloadOpt

OverloadOperandTrait configures a set of traits which the first argument to the overload must implement in order to besuccessfully invoked.

funcUnaryBindingadded inv0.12.0

func UnaryBinding(bindingfunctions.UnaryOp)OverloadOpt

UnaryBinding provides the implementation of a unary overload. The provided function is protected by a runtimetype-guard which ensures runtime type agreement between the overload signature and runtime argument types.

typeOverloadSelectoradded inv0.24.0

type OverloadSelector =decls.OverloadSelector

OverloadSelector selects an overload associated with a given function when it returns true.

Used in combination with the FunctionDecl.Subset method.

funcExcludeOverloadsadded inv0.24.0

func ExcludeOverloads(overloadIDs ...string)OverloadSelector

ExcludeOverloads defines an OverloadSelector which deny-lists a set of overloads by their ids.

funcIncludeOverloadsadded inv0.24.0

func IncludeOverloads(overloadIDs ...string)OverloadSelector

IncludeOverloads defines an OverloadSelector which allow-lists a set of overloads by their ids.

typePartialActivationadded inv0.24.0

type PartialActivation =interpreter.PartialActivation

PartialActivation extends the Activation interface with a set of unknown AttributePatterns.

funcPartialVarsadded inv0.4.0

func PartialVars(varsany,unknowns ...*AttributePatternType) (PartialActivation,error)

PartialVars returns a PartialActivation which contains variables and a set of AttributePatternvalues that indicate variables or parts of variables whose value are not yet known.

This method relies on manually configured sets of missing attribute patterns. For a method whichinfers the missing variables from the input and the configured environment, use Env.PartialVars().

The `vars` value may either be an Activation or any valid input to the NewActivation call.

typeProgram

type Program interface {// Eval returns the result of an evaluation of the Ast and environment against the input vars.//// The vars value may either be an `Activation` or a `map[string]any`.//// If the `OptTrackState`, `OptTrackCost` or `OptExhaustiveEval` flags are used, the `details` response will// be non-nil. Given this caveat on `details`, the return state from evaluation will be://// *  `val`, `details`, `nil` - Successful evaluation of a non-error result.// *  `val`, `details`, `err` - Successful evaluation to an error result.// *  `nil`, `details`, `err` - Unsuccessful evaluation.//// An unsuccessful evaluation is typically the result of a series of incompatible `EnvOption`// or `ProgramOption` values used in the creation of the evaluation environment or executable// program.Eval(any) (ref.Val, *EvalDetails,error)// ContextEval evaluates the program with a set of input variables and a context object in order// to support cancellation and timeouts. This method must be used in conjunction with the// InterruptCheckFrequency() option for cancellation interrupts to be impact evaluation.//// The vars value may either be an `Activation` or `map[string]any`.//// The output contract for `ContextEval` is otherwise identical to the `Eval` method.ContextEval(context.Context,any) (ref.Val, *EvalDetails,error)}

Program is an evaluable view of an Ast.

typeProgramOption

type ProgramOption func(p *prog) (*prog,error)

ProgramOption is a functional interface for configuring evaluation bindings and behaviors.

funcCostLimitadded inv0.10.0

func CostLimit(costLimituint64)ProgramOption

CostLimit enables cost tracking and sets configures program evaluation to exit early with a"runtime cost limit exceeded" error if the runtime cost exceeds the costLimit.The CostLimit is a metric that corresponds to the number and estimated expense of operationsperformed while evaluating an expression. It is indicative of CPU usage, not memory usage.

funcCostTrackerOptionsadded inv0.17.7

func CostTrackerOptions(costOpts ...interpreter.CostTrackerOption)ProgramOption

CostTrackerOptions configures a set of options for cost-tracking.

Note, CostTrackerOptions is a no-op unless CostTracking is also enabled.

funcCostTrackingadded inv0.10.0

func CostTracking(costEstimatorinterpreter.ActualCostEstimator)ProgramOption

CostTracking enables cost tracking and registers a ActualCostEstimator that can optionally provide a runtime cost estimate for any function calls.

funcCustomDecoratoradded inv0.6.0

CustomDecorator appends an InterpreterDecorator to the program.

InterpretableDecorators can be used to inspect, alter, or replace the Program plan.

funcEvalOptions

func EvalOptions(opts ...EvalOption)ProgramOption

EvalOptions sets one or more evaluation options which may affect the evaluation or Result.

funcFunctionsdeprecated

func Functions(funcs ...*functions.Overload)ProgramOption

Functions adds function overloads that extend or override the set of CEL built-ins.

Deprecated: use Function() instead to declare the function, its overload signatures,and the overload implementations.

funcGlobals

func Globals(varsany)ProgramOption

Globals sets the global variable values for a given program. These values may be shadowed byvariables with the same name provided to the Eval() call. If Globals is used in a Library witha Lib EnvOption, vars may shadow variables provided by previously added libraries.

The vars value may either be an `cel.Activation` instance or a `map[string]any`.

funcInterruptCheckFrequencyadded inv0.10.0

func InterruptCheckFrequency(checkFrequencyuint)ProgramOption

InterruptCheckFrequency configures the number of iterations within a comprehension to evaluatebefore checking whether the function evaluation has been interrupted.

funcOptimizeRegexadded inv0.10.0

func OptimizeRegex(regexOptimizations ...*interpreter.RegexOptimization)ProgramOption

OptimizeRegex provides a way to replace the InterpretableCall for regex functions. This can be usedto compile regex string constants at program creation time and report any errors and then use thecompiled regex for all regex function invocations.

typePromptadded inv0.25.0

type Prompt struct {// Persona indicates something about the kind of user making the requestPersonastring// FormatRules indicate how the LLM should generate its outputFormatRulesstring// GeneralUsage specifies additional context on how CEL should be used.GeneralUsagestring// contains filtered or unexported fields}

Prompt represents the core components of an LLM prompt based on a CEL environment.

All fields of the prompt may be overwritten / modified with support for rendering theprompt to a human-readable string.

funcAuthoringPromptadded inv0.25.0

func AuthoringPrompt(env *Env) (*Prompt,error)

AuthoringPrompt creates a prompt template from a CEL environment for the purpose of AI-assisted authoring.

func (*Prompt)Renderadded inv0.25.0

func (p *Prompt) Render(userPromptstring)string

Render renders the user prompt with the associated context from the prompt templatefor use with LLM generators.

typeSingletonLibraryadded inv0.13.0

type SingletonLibrary interface {Library// LibraryName provides a namespaced name which is used to check whether the library has already// been configured in the environment.LibraryName()string}

SingletonLibrary refines the Library interface to ensure that libraries in this format are onlyconfigured once within the environment.

typeSource

type Source =common.Source

Source interface representing a user-provided expression.

typeStaticOptimizeradded inv0.18.0

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

StaticOptimizer contains a sequence of ASTOptimizer instances which will be applied in order.

The static optimizer normalizes expression ids and type-checking run between optimizationpasses to ensure that the final optimized output is a valid expression with metadata consistentwith what would have been generated from a parsed and checked expression.

Note: source position information is best-effort and likely wrong, but optimized expressionsshould be suitable for calls to parser.Unparse.

funcNewStaticOptimizeradded inv0.18.0

func NewStaticOptimizer(optimizers ...ASTOptimizer) *StaticOptimizer

NewStaticOptimizer creates a StaticOptimizer with a sequence of ASTOptimizer's to be appliedto a checked expression.

func (*StaticOptimizer)Optimizeadded inv0.18.0

func (opt *StaticOptimizer) Optimize(env *Env, a *Ast) (*Ast, *Issues)

Optimize applies a sequence of optimizations to an Ast within a given environment.

If issues are encountered, the Issues.Err() return value will be non-nil.

typeStdLibOptionadded inv0.24.0

type StdLibOption func(*stdLibrary) *stdLibrary

StdLibOption specifies a functional option for configuring the standard CEL library.

funcStdLibSubsetadded inv0.24.0

func StdLibSubset(subset *env.LibrarySubset)StdLibOption

StdLibSubset configures the standard library to use a subset of its functions and macros.

Since the StdLib is a singleton library, only the first instance of the StdLib() environment optionswill be configured on the environment which means only the StdLibSubset() initially configured withthe library will be used.

typeTypeadded inv0.12.0

type Type =types.Type

Type holds a reference to a runtime type with an optional type-checked set of type parameters.

funcExprTypeToTypeadded inv0.12.0

func ExprTypeToType(t *exprpb.Type) (*Type,error)

ExprTypeToType converts a protobuf CEL type representation to a CEL-native type representation.

typeValidatorConfigadded inv0.17.0

type ValidatorConfig interface {GetOrDefault(namestring, valueany)any}

ValidatorConfig provides an accessor method for querying validator configuration state.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp