types

package
v0.0.0-...-50d4735 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0, BSD-3-Clause Imports: 29 Imported by: 0

Documentation

Overview

Package types contains the types, traits, and utilities common to all components of expression handling.

Index

Constants

View Source
const (
	False = Bool(false)
	True  = Bool(true)
)

Boolean constants

View Source
const (
	// IntZero is the zero-value for Int
	IntZero   = Int(0)
	IntOne    = Int(1)
	IntNegOne = Int(-1)
)

Int constants used for comparison results.

Variables

View Source
var (
	// OptionalType indicates the runtime type of an optional value.
	OptionalType = NewOpaqueType("optional_type")

	// OptionalNone is a sentinel value which is used to indicate an empty optional value.
	OptionalNone = &Optional{}
)
View Source
var (
	// AnyType represents the google.protobuf.Any type.
	AnyType = &Type{
		kind:            AnyKind,
		runtimeTypeName: "google.protobuf.Any",
		traitMask: traits.FieldTesterType |
			traits.IndexerType,
	}
	// BoolType represents the bool type.
	BoolType = &Type{
		kind:            BoolKind,
		runtimeTypeName: "bool",
		traitMask: traits.ComparerType |
			traits.NegatorType,
	}
	// BytesType represents the bytes type.
	BytesType = &Type{
		kind:            BytesKind,
		runtimeTypeName: "bytes",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.SizerType,
	}
	// DoubleType represents the double type.
	DoubleType = &Type{
		kind:            DoubleKind,
		runtimeTypeName: "double",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.DividerType |
			traits.MultiplierType |
			traits.NegatorType |
			traits.SubtractorType,
	}
	// DurationType represents the CEL duration type.
	DurationType = &Type{
		kind:            DurationKind,
		runtimeTypeName: "google.protobuf.Duration",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.NegatorType |
			traits.ReceiverType |
			traits.SubtractorType,
	}
	// DynType represents a dynamic CEL type whose type will be determined at runtime from context.
	DynType = &Type{
		kind:            DynKind,
		runtimeTypeName: "dyn",
	}
	// ErrorType represents a CEL error value.
	ErrorType = &Type{
		kind:            ErrorKind,
		runtimeTypeName: "error",
	}
	// IntType represents the int type.
	IntType = &Type{
		kind:            IntKind,
		runtimeTypeName: "int",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.DividerType |
			traits.ModderType |
			traits.MultiplierType |
			traits.NegatorType |
			traits.SubtractorType,
	}
	// ListType represents the runtime list type.
	ListType = NewListType(nil)
	// MapType represents the runtime map type.
	MapType = NewMapType(nil, nil)
	// NullType represents the type of a null value.
	NullType = &Type{
		kind:            NullTypeKind,
		runtimeTypeName: "null_type",
	}
	// StringType represents the string type.
	StringType = &Type{
		kind:            StringKind,
		runtimeTypeName: "string",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.MatcherType |
			traits.ReceiverType |
			traits.SizerType,
	}
	// TimestampType represents the time type.
	TimestampType = &Type{
		kind:            TimestampKind,
		runtimeTypeName: "google.protobuf.Timestamp",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.ReceiverType |
			traits.SubtractorType,
	}
	// TypeType represents a CEL type
	TypeType = &Type{
		kind:            TypeKind,
		runtimeTypeName: "type",
	}
	// UintType represents a uint type.
	UintType = &Type{
		kind:            UintKind,
		runtimeTypeName: "uint",
		traitMask: traits.AdderType |
			traits.ComparerType |
			traits.DividerType |
			traits.ModderType |
			traits.MultiplierType |
			traits.SubtractorType,
	}
	// UnknownType represents an unknown value type.
	UnknownType = &Type{
		kind:            UnknownKind,
		runtimeTypeName: "unknown",
	}
)
View Source
var DefaultTypeAdapter = &defaultTypeAdapter{}

DefaultTypeAdapter adapts canonical CEL types from their equivalent Go values.

View Source
var (
	// ErrType singleton.
	ErrType = NewOpaqueType("error")
)
View Source
var (
	// IteratorType singleton.
	IteratorType = NewObjectType("iterator", traits.IteratorType)
)
View Source
var (
	// NullValue singleton.
	NullValue = Null(structpb.NullValue_NULL_VALUE)
)

ProtoCELPrimitives provides a map from the protoreflect Kind to the equivalent CEL type.

Functions

func DurationGetHours

func DurationGetHours(val ref.Val) ref.Val

DurationGetHours returns the duration in hours.

func DurationGetMilliseconds

func DurationGetMilliseconds(val ref.Val) ref.Val

DurationGetMilliseconds returns duration in milliseconds.

func DurationGetMinutes

func DurationGetMinutes(val ref.Val) ref.Val

DurationGetMinutes returns duration in minutes.

func DurationGetSeconds

func DurationGetSeconds(val ref.Val) ref.Val

