env

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GlobalCacheSize is the size of the global AST cache shared across all CEL evaluations.
	// This cache significantly improves performance by reusing compiled CEL programs.
	GlobalCacheSize = 10000
)

Variables

This section is empty.

Functions

func DebugOutEnvOptions

func DebugOutEnvOptions(w *writer.Writer) []cel.EnvOption

DebugOutEnvOptions returns the CEL environment options for debug.out with the given Writer. This is useful for adding debug.out support to environments created via New(). If Writer is nil, debug.out will silently skip output.

func GlobalCache

func GlobalCache() *celexp.ProgramCache

GlobalCache returns the global singleton program cache used for AST caching. This cache is shared across all CEL evaluations and significantly improves performance by reusing compiled CEL programs.

The cache is initialized on first call with GlobalCacheSize entries. This function is thread-safe and can be called concurrently.

Example usage:

cache := env.GlobalCache()
if cache != nil {
    stats := cache.Stats()
    fmt.Printf("Cache hit rate: %.2f%%\n", stats.HitRate)
}

func New

func New(ctx context.Context, declarations ...cel.EnvOption) (*cel.Env, error)

New creates a new CEL environment with the provided declarations and all registered CEL extension functions from the ext package. It accepts variadic EnvOptions to allow for multiple declarations and other options.

The function caches base extension options for performance, so repeated calls are much faster than the first call. The context is checked for cancellation before and during environment construction.

debug.out is automatically included if a Writer is found in the context via writer.FromContext(ctx). If no Writer is in context, debug.out is not available. To explicitly control debug.out, use NewWithWriter() instead.

Example:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
env, err := env.New(ctx, cel.Variable("x", cel.IntType))

func NewWithWriter

func NewWithWriter(ctx context.Context, w *writer.Writer, declarations ...cel.EnvOption) (*cel.Env, error)

NewWithWriter creates a new CEL environment with debug.out support. This is a convenience wrapper around New() that includes debug.DebugOutFunc.

The Writer parameter is used by debug.out for debug output. Pass nil if debug output should be silently skipped.

Example:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
env, err := env.NewWithWriter(ctx, w, cel.Variable("x", cel.IntType))

Types

This section is empty.

Jump to

Keyboard shortcuts

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