package decls

import "github.com/google/cel-go/checker/decls"

Package decls provides helpers for creating variable and function declarations.

Index

Variables

var (
	// Error type used to communicate issues during type-checking.
	Error = &exprpb.Type{
		TypeKind: &exprpb.Type_Error{
			Error: &emptypb.Empty{}}}

	// Dyn is a top-type used to represent any value.
	Dyn = &exprpb.Type{
		TypeKind: &exprpb.Type_Dyn{
			Dyn: &emptypb.Empty{}}}
)
var (
	Bool   = NewPrimitiveType(exprpb.Type_BOOL)
	Bytes  = NewPrimitiveType(exprpb.Type_BYTES)
	Double = NewPrimitiveType(exprpb.Type_DOUBLE)
	Int    = NewPrimitiveType(exprpb.Type_INT64)
	Null   = &exprpb.Type{
		TypeKind: &exprpb.Type_Null{
			Null: structpb.NullValue_NULL_VALUE}}
	String = NewPrimitiveType(exprpb.Type_STRING)
	Uint   = NewPrimitiveType(exprpb.Type_UINT64)
)

Commonly used types.

var (
	Any       = NewWellKnownType(exprpb.Type_ANY)
	Duration  = NewWellKnownType(exprpb.Type_DURATION)
	Timestamp = NewWellKnownType(exprpb.Type_TIMESTAMP)
)

Well-known types. TODO: Replace with an abstract type registry.

Functions

func NewAbstractType

func NewAbstractType(name string, paramTypes ...*exprpb.Type) *exprpb.Type

NewAbstractType creates an abstract type declaration which references a proto message name and may also include type parameters.

func NewConst

func NewConst(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl

NewConst creates a constant identifier with a CEL constant literal value.

func NewFunction

func NewFunction(name string,
	overloads ...*exprpb.Decl_FunctionDecl_Overload) *exprpb.Decl

NewFunction creates a named function declaration with one or more overloads.

func NewFunctionType

func NewFunctionType(resultType *exprpb.Type,
	argTypes ...*exprpb.Type) *exprpb.Type

NewFunctionType creates a function invocation contract, typically only used by type-checking steps after overload resolution.

func NewFunctionWithDoc

func NewFunctionWithDoc(name, doc string,
	overloads ...*exprpb.Decl_FunctionDecl_Overload) *exprpb.Decl

NewFunctionWithDoc creates a named function declaration with a description and one or more overloads.

func NewIdent

func NewIdent(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl

NewIdent creates a named identifier declaration with an optional literal value.

Literal values are typically only associated with enum identifiers.

Deprecated: Use NewVar or NewConst instead.

func NewInstanceOverload

func NewInstanceOverload(id string, argTypes []*exprpb.Type, resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload

NewInstanceOverload creates a instance function overload contract. First element of argTypes is instance.

func NewListType

func NewListType(elem *exprpb.Type) *exprpb.Type

NewListType generates a new list with elements of a certain type.

func NewMapType

func NewMapType(key *exprpb.Type, value *exprpb.Type) *exprpb.Type

NewMapType generates a new map with typed keys and values.

func NewObjectType

func NewObjectType(typeName string) *exprpb.Type

NewObjectType creates an object type for a qualified type name.

func NewOptionalType

func NewOptionalType(paramType *exprpb.Type) *exprpb.Type

NewOptionalType constructs an abstract type indicating that the parameterized type may be contained within the object.

func NewOverload

func NewOverload(id string, argTypes []*exprpb.Type, resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload

NewOverload creates a function overload declaration which contains a unique overload id as well as the expected argument and result types. Overloads must be aggregated within a Function declaration.

func NewParameterizedInstanceOverload

func NewParameterizedInstanceOverload(id string,
	argTypes []*exprpb.Type,
	resultType *exprpb.Type,
	typeParams []string) *exprpb.Decl_FunctionDecl_Overload

NewParameterizedInstanceOverload creates a parametric function instance overload type.

func NewParameterizedOverload

func NewParameterizedOverload(id string,
	argTypes []*exprpb.Type,
	resultType *exprpb.Type,
	typeParams []string) *exprpb.Decl_FunctionDecl_Overload

NewParameterizedOverload creates a parametric function overload type.

func NewPrimitiveType

func NewPrimitiveType(primitive exprpb.Type_PrimitiveType) *exprpb.Type

NewPrimitiveType creates a type for a primitive value. See the var declarations for Int, Uint, etc.

func NewTypeParamType

func NewTypeParamType(name string) *exprpb.Type

NewTypeParamType creates a type corresponding to a named, contextual parameter.

func NewTypeType

func NewTypeType(nested *exprpb.Type) *exprpb.Type

NewTypeType creates a new type designating a type.

func NewVar

func NewVar(name string, t *exprpb.Type) *exprpb.Decl

NewVar creates a variable identifier.

func NewVarWithDoc

func NewVarWithDoc(name string, t *exprpb.Type, desc string) *exprpb.Decl

NewVarWithDoc creates a variable identifier with a type and a description string.

func NewWellKnownType

func NewWellKnownType(wellKnown exprpb.Type_WellKnownType) *exprpb.Type

NewWellKnownType creates a type corresponding to a protobuf well-known type value.

func NewWrapperType

func NewWrapperType(wrapped *exprpb.Type) *exprpb.Type

NewWrapperType creates a wrapped primitive type instance. Wrapped types are roughly equivalent to a nullable, or optionally valued type.

Source Files

decls.go

Version
v0.25.0 (latest)
Published
Apr 22, 2025
Platform
linux/amd64
Imports
3 packages
Last checked
5 hours ago

Tools for package owners.