DurationGetSeconds returns duration in seconds.

func Equal

func Equal(lhs ref.Val, rhs ref.Val) ref.Val

Equal returns whether the two ref.Value are heterogeneously equivalent.

func IndexOrError

func IndexOrError(index ref.Val) (int, error)

IndexOrError converts an input index value into either a lossless integer index or an error.

func InsertMapKeyValue

func InsertMapKeyValue(m traits.Mapper, k, v ref.Val) ref.Val

InsertMapKeyValue inserts a key, value pair into the target map if the target map does not already contain the given key.

If the map is mutable, it is modified in-place per the MutableMapper contract. If the map is not mutable, a copy containing the new key, value pair is made.

func IsBool

func IsBool(elem ref.Val) bool

IsBool returns whether the input ref.Val or ref.Type is equal to BoolType.

func IsError

func IsError(val ref.Val) bool

IsError returns whether the input element ref.Type or ref.Val is equal to the ErrType singleton.

func IsPrimitiveType

func IsPrimitiveType(val ref.Val) bool

IsPrimitiveType returns whether the input element ref.Val is a primitive type. Note, primitive types do not include well-known types such as Duration and Timestamp.

func IsUnknown

func IsUnknown(val ref.Val) bool

IsUnknown returns whether the element ref.Val is in instance of *types.Unknown

func IsUnknownOrError

func IsUnknownOrError(val ref.Val) bool

IsUnknownOrError returns whether the input element ref.Val is an ErrType or UnknownType.

func LabelErrNode

func LabelErrNode(id int64, val ref.Val) ref.Val

LabelErrNode returns val unaltered it is not an Err or if the error has a non-zero AST node ID already present. Otherwise the id is added to the error for recovery with the Err.NodeID method.

func MaybeNoSuchOverloadErr

func MaybeNoSuchOverloadErr(val ref.Val) ref.Val

MaybeNoSuchOverloadErr returns the error or unknown if the input ref.Val is one of these types, else a new no such overload error.

func NewDynamicList

func NewDynamicList(adapter Adapter, value any) traits.Lister

NewDynamicList returns a traits.Lister with heterogenous elements. value should be an array of "native" types, i.e. any type that NativeToValue() can convert to a ref.Val.

func NewDynamicMap

func NewDynamicMap(adapter Adapter, value any) traits.Mapper

NewDynamicMap returns a traits.Mapper value with dynamic key, value pairs.

func NewErr

func NewErr(format string, args ...any) ref.Val

NewErr creates a new Err described by the format string and args. TODO: Audit the use of this function and standardize the error messages and codes.

func NewErrWithNodeID

func NewErrWithNodeID(id int64, format string, args ...any) ref.Val

NewErrWithNodeID creates a new Err described by the format string and args. TODO: Audit the use of this function and standardize the error messages and codes.

func NewJSONList

func NewJSONList(adapter Adapter, l *structpb.ListValue) traits.Lister

NewJSONList returns a traits.Lister based on structpb.ListValue instance.

func NewJSONStruct

func NewJSONStruct(adapter Adapter, value *structpb.Struct) traits.Mapper

NewJSONStruct creates a traits.Mapper implementation backed by a JSON struct that has been encoded in protocol buffer form.

The `adapter` argument provides type adaptation capabilities from proto to CEL.

func NewMutableList

func NewMutableList(adapter Adapter) traits.MutableLister

NewMutableList creates a new mutable list whose internal state can be modified.

func NewMutableMap

func NewMutableMap(adapter Adapter, mutableValues map[ref.Val]ref.Val) traits.MutableMapper

NewMutableMap constructs a mutable map from an adapter and a set of map values.

func NewObject

func NewObject(adapter Adapter,
	typeDesc *pb.TypeDescription,
	typeValue ref.Val,
	value proto.Message,
) ref.Val

NewObject returns an object based on a proto.Message value which handles conversion between protobuf type values and expression type values. Objects support indexing and iteration.

Note: the type value is pulled from the list of registered types within the type provider. If the proto type is not registered within the type provider, then this will result in an error within the type adapter / provider.

func NewProtoList

func NewProtoList(adapter Adapter, list protoreflect.List) traits.Lister

NewProtoList returns a traits.Lister based on a pb.List instance.

func NewProtoMap

func NewProtoMap(adapter Adapter, value *pb.Map) traits.Mapper

NewProtoMap returns a specialized traits.Mapper for handling protobuf map values.

func NewRefValList

func NewRefValList(adapter Adapter, elems []ref.Val) traits.Lister

NewRefValList returns a traits.Lister with ref.Val elements.

This type specialization is used with list literals within CEL expressions.

func NewRefValMap

func NewRefValMap(adapter Adapter, value map[ref.Val]ref.Val) traits.Mapper

NewRefValMap returns a specialized traits.Mapper with CEL valued keys and values.

func NewStringInterfaceMap

