compiler

package
v0.0.0-...-c55aebf Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2025 License: Apache-2.0, BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package compiler exposes a standard way to set up a compiler which can be used for CEL expressions and policies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvironmentFile

func EnvironmentFile(path string) cel.EnvOption

EnvironmentFile returns an EnvOption which loads a serialized CEL environment from a file. The file must be in one of the following formats: - Textproto - YAML - Binarypb

func TypeDescriptorSetFile

func TypeDescriptorSetFile(path string) cel.EnvOption

TypeDescriptorSetFile returns an EnvOption which loads type descriptors from a file. The file must be in binary format.

Types

type CompiledExpression

type CompiledExpression struct {
	Path string
}

CompiledExpression is an InputExpression which loads a CheckedExpr from a file.

func (*CompiledExpression) CreateAST

func (c *CompiledExpression) CreateAST(_ Compiler) (*cel.Ast, map[string]any, error)

CreateAST creates a CEL AST from a checked expression file. The file must be in one of the following formats: - Binarypb - Textproto

type Compiler

type Compiler interface {
	// CreateEnv creates a singleton CEL environment with the configured environment options.
	CreateEnv() (*cel.Env, error)
	// CreatePolicyParser creates a policy parser using the optionally configured parser options.
	CreatePolicyParser() (*policy.Parser, error)
	// PolicyCompilerOptions returns the policy compiler options.
	PolicyCompilerOptions() []policy.CompilerOption
	// PolicyMetadataEnvOptions returns the policy metadata environment options.
	PolicyMetadataEnvOptions() []PolicyMetadataEnvOption
}

Compiler interface is used to set up a compiler with the following capabilities: - create a CEL environment - create a policy parser - fetch policy compiler options - fetch policy environment options

Note: This compiler is not the same as the CEL expression compiler, rather it provides an abstraction layer which can create the different components needed for parsing and compiling CEL expressions and policies.

func NewCompiler

func NewCompiler(opts ...any) (Compiler, error)

NewCompiler creates a new compiler with a set of functional options.

type ExpressionType

type ExpressionType int

ExpressionType is an enum for the type of input expression.

const (
	// ExpressionTypeUnspecified is used when the expression type is not specified.
	ExpressionTypeUnspecified ExpressionType = iota
	// CompiledExpressionFile is file containing a checked expression.
	CompiledExpressionFile
	// PolicyFile is a file containing a CEL policy.
	PolicyFile
	// ExpressionFile is a file containing a CEL expression.
	ExpressionFile
	// RawExpressionString is a raw CEL expression string.
	RawExpressionString
)

type FileExpression

type FileExpression struct {
	Path string
}

FileExpression is an InputExpression which loads a CEL expression or policy from a file.

func (*FileExpression) CreateAST

func (f *FileExpression) CreateAST(compiler Compiler) (*cel.Ast, map[string]any, error)

CreateAST creates a CEL AST from a file using the provided compiler: - All policy metadata options are executed using the policy metadata map to extend the environment. - All policy compiler options are passed on to compile the parsed policy.

The file must be in one of the following formats: - .cel: CEL string expression - .celpolicy: CEL policy

type FileFormat

type FileFormat int

FileFormat represents the format of the file being loaded.

const (
	// Unspecified is used when the file format is not determined.
	Unspecified FileFormat = iota + 1
	// BinaryProto is used for a binary proto file.
	BinaryProto
	// TextProto is used for a text proto file.
	TextProto
	// TextYAML is used for a YAML file.
	TextYAML
	// CELString is used for a CEL string expression defined in a file with .cel extension.
	CELString
	// CELPolicy is used for a CEL policy file with .celpolicy extension.
	CELPolicy
)

func InferFileFormat

func InferFileFormat(path string) FileFormat

InferFileFormat infers the file format from the file path.

type InputExpression

type InputExpression interface {
	// CreateAST creates a CEL AST from the input expression using the provided compiler.
	CreateAST(Compiler) (*cel.Ast, map[string]any, error)
}

InputExpression is an interface for an expression which can be compiled into a CEL AST and return an optional policy metadata map.

type PolicyMetadataEnvOption

type PolicyMetadataEnvOption func(map[string]any) cel.EnvOption

PolicyMetadataEnvOption represents a function which accepts a policy metadata map and returns an environment option used to extend the CEL environment.

The policy metadata map is generally produced as a byproduct of parsing the policy and it can be optionally customised by providing a custom policy parser.

type RawExpression

type RawExpression struct {
	Value string
}

RawExpression is an InputExpression which loads a CEL expression from a string.

func (*RawExpression) CreateAST

func (r *RawExpression) CreateAST(compiler Compiler) (*cel.Ast, map[string]any, error)

CreateAST creates a CEL AST from a raw CEL expression using the provided compiler.