func NewStringInterfaceMap(adapter Adapter, value map[string]any) traits.Mapper

NewStringInterfaceMap returns a specialized traits.Mapper with string keys and interface values.

func NewStringList

func NewStringList(adapter Adapter, elems []string) traits.Lister

NewStringList returns a traits.Lister containing only strings.

func NewStringStringMap

func NewStringStringMap(adapter Adapter, value map[string]string) traits.Mapper

NewStringStringMap returns a specialized traits.Mapper with string keys and values.

func NoSuchOverloadErr

func NoSuchOverloadErr() ref.Val

NoSuchOverloadErr returns a new types.Err instance with a no such overload message.

func StringContains

func StringContains(s, sub ref.Val) ref.Val

StringContains returns whether the string contains a substring.

func StringEndsWith

func StringEndsWith(s, suf ref.Val) ref.Val

StringEndsWith returns whether the target string contains the input suffix.

func StringStartsWith

func StringStartsWith(s, pre ref.Val) ref.Val

StringStartsWith returns whether the target string contains the input prefix.

func ToFoldableList

func ToFoldableList(l traits.Lister) traits.Foldable

ToFoldableList will create a Foldable version of a list suitable for key-value pair iteration.

For values which are already Foldable, this call is a no-op. For all other values, the fold is driven via the Size() and Get() calls which means that the folding will function, but take a performance hit.

func ToFoldableMap

func ToFoldableMap(m traits.Mapper) traits.Foldable

ToFoldableMap will create a Foldable version of a map suitable for key-value pair iteration.

For values which are already Foldable, this call is a no-op. For all other values, the fold is driven via the Iterator HasNext() and Next() calls as well as the map's Get() method which means that the folding will function, but take a performance hit.

func TypeToExprType

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

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

func UnsupportedRefValConversionErr

func UnsupportedRefValConversionErr(val any) ref.Val

UnsupportedRefValConversionErr returns a types.NewErr instance with a no such conversion message that indicates that the native value could not be converted to a CEL ref.Val.

func ValOrErr

func ValOrErr(val ref.Val, format string, args ...any) ref.Val

ValOrErr either returns the existing error or creates a new one. TODO: Audit the use of this function and standardize the error messages and codes.

func WrapErr

func WrapErr(err error) ref.Val

WrapErr wraps an existing Go error value into a CEL Err value.

Types

type Adapter

type Adapter = ref.TypeAdapter

Adapter converts native Go values of varying type and complexity to equivalent CEL values.

type AttributeQualifier

type AttributeQualifier interface {
	bool | int64 | uint64 | string
}

AttributeQualifier constrains the possible types which may be used to qualify an attribute.

type AttributeTrail

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

AttributeTrail specifies a variable with an optional qualifier path. An attribute value is expected to correspond to an AbsoluteAttribute, meaning a field selection which starts with a top-level variable.

The qualifer path elements adhere to the AttributeQualifier type constraint.

func NewAttributeTrail

func NewAttributeTrail(variable string) *AttributeTrail

NewAttributeTrail creates a new simple attribute from a variable name.

func QualifyAttribute

func QualifyAttribute[T AttributeQualifier](attr *AttributeTrail, qualifier T) *AttributeTrail

QualifyAttribute qualifies an attribute using a valid AttributeQualifier type.

func (*AttributeTrail) Equal

func (a *AttributeTrail) Equal(other *AttributeTrail) bool

Equal returns whether two attribute values have the same variable name and qualifier paths.

func (*AttributeTrail) QualifierPath

func (a *AttributeTrail) QualifierPath() []any

QualifierPath returns the optional set of qualifying fields or indices applied to the variable.

func (*AttributeTrail) String

func (a *AttributeTrail) String() string

String returns the string representation of the Attribute.

func (*AttributeTrail) Variable

func (a *AttributeTrail) Variable() string

Variable returns the variable name associated with the attribute.

type Bool

type Bool bool

Bool type that implements ref.Val and supports comparison and negation.

func (Bool) Compare

func (b Bool) Compare(other ref.Val) ref.Val

Compare implements the traits.Comparer interface method.

func (Bool) ConvertToNative

func (b Bool) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements the ref.Val interface method.

func (Bool) ConvertToType

func (b Bool) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements the ref.Val interface method.

func (Bool) Equal

func (b Bool) Equal(other ref.Val) ref.Val

Equal implements the ref.Val interface method.

func (Bool) IsZeroValue

func (b Bool) IsZeroValue() bool

IsZeroValue returns true if the boolean value is false.

func (Bool) Negate

func (b Bool) Negate() ref.Val

Negate implements the traits.Negater interface method.

func (Bool) Type

func (b Bool) Type() ref.Type

Type implements the ref.Val interface method.

func (Bool) Value

func (b Bool) Value() any

Value implements the ref.Val interface method.

type Bytes

type Bytes []byte

Bytes type that implements ref.Val and supports add, compare, and size operations.

func (Bytes) Add

func (b Bytes) Add(other ref.Val) ref.Val

Add implements traits.Adder interface method by concatenating byte sequences.

func (Bytes) Compare

func (b Bytes) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer interface method by lexicographic ordering.

func (Bytes) ConvertToNative

func (b Bytes) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements the ref.Val interface method.

func (Bytes) ConvertToType

func (b Bytes) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements the ref.Val interface method.

func (Bytes) Equal

func (b Bytes) Equal(other ref.Val) ref.Val

Equal implements the ref.Val interface method.

func (Bytes) IsZeroValue

func (b Bytes) IsZeroValue() bool

IsZeroValue returns true if the byte array is empty.

func (Bytes) Size

func (b Bytes) Size() ref.Val

Size implements the traits.Sizer interface method.

func (Bytes) Type

func (b Bytes) Type() ref.Type

Type implements the ref.Val interface method.

func (Bytes) Value

func (b Bytes) Value() any

Value implements the ref.Val interface method.

type Double

type Double float64

Double type that implements ref.Val, comparison, and mathematical operations.

func (Double) Add

func (d Double) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (Double) Compare

func (d Double) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (Double) ConvertToNative

func (d Double) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Double) ConvertToType

func (d Double) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Double) Divide

func (d Double) Divide(other ref.Val) ref.Val

Divide implements traits.Divider.Divide.

func (Double) Equal

func (d Double) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Double) IsZeroValue

func (d Double) IsZeroValue() bool

IsZeroValue returns true if double value is 0.0

func (Double) Multiply

func (d Double) Multiply(other ref.Val) ref.Val

Multiply implements traits.Multiplier.Multiply.

func (Double) Negate

func (d Double) Negate() ref.Val

Negate implements traits.Negater.Negate.

func (Double) Subtract

func (d Double) Subtract(subtrahend ref.Val) ref.Val

Subtract implements traits.Subtractor.Subtract.

func (Double) Type

func (d Double) Type() ref.Type

Type implements ref.Val.Type.

func (Double) Value

func (d Double) Value() any

Value implements ref.Val.Value.

type Duration

type Duration struct {
	time.Duration
}

Duration type that implements ref.Val and supports add, compare, negate, and subtract operators. This type is also a receiver which means it can participate in dispatch to receiver functions.

func (Duration) Add

func (d Duration) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (Duration) Compare

func (d Duration) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (Duration) ConvertToNative

func (d Duration) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Duration) ConvertToType

func (d Duration) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Duration) Equal

func (d Duration) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Duration) IsZeroValue

func (d Duration) IsZeroValue() bool

IsZeroValue returns true if the duration value is zero

func (Duration) Negate

func (d Duration) Negate() ref.Val

Negate implements traits.Negater.Negate.

func (Duration) Receive

func (d Duration) Receive(function string, overload string, args []ref.Val) ref.Val

Receive implements traits.Receiver.Receive.

func (Duration) Subtract

func (d Duration) Subtract(subtrahend ref.Val) ref.Val

Subtract implements traits.Subtractor.Subtract.

func (Duration) Type

func (d Duration) Type() ref.Type

Type implements ref.Val.Type.

func (Duration) Value

func (d Duration) Value() any

Value implements ref.Val.Value.

type Err

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

Err type which extends the built-in go error and implements ref.Val.

func (*Err) ConvertToNative

func (e *Err) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (*Err) ConvertToType

func (e *Err) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (*Err) Equal

func (e *Err) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (*Err) Is

func (e *Err) Is(target error) bool

Is implements errors.Is.

func (*Err) NodeID

func (e *Err) NodeID() int64

NodeID returns the AST node ID of the expression that returned the error.

func (*Err) String

func (e *Err) String() string

String implements fmt.Stringer.

func (*Err) Type

func (e *Err) Type() ref.Type

Type implements ref.Val.Type.

func (*Err) Unwrap

func (e *Err) Unwrap() error

Unwrap implements errors.Unwrap.

func (*Err) Value

func (e *Err) Value() any

Value implements ref.Val.Value.

type Error

type Error interface {
	error
	ref.Val
}

Error interface which allows types types.Err values to be treated as error values.

type FieldType

type FieldType struct {
	// Type of the field as a CEL native type value.
	Type *Type

	// IsSet indicates whether the field is set on an input object.
	IsSet ref.FieldTester

	// GetFrom retrieves the field value on the input object, if set.
	GetFrom ref.FieldGetter
}

FieldType represents a field's type value and whether that field supports presence detection.

type Int

type Int int64

Int type that implements ref.Val as well as comparison and math operators.

func (Int) Add

func (i Int) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (Int) Compare

func (i Int) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (Int) ConvertToNative

func (i Int) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Int) ConvertToType

func (i Int) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Int) Divide

func (i Int) Divide(other ref.Val) ref.Val

Divide implements traits.Divider.Divide.

func (Int) Equal

func (i Int) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Int) IsZeroValue

func (i Int) IsZeroValue() bool

IsZeroValue returns true if integer is equal to 0

func (Int) Modulo

func (i Int) Modulo(other ref.Val) ref.Val

Modulo implements traits.Modder.Modulo.

func (Int) Multiply

func (i Int) Multiply(other ref.Val) ref.Val

Multiply implements traits.Multiplier.Multiply.

func (Int) Negate

func (i Int) Negate() ref.Val

Negate implements traits.Negater.Negate.

func (Int) Subtract

func (i Int) Subtract(subtrahend ref.Val) ref.Val

Subtract implements traits.Subtractor.Subtract.

func (Int) Type

func (i Int) Type() ref.Type

Type implements ref.Val.Type.

func (Int) Value

func (i Int) Value() any

Value implements ref.Val.Value.

type Kind

type Kind uint

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

const (
	// UnspecifiedKind is returned when the type is nil or its kind is not specified.
	UnspecifiedKind Kind = iota

	// DynKind represents a dynamic type. This kind only exists at type-check time.
	DynKind

	// AnyKind represents a google.protobuf.Any type. This kind only exists at type-check time.
	// Prefer DynKind to AnyKind as AnyKind has a specific meaning which is based on protobuf
	// well-known types.
	AnyKind

	// BoolKind represents a boolean type.
	BoolKind

	// BytesKind represents a bytes type.
	BytesKind

	// DoubleKind represents a double type.
	DoubleKind

	// DurationKind represents a CEL duration type.
	DurationKind

	// ErrorKind represents a CEL error type.
	ErrorKind

	// IntKind represents an integer type.
	IntKind

	// ListKind represents a list type.
	ListKind

	// MapKind represents a map type.
	MapKind

	// NullTypeKind represents a null type.
	NullTypeKind

	// OpaqueKind represents an abstract type which has no accessible fields.
	OpaqueKind

	// StringKind represents a string type.
	StringKind

	// StructKind represents a structured object with typed fields.
	StructKind

	// TimestampKind represents a a CEL time type.
	TimestampKind

	// TypeKind represents the CEL type.
	TypeKind

	// TypeParamKind represents a parameterized type whose type name will be resolved at type-check time, if possible.
	TypeParamKind

	// UintKind represents a uint type.
	UintKind

	// UnknownKind represents an unknown value type.
	UnknownKind
)

type Null

type Null structpb.NullValue

Null type implementation.

func (Null) ConvertToNative

func (n Null) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Null) ConvertToType

func (n Null) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Null) Equal

func (n Null) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Null) IsZeroValue

func (n Null) IsZeroValue() bool

IsZeroValue returns true as null always represents an absent value.

func (Null) Type

func (n Null) Type() ref.Type

Type implements ref.Val.Type.

func (Null) Value

func (n Null) Value() any

Value implements ref.Val.Value.

type Optional

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

Optional value which points to a value if non-empty.

func OptionalOf

func OptionalOf(value ref.Val) *Optional

OptionalOf returns an optional value which wraps a concrete CEL value.

func (*Optional) ConvertToNative

func (o *Optional) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements the ref.Val interface method.

func (*Optional) ConvertToType

func (o *Optional) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements the ref.Val interface method.

func (*Optional) Equal

func (o *Optional) Equal(other ref.Val) ref.Val

Equal determines whether the values contained by two optional values are equal.

func (*Optional) GetValue

func (o *Optional) GetValue() ref.Val

GetValue returns the wrapped value contained in the optional.

func (*Optional) HasValue

func (o *Optional) HasValue() bool

HasValue returns true if the optional has a value.

func (*Optional) String

func (o *Optional) String() string

func (*Optional) Type

func (o *Optional) Type() ref.Type

Type implements the ref.Val interface method.

func (*Optional) Value

func (o *Optional) Value() any

Value returns the underlying 'Value()' of the wrapped value, if present.

type Provider

type Provider interface {
	// EnumValue returns the numeric value of the given enum value name.
	EnumValue(enumName string) ref.Val

	// FindIdent takes a qualified identifier name and returns a ref.Val if one exists.
	FindIdent(identName string) (ref.Val, bool)

	// FindStructType returns the Type give a qualified type name.
	//
	// For historical reasons, only struct types are expected to be returned through this
	// method, and the type values are expected to be wrapped in a TypeType instance using
	// TypeTypeWithParam(<structType>).
	//
	// Returns false if not found.
	FindStructType(structType string) (*Type, bool)

	// FindStructFieldNames returns thet field names associated with the type, if the type
	// is found.
	FindStructFieldNames(structType string) ([]string, bool)

	// FieldStructFieldType returns the field type for a checked type value. Returns
	// false if the field could not be found.
	FindStructFieldType(structType, fieldName string) (*FieldType, bool)

	// NewValue creates a new type value from a qualified name and map of field
	// name to value.
	//
	// Note, for each value, the Val.ConvertToNative function will be invoked
	// to convert the Val to the field's native type. If an error occurs during
	// conversion, the NewValue will be a types.Err.
	NewValue(structType string, fields map[string]ref.Val) ref.Val
}

Provider specifies functions for creating new object instances and for resolving enum values by name.

type Registry

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

Registry provides type information for a set of registered types.

func NewEmptyRegistry

func NewEmptyRegistry() *Registry

NewEmptyRegistry returns a registry which is completely unconfigured.

func NewRegistry

func NewRegistry(types ...proto.Message) (*Registry, error)

NewRegistry accepts a list of proto message instances and returns a type provider which can create new instances of the provided message or any message that proto depends upon in its FileDescriptor.

func (*Registry) Copy

func (p *Registry) Copy() *Registry

Copy copies the current state of the registry into its own memory space.

func (*Registry) EnumValue

func (p *Registry) EnumValue(enumName string) ref.Val

EnumValue returns the numeric value of the given enum value name.

func (*Registry) FindFieldType deprecated

func (p *Registry) FindFieldType(structType, fieldName string) (*ref.FieldType, bool)

FindFieldType returns the field type for a checked type value. Returns false if the field could not be found.

Deprecated: use FindStructFieldType

func (*Registry) FindIdent

func (p *Registry) FindIdent(identName string) (ref.Val, bool)

FindIdent takes a qualified identifier name and returns a ref.Val if one exists.

func (*Registry) FindStructFieldNames

func (p *Registry) FindStructFieldNames(structType string) ([]string, bool)

FindStructFieldNames returns the set of field names for the given struct type, if the type exists in the registry.

func (*Registry) FindStructFieldType

func (p *Registry) FindStructFieldType(structType, fieldName string) (*FieldType, bool)

FindStructFieldType returns the field type for a checked type value. Returns false if the field could not be found.

func (*Registry) FindStructType

func (p *Registry) FindStructType(structType string) (*Type, bool)

FindStructType returns the Type give a qualified type name.

For historical reasons, only struct types are expected to be returned through this method, and the type values are expected to be wrapped in a TypeType instance using TypeTypeWithParam(<structType>).

Returns false if not found.

func (*Registry) FindType deprecated

func (p *Registry) FindType(structType string) (*exprpb.Type, bool)

FindType looks up the Type given a qualified typeName. Returns false if not found.

Deprecated: use FindStructType

func (*Registry) NativeToValue

func (p *Registry) NativeToValue(value any) ref.Val

NativeToValue converts various "native" types to ref.Val with this specific implementation providing support for custom proto-based types.

This method should be the inverse of ref.Val.ConvertToNative.

func (*Registry) NewValue

func (p *Registry) NewValue(structType string, fields map[string]ref.Val) ref.Val

NewValue creates a new type value from a qualified name and map of field name to value.

Note, for each value, the Val.ConvertToNative function will be invoked to convert the Val to the field's native type. If an error occurs during conversion, the NewValue will be a types.Err.

func (*Registry) RegisterDescriptor

func (p *Registry) RegisterDescriptor(fileDesc protoreflect.FileDescriptor) error

RegisterDescriptor registers the contents of a protocol buffer `FileDescriptor`.

func (*Registry) RegisterMessage

func (p *Registry) RegisterMessage(message proto.Message) error

RegisterMessage registers a protocol buffer message and its dependencies.

func (*Registry) RegisterType

func (p *Registry) RegisterType(types ...ref.Type) error

RegisterType registers a type value with the provider which ensures the provider is aware of how to map the type to an identifier.

If the `ref.Type` value is a `*types.Type` it will be registered directly by its runtime type name. If the `ref.Type` value is not a `*types.Type` instance, a `*types.Type` instance which reflects the traits present on the input and the runtime type name. By default this foreign type will be treated as a types.StructKind. To avoid potential issues where the `ref.Type` values does not match the generated `*types.Type` instance, consider always using the `*types.Type` to represent type extensions to CEL, even when they're not based on protobuf types.

type String

type String string

String type implementation which supports addition, comparison, matching, and size functions.

func (String) Add

func (s String) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (String) Compare

func (s String) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (String) ConvertToNative

func (s String) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (String) ConvertToType

func (s String) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (String) Equal

func (s String) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (String) IsZeroValue

func (s String) IsZeroValue() bool

IsZeroValue returns true if the string is empty.

func (String) Match

func (s String) Match(pattern ref.Val) ref.Val

Match implements traits.Matcher.Match.

func (String) Receive

func (s String) Receive(function string, overload string, args []ref.Val) ref.Val

Receive implements traits.Receiver.Receive.

func (String) Size

func (s String) Size() ref.Val

Size implements traits.Sizer.Size.

func (String) Type

func (s String) Type() ref.Type

Type implements ref.Val.Type.

func (String) Value

func (s String) Value() any

Value implements ref.Val.Value.

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp type implementation which supports add, compare, and subtract operations. Timestamps are also capable of participating in dynamic function dispatch to instance methods.

func (Timestamp) Add

func (t Timestamp) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (Timestamp) Compare

func (t Timestamp) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (Timestamp) ConvertToNative

func (t Timestamp) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Timestamp) ConvertToType

func (t Timestamp) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Timestamp) Equal

func (t Timestamp) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Timestamp) IsZeroValue

func (t Timestamp) IsZeroValue() bool

IsZeroValue returns true if the timestamp is epoch 0.

func (Timestamp) Receive

func (t Timestamp) Receive(function string, overload string, args []ref.Val) ref.Val

Receive implements traits.Receiver.Receive.

func (Timestamp) Subtract

func (t Timestamp) Subtract(subtrahend ref.Val) ref.Val

Subtract implements traits.Subtractor.Subtract.

func (Timestamp) Type

func (t Timestamp) Type() ref.Type

Type implements ref.Val.Type.

func (Timestamp) Value

func (t Timestamp) Value() any

Value implements ref.Val.Value.

type Type

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

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

func AlphaProtoAsType

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

AlphaProtoAsType converts a CEL v1alpha1.Type protobuf type to a CEL-native type representation.

func ExprTypeToType

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

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

func NewListType

func NewListType(elemType *Type) *Type

NewListType creates an instances of a list type value with the provided element type.

func NewMapType

func NewMapType(keyType, valueType *Type) *Type

NewMapType creates an instance of a map type value with the provided key and value types.

func NewNullableType

func NewNullableType(wrapped *Type) *Type

NewNullableType creates an instance of a nullable type with the provided wrapped type.

Note: only primitive types are supported as wrapped types.

func NewObjectType

func NewObjectType(typeName string, traits ...int) *Type

NewObjectType creates a type reference to an externally defined type, e.g. a protobuf message type.

An object type is assumed to support field presence testing and field indexing. Additionally, the type may also indicate additional traits through the use of the optional traits vararg argument.

func NewObjectTypeValue deprecated

func NewObjectTypeValue(typeName string) *Type

NewObjectTypeValue creates a type reference to an externally defined type.

Deprecated: use cel.ObjectType(typeName)

func NewOpaqueType

func NewOpaqueType(name string, params ...*Type) *Type

NewOpaqueType creates an abstract parameterized type with a given name.

func NewOptionalType

func NewOptionalType(param *Type) *Type

NewOptionalType creates an abstract parameterized type instance corresponding to CEL's notion of optional.

func NewTypeParamType

func NewTypeParamType(paramName string) *Type

NewTypeParamType creates a parameterized type instance.

func NewTypeTypeWithParam

func NewTypeTypeWithParam(param *Type) *Type

NewTypeTypeWithParam creates a type with a type parameter. Used for type-checking purposes, but equivalent to TypeType otherwise.

func NewTypeValue deprecated

func NewTypeValue(typeName string, traits ...int) *Type

NewTypeValue creates an opaque type which has a set of optional type traits as defined in the common/types/traits package.

Deprecated: use cel.ObjectType(typeName, traits)

func ProtoAsType

func ProtoAsType(t *celpb.Type) (*Type, error)

ProtoAsType converts a canonical CEL celpb.Type protobuf type to a CEL-native type representation.

func (*Type) ConvertToNative

func (t *Type) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (*Type) ConvertToType

func (t *Type) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (*Type) DeclaredTypeName

func (t *Type) DeclaredTypeName() string

DeclaredTypeName indicates the fully qualified and parameterized type-check type name.

func (*Type) Equal

func (t *Type) Equal(other ref.Val) ref.Val

Equal indicates whether two types have the same runtime type name.

The name Equal is a bit of a misnomer, but for historical reasons, this is the runtime behavior. For a more accurate definition see IsType().

func (*Type) HasTrait

func (t *Type) HasTrait(trait int) bool

HasTrait implements the ref.Type interface method.

func (*Type) IsAssignableRuntimeType

func (t *Type) IsAssignableRuntimeType(val ref.Val) bool

IsAssignableRuntimeType determines whether the current type is runtime assignable from the input runtimeType.

At runtime, parameterized types are erased and so a function which type-checks to support a map(string, string) will have a runtime assignable type of a map.

func (*Type) IsAssignableType

func (t *Type) IsAssignableType(fromType *Type) bool

IsAssignableType determines whether the current type is type-check assignable from the input fromType.

func (*Type) IsEquivalentType

func (t *Type) IsEquivalentType(other *Type) bool

IsEquivalentType indicates whether two types are equivalent. This check ignores type parameter type names.

func (*Type) IsExactType

func (t *Type) IsExactType(other *Type) bool

IsExactType indicates whether the two types are exactly the same. This check also verifies type parameter type names.

func (*Type) Kind

func (t *Type) Kind() Kind

Kind indicates general category of the type.

func (*Type) Parameters

func (t *Type) Parameters() []*Type

Parameters returns the list of type parameters if set.

For ListKind, Parameters()[0] represents the list element type For MapKind, Parameters()[0] represents the map key type, and Parameters()[1] represents the map value type.

func (*Type) String

func (t *Type) String() string

String returns a human-readable definition of the type name.

func (*Type) Type

func (t *Type) Type() ref.Type

Type implements the ref.Val interface method.

func (*Type) TypeName

func (t *Type) TypeName() string

TypeName returns the type-erased fully qualified runtime type name.

TypeName implements the ref.Type interface method.

func (*Type) Value

func (t *Type) Value() any

Value implements the ref.Val interface method.

func (*Type) WithTraits

func (t *Type) WithTraits(traits int) *Type

WithTraits creates a copy of the current Type and sets the trait mask to the traits parameter.

This method should be used with Opaque types where the type acts like a container, e.g. vector.

type Uint

type Uint uint64

Uint type implementation which supports comparison and math operators.

func (Uint) Add

func (i Uint) Add(other ref.Val) ref.Val

Add implements traits.Adder.Add.

func (Uint) Compare

func (i Uint) Compare(other ref.Val) ref.Val

Compare implements traits.Comparer.Compare.

func (Uint) ConvertToNative

func (i Uint) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Uint) ConvertToType

func (i Uint) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Uint) Divide

func (i Uint) Divide(other ref.Val) ref.Val

Divide implements traits.Divider.Divide.

func (Uint) Equal

func (i Uint) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Uint) IsZeroValue

func (i Uint) IsZeroValue() bool

IsZeroValue returns true if the uint is zero.

func (Uint) Modulo

func (i Uint) Modulo(other ref.Val) ref.Val

Modulo implements traits.Modder.Modulo.

func (Uint) Multiply

func (i Uint) Multiply(other ref.Val) ref.Val

Multiply implements traits.Multiplier.Multiply.

func (Uint) Subtract

func (i Uint) Subtract(subtrahend ref.Val) ref.Val

Subtract implements traits.Subtractor.Subtract.

func (Uint) Type

func (i Uint) Type() ref.Type

Type implements ref.Val.Type.

func (Uint) Value

func (i Uint) Value() any

Value implements ref.Val.Value.

type Unknown

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

Unknown type which collects expression ids which caused the current value to become unknown.

func MaybeMergeUnknowns

func MaybeMergeUnknowns(val ref.Val, unk *Unknown) (*Unknown, bool)

MaybeMergeUnknowns determines whether an input value and another, possibly nil, unknown will produce an unknown result.

If the input `val` is another Unknown, then the result will be the merge of the `val` and the input `unk`. If the `val` is not unknown, then the result will depend on whether the input `unk` is nil. If both values are non-nil and unknown, then the return value will be a merge of both unknowns.

func MergeUnknowns

func MergeUnknowns(unk1, unk2 *Unknown) *Unknown

MergeUnknowns combines two unknown values into a new unknown value.

func NewUnknown

func NewUnknown(id int64, attr *AttributeTrail) *Unknown

NewUnknown creates a new unknown at a given expression id for an attribute.

If the attribute is nil, the attribute value will be the `unspecifiedAttribute`.

func (*Unknown) Contains

func (u *Unknown) Contains(other *Unknown) bool

Contains returns true if the input unknown is a subset of the current unknown.

func (*Unknown) ConvertToNative

func (u *Unknown) ConvertToNative(typeDesc reflect.Type) (any, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (*Unknown) ConvertToType

func (u *Unknown) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType is an identity function since unknown values cannot be modified.

func (*Unknown) Equal

func (u *Unknown) Equal(other ref.Val) ref.Val

Equal is an identity function since unknown values cannot be modified.

func (*Unknown) GetAttributeTrails

func (u *Unknown) GetAttributeTrails(id int64) ([]*AttributeTrail, bool)

GetAttributeTrails returns the attribute trails, if present, missing for a given expression id.

func (*Unknown) IDs

func (u *Unknown) IDs() []int64

IDs returns the set of unknown expression ids contained by this value.

Numeric identifiers are guaranteed to be in sorted order.

func (*Unknown) String

func (u *Unknown) String() string

String implements the Stringer interface

func (*Unknown) Type

func (u *Unknown) Type() ref.Type

Type implements ref.Val.Type.

func (*Unknown) Value

func (u *Unknown) Value() any

Value implements ref.Val.Value.

Directories

Path Synopsis
Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.
Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.
Package ref contains the reference interfaces used throughout the types components.
Package ref contains the reference interfaces used throughout the types components.
Package traits defines interfaces that a type may implement to participate in operator overloads and function dispatch.
Package traits defines interfaces that a type may implement to participate in operator overloads and function dispatch.

Jump to

Keyboard shortcuts

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