cel

package
v0.28.2 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: Apache-2.0 Imports: 17 Imported by: 9

Documentation

Index

Constants

View Source
const (
	// DefaultMaxRequestSizeBytes is the size of the largest request that will be accepted
	DefaultMaxRequestSizeBytes = celconfig.MaxRequestSizeBytes

	// MaxDurationSizeJSON
	// OpenAPI duration strings follow RFC 3339, section 5.6 - see the comment on maxDatetimeSizeJSON
	MaxDurationSizeJSON = 32
	// MaxDatetimeSizeJSON
	// OpenAPI datetime strings follow RFC 3339, section 5.6, and the longest possible
	// such string is 9999-12-31T23:59:59.999999999Z, which has length 30 - we add 2
	// to allow for quotation marks
	MaxDatetimeSizeJSON = 32
	// MinDurationSizeJSON
	// Golang allows a string of 0 to be parsed as a duration, so that plus 2 to account for
	// quotation marks makes 3
	MinDurationSizeJSON = 3
	// JSONDateSize is the size of a date serialized as part of a JSON object
	// RFC 3339 dates require YYYY-MM-DD, and then we add 2 to allow for quotation marks
	JSONDateSize = 12
	// MinDatetimeSizeJSON is the minimal length of a datetime formatted as RFC 3339
	// RFC 3339 datetimes require a full date (YYYY-MM-DD) and full time (HH:MM:SS), and we add 3 for
	// quotation marks like always in addition to the capital T that separates the date and time
	MinDatetimeSizeJSON = 21
	// MinStringSize is the size of literal ""
	MinStringSize = 2
	// MinBoolSize is the length of literal true
	MinBoolSize = 4
	// MinNumberSize is the length of literal 0
	MinNumberSize = 1
)

Variables

View Source
var (
	QuantityObject = decls.NewObjectType("kubernetes.Quantity")

	QuantityType = cel.ObjectType("kubernetes.Quantity")
)
View Source
var (
	// AnyType is equivalent to the CEL 'protobuf.Any' type in that the value may have any of the
	// types supported.
	AnyType = NewSimpleTypeWithMinSize("any", cel.AnyType, nil, 1)

	// BoolType is equivalent to the CEL 'bool' type.
	BoolType = NewSimpleTypeWithMinSize("bool", cel.BoolType, types.False, MinBoolSize)

	// BytesType is equivalent to the CEL 'bytes' type.
	BytesType = NewSimpleTypeWithMinSize("bytes", cel.BytesType, types.Bytes([]byte{}), MinStringSize)

	// DoubleType is equivalent to the CEL 'double' type which is a 64-bit floating point value.
	DoubleType = NewSimpleTypeWithMinSize("double", cel.DoubleType, types.Double(0), MinNumberSize)

	// DurationType is equivalent to the CEL 'duration' type.
	DurationType = NewSimpleTypeWithMinSize("duration", cel.DurationType, types.Duration{Duration: time.Duration(0)}, MinDurationSizeJSON)

	// DateType is equivalent to the CEL 'date' type.
	DateType = NewSimpleTypeWithMinSize("date", cel.TimestampType, types.Timestamp{Time: time.Time{}}, JSONDateSize)

	// DynType is the equivalent of the CEL 'dyn' concept which indicates that the type will be
	// determined at runtime rather than compile time.
	DynType = NewSimpleTypeWithMinSize("dyn", cel.DynType, nil, 1)

	// IntType is equivalent to the CEL 'int' type which is a 64-bit signed int.
	IntType = NewSimpleTypeWithMinSize("int", cel.IntType, types.IntZero, MinNumberSize)

	// NullType is equivalent to the CEL 'null_type'.
	NullType = NewSimpleTypeWithMinSize("null_type", cel.NullType, types.NullValue, 4)

	// StringType is equivalent to the CEL 'string' type which is expected to be a UTF-8 string.
	// StringType values may either be string literals or expression strings.
	StringType = NewSimpleTypeWithMinSize("string", cel.StringType, types.String(""), MinStringSize)

	// TimestampType corresponds to the well-known protobuf.Timestamp type supported within CEL.
	// Note that both the OpenAPI date and date-time types map onto TimestampType, so not all types
	// labeled as Timestamp will necessarily have the same MinSerializedSize.
	TimestampType = NewSimpleTypeWithMinSize("timestamp", cel.TimestampType, types.Timestamp{Time: time.Time{}}, JSONDateSize)

	// UintType is equivalent to the CEL 'uint' type.
	UintType = NewSimpleTypeWithMinSize("uint", cel.UintType, types.Uint(0), 1)

	// ListType is equivalent to the CEL 'list' type.
	ListType = NewListType(AnyType, noMaxLength)

	// MapType is equivalent to the CEL 'map' type.
	MapType = NewMapType(AnyType, AnyType, noMaxLength)
)
View Source
var (
	URLObject = decls.NewObjectType("kubernetes.URL")

	URLType = cel.ObjectType("kubernetes.URL")
)

Functions

func Escape

func Escape(ident string) (string, bool)

Escape escapes ident and returns a CEL identifier (of the form '[a-zA-Z_][a-zA-Z0-9_]*'), or returns false if the ident does not match the supported input format of `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*`. Escaping Rules:

  • '__' escapes to '__underscores__'
  • '.' escapes to '__dot__'
  • '-' escapes to '__dash__'
  • '/' escapes to '__slash__'
  • Identifiers that exactly match a CEL RESERVED keyword escape to '__{keyword}__'. The keywords are: "true", "false", "null", "in", "as", "break", "const", "continue", "else", "for", "function", "if", "import", "let", loop", "package", "namespace", "return".

func FieldTypeMap

func FieldTypeMap(path string, t *DeclType) map[string]*DeclType

FieldTypeMap constructs a map of the field and object types nested within a given type.

func Unescape

func Unescape(escaped string) (string, bool)

Unescape unescapes an CEL identifier containing the escape sequences described in Escape, or return false if the string contains invalid escape sequences. The escaped input is expected to be a valid CEL identifier, but is not checked.

Types

type DeclField

type DeclField struct {
	Name     string
	Type     *DeclType
	Required bool
	// contains filtered or unexported fields
}

DeclField describes the name, ordinal, and optionality of a field declaration within a type.

func NewDeclField

func NewDeclField(name string, declType *DeclType, required bool, enumValues []interface{}, defaultValue interface{}) *DeclField

func (*DeclField) DefaultValue

func (f *DeclField) DefaultValue() ref.Val

DefaultValue returns the zero value associated with the field.

func (*DeclField) EnumValues

func (f *DeclField) EnumValues() []ref.Val

EnumValues returns the set of values that this field may take.

func (*DeclField) TypeName

func (f *DeclField) TypeName() string

TypeName returns the string type name of the field.

type DeclType

type DeclType struct {
	fmt.Stringer

	// Fields contains a map of escaped CEL identifier field names to field declarations.
	Fields      map[string]*DeclField
	KeyType     *DeclType
	ElemType    *DeclType
	TypeParam   bool
	Metadata    map[string]string
	MaxElements int64
	// MinSerializedSize represents the smallest possible size in bytes that
	// the DeclType could be serialized to in JSON.
	MinSerializedSize int64
	// contains filtered or unexported fields
}

DeclType represents the universal type descriptor for OpenAPIv3 types.

func NewListType

func NewListType(elem *DeclType, maxItems int64) *DeclType

NewListType returns a parameterized list type with a specified element type.

func NewMapType

func NewMapType(key, elem *DeclType, maxProperties int64) *DeclType

NewMapType returns a parameterized map type with the given key and element types.

func NewObjectType

func NewObjectType(name string, fields map[string]*DeclField) *DeclType

NewObjectType creates an object type with a qualified name and a set of field declarations.

func NewSimpleTypeWithMinSize

func NewSimpleTypeWithMinSize(name string, celType *cel.Type, zeroVal ref.Val, minSize int64) *DeclType

func (*DeclType) CelType

func (t *DeclType) CelType() *cel.Type

CelType returns the CEL type of this declaration.

func (*DeclType) DefaultValue

func (t *DeclType) DefaultValue() ref.Val

DefaultValue returns the CEL ref.Val representing the default value for this object type, if one exists.

func (*DeclType) ExprType

func (t *DeclType) ExprType() (*exprpb.Type, error)

ExprType returns the CEL expression type of this declaration.

func (*DeclType) FindField

func (t *DeclType) FindField(name string) (*DeclField, bool)

FindField returns the DeclField with the given name if present.

func (*DeclType) HasTrait

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

HasTrait implements the CEL ref.Type interface making this type declaration suitable for use within the CEL evaluator.

func (*DeclType) IsList

func (t *DeclType) IsList() bool

IsList returns whether the declaration is a `list` type which defines a parameterized element type, but not a parameterized key type or fields.

func (*DeclType) IsMap

func (t *DeclType) IsMap() bool

IsMap returns whether the declaration is a 'map' type which defines parameterized key and element types, but not fields.

func (*DeclType) IsObject

func (t *DeclType) IsObject() bool

IsObject returns whether the declartion is an 'object' type which defined a set of typed fields.

func (*DeclType) MaybeAssignTypeName

func (t *DeclType) MaybeAssignTypeName(name string) *DeclType

MaybeAssignTypeName attempts to set the DeclType name to a fully qualified name, if the type is of `object` type.

The DeclType must return true for `IsObject` or this assignment will error.

func (*DeclType) String

func (t *DeclType) String() string

String implements the fmt.Stringer interface method.

func (*DeclType) TypeName

func (t *DeclType) TypeName() string

TypeName returns the fully qualified type name for the DeclType.

type DeclTypeProvider added in v0.28.0

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

DeclTypeProvider extends the CEL ref.TypeProvider interface and provides an Open API Schema-based type-system.

func NewDeclTypeProvider added in v0.28.0

func NewDeclTypeProvider(rootTypes ...*DeclType) *DeclTypeProvider

NewDeclTypeProvider returns an Open API Schema-based type-system which is CEL compatible.

func (*DeclTypeProvider) EnumValue added in v0.28.0

func (rt *DeclTypeProvider) EnumValue(enumName string) ref.Val

func (*DeclTypeProvider) EnvOptions added in v0.28.0

func (rt *DeclTypeProvider) EnvOptions(tp ref.TypeProvider) ([]cel.EnvOption, error)

EnvOptions returns a set of cel.EnvOption values which includes the declaration set as well as a custom ref.TypeProvider.

If the DeclTypeProvider value is nil, an empty []cel.EnvOption set is returned.

func (*DeclTypeProvider) FindDeclType added in v0.28.0

func (rt *DeclTypeProvider) FindDeclType(typeName string) (*DeclType, bool)

FindDeclType returns the CPT type description which can be mapped to a CEL type.

func (*DeclTypeProvider) FindFieldType added in v0.28.0

func (rt *DeclTypeProvider) FindFieldType(typeName, fieldName string) (*ref.FieldType, bool)

FindFieldType returns a field type given a type name and field name, if found.

Note, the type name for an Open API Schema type is likely to be its qualified object path. If, in the future an object instance rather than a type name were provided, the field resolution might more accurately reflect the expected type model. However, in this case concessions were made to align with the existing CEL interfaces.

func (*DeclTypeProvider) FindIdent added in v0.28.0

func (rt *DeclTypeProvider) FindIdent(identName string) (ref.Val, bool)

func (*DeclTypeProvider) FindType added in v0.28.0

func (rt *DeclTypeProvider) FindType(typeName string) (*exprpb.Type, bool)

FindType attempts to resolve the typeName provided from the rule's rule-schema, or if not from the embedded ref.TypeProvider.

FindType overrides the default type-finding behavior of the embedded TypeProvider.

Note, when the type name is based on the Open API Schema, the name will reflect the object path where the type definition appears.

func (*DeclTypeProvider) NativeToValue added in v0.28.0

func (rt *DeclTypeProvider) NativeToValue(val interface{}) ref.Val

NativeToValue is an implementation of the ref.TypeAdapater interface which supports conversion of rule values to CEL ref.Val instances.

func (*DeclTypeProvider) NewValue added in v0.28.0

func (rt *DeclTypeProvider) NewValue(typeName string, fields map[string]ref.Val) ref.Val

func (*DeclTypeProvider) TypeNames added in v0.28.0

func (rt *DeclTypeProvider) TypeNames() []string

TypeNames returns the list of type names declared within the DeclTypeProvider object.

func (*DeclTypeProvider) WithTypeProvider added in v0.28.0

func (rt *DeclTypeProvider) WithTypeProvider(tp ref.TypeProvider) (*DeclTypeProvider, error)

WithTypeProvider returns a new DeclTypeProvider that sets the given TypeProvider If the original DeclTypeProvider is nil, the returned DeclTypeProvider is still nil.

type DynValue

type DynValue struct {
	ID          int64
	EncodeStyle EncodeStyle
	// contains filtered or unexported fields
}

DynValue is a dynamically typed value used to describe unstructured content. Whether the value has the desired type is determined by where it is used within the Instance or Template, and whether there are schemas which might enforce a more rigid type definition.

func NewDynValue

func NewDynValue(id int64, val interface{}) (*DynValue, error)

NewDynValue returns a DynValue that corresponds to a parse node id and value.

func NewEmptyDynValue

func NewEmptyDynValue() *DynValue

NewEmptyDynValue returns the zero-valued DynValue.

func (*DynValue) ConvertToNative

func (dv *DynValue) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative is an implementation of the CEL ref.Val method used to adapt between CEL types and Go-native types.

The default behavior of this method is to first convert to a CEL type which has a well-defined set of conversion behaviors and proxy to the CEL ConvertToNative method for the type.

func (*DynValue) DeclType

func (dv *DynValue) DeclType() *DeclType

DeclType returns the policy model type of the dyn value.

func (*DynValue) Equal

func (dv *DynValue) Equal(other ref.Val) ref.Val

Equal returns whether the dyn value is equal to a given CEL value.

func (*DynValue) ExprValue

func (dv *DynValue) ExprValue() ref.Val

ExprValue converts the DynValue into a CEL value.

func (*DynValue) SetValue

func (dv *DynValue) SetValue(value interface{}) error

SetValue updates the underlying value held by this reference.

func (*DynValue) Type

func (dv *DynValue) Type() ref.Type

Type returns the CEL type for the given value.

func (*DynValue) Value

func (dv *DynValue) Value() interface{}

Value returns the underlying value held by this reference.

type EncodeStyle

type EncodeStyle int

EncodeStyle is a hint for string encoding of parsed values.

const (
	// BlockValueStyle is the default string encoding which preserves whitespace and newlines.
	BlockValueStyle EncodeStyle = iota

	// FlowValueStyle indicates that the string is an inline representation of complex types.
	FlowValueStyle

	// FoldedValueStyle is a multiline string with whitespace and newlines trimmed to a single
	// a whitespace. Repeated newlines are replaced with a single newline rather than a single
	// whitespace.
	FoldedValueStyle

	// LiteralStyle is a multiline string that preserves newlines, but trims all other whitespace
	// to a single character.
	LiteralStyle
)

type Error

type Error struct {
	Type   ErrorType
	Detail string
}

Error is an implementation of the 'error' interface, which represents a XValidation error.

func (*Error) Error

func (v *Error) Error() string

Error implements the error interface.

type ErrorType

type ErrorType string

ErrorType is a machine readable value providing more detail about why a XValidation is invalid.

const (
	// ErrorTypeRequired is used to report withNullable values that are not
	// provided (e.g. empty strings, null values, or empty arrays).  See
	// Required().
	ErrorTypeRequired ErrorType = "RuleRequired"
	// ErrorTypeInvalid is used to report malformed values
	ErrorTypeInvalid ErrorType = "RuleInvalid"
	// ErrorTypeInternal is used to report other errors that are not related
	// to user input.  See InternalError().
	ErrorTypeInternal ErrorType = "InternalError"
)

type Field

type Field struct {
	ID   int64
	Name string
	Ref  *DynValue
}

Field specifies a field name and a reference to a dynamic value.

func NewField

func NewField(id int64, name string) *Field

NewField returns a MapField instance with an empty DynValue that refers to the specified parse node id and field name.

type ListValue

type ListValue struct {
	Entries []*DynValue
	// contains filtered or unexported fields
}

ListValue contains a list of dynamically typed entries.

func NewListValue

func NewListValue() *ListValue

NewListValue returns an empty ListValue instance.

func (*ListValue) Add

func (lv *ListValue) Add(other ref.Val) ref.Val

Add concatenates two lists together to produce a new CEL list value.

func (*ListValue) Append

func (lv *ListValue) Append(entry *DynValue)

Append adds another entry into the ListValue.

func (*ListValue) Contains

func (lv *ListValue) Contains(val ref.Val) ref.Val

Contains returns whether the input `val` is equal to an element in the list.

If any pair-wise comparison between the input value and the list element is an error, the operation will return an error.

func (*ListValue) ConvertToNative

func (lv *ListValue) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative is an implementation of the CEL ref.Val method used to adapt between CEL types and Go-native array-like types.

func (*ListValue) ConvertToType

func (lv *ListValue) ConvertToType(t ref.Type) ref.Val

ConvertToType converts the ListValue to another CEL type.

func (*ListValue) Equal

func (lv *ListValue) Equal(other ref.Val) ref.Val

Equal returns true if two lists are of the same size, and the values at each index are also equal.

func (*ListValue) Get

func (lv *ListValue) Get(idx ref.Val) ref.Val

Get returns the value at the given index.

If the index is negative or greater than the size of the list, an error is returned.

func (*ListValue) Iterator

func (lv *ListValue) Iterator() traits.Iterator

Iterator produces a traits.Iterator suitable for use in CEL comprehension macros.

func (*ListValue) Size

func (lv *ListValue) Size() ref.Val

Size returns the number of elements in the list.

func (*ListValue) Type

func (lv *ListValue) Type() ref.Type

Type returns the CEL ref.Type for the list.

func (*ListValue) Value

func (lv *ListValue) Value() interface{}

Value returns the Go-native value.

type MapValue

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

MapValue declares an object with a set of named fields whose values are dynamically typed.

func NewMapValue

func NewMapValue() *MapValue

NewMapValue returns an empty MapValue.

func (MapValue) AddField

func (sv MapValue) AddField(field *Field)

AddField appends a MapField to the MapValue and indexes the field by name.

func (*MapValue) Contains

func (m *MapValue) Contains(key ref.Val) ref.Val

Contains returns whether the given key is contained in the MapValue.

func (MapValue) ConvertToNative

func (sv MapValue) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative converts the MapValue type to a native go types.

func (*MapValue) ConvertToObject

func (m *MapValue) ConvertToObject(declType *DeclType) *ObjectValue

ConvertToObject produces an ObjectValue from the MapValue with the associated schema type.

The conversion is shallow and the memory shared between the Object and Map as all references to the map are expected to be replaced with the Object reference.

func (*MapValue) ConvertToType

func (m *MapValue) ConvertToType(t ref.Type) ref.Val

ConvertToType converts the MapValue to another CEL type, if possible.

func (*MapValue) Equal

func (m *MapValue) Equal(other ref.Val) ref.Val

Equal returns true if the maps are of the same size, have the same keys, and the key-values from each map are equal.

func (*MapValue) Find

func (m *MapValue) Find(name ref.Val) (ref.Val, bool)

Find returns the value for the key in the map, if found.

func (*MapValue) Get

func (m *MapValue) Get(key ref.Val) ref.Val

Get returns the value for the key in the map, or error if not found.

func (MapValue) GetField

func (sv MapValue) GetField(name string) (*Field, bool)

GetField returns a MapField by name if one exists.

func (MapValue) IsSet

func (sv MapValue) IsSet(key ref.Val) ref.Val

IsSet returns whether the given field, which is defined, has also been set.

func (*MapValue) Iterator

func (m *MapValue) Iterator() traits.Iterator

Iterator produces a traits.Iterator which walks over the map keys.

The Iterator is frequently used within comprehensions.

func (*MapValue) Size

func (m *MapValue) Size() ref.Val

Size returns the number of keys in the map.

func (*MapValue) Type

func (m *MapValue) Type() ref.Type

Type returns the CEL ref.Type for the map.

func (*MapValue) Value

func (m *MapValue) Value() interface{}

Value returns the Go-native representation of the MapValue.

type MultilineStringValue

type MultilineStringValue struct {
	Value string
	Raw   string
}

MultilineStringValue is a multiline string value which has been parsed in a way which omits whitespace as well as a raw form which preserves whitespace.

type ObjectValue

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

ObjectValue is a struct with a custom schema type which indicates the fields and types associated with the structure.

func NewObjectValue

func NewObjectValue(sType *DeclType) *ObjectValue

NewObjectValue creates a struct value with a schema type and returns the empty ObjectValue.

func (ObjectValue) AddField

func (sv ObjectValue) AddField(field *Field)

AddField appends a MapField to the MapValue and indexes the field by name.

func (ObjectValue) ConvertToNative

func (sv ObjectValue) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative converts the MapValue type to a native go types.

func (*ObjectValue) ConvertToType

func (o *ObjectValue) ConvertToType(t ref.Type) ref.Val

ConvertToType is an implementation of the CEL ref.Val interface method.

func (*ObjectValue) Equal

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

Equal returns true if the two object types are equal and their field values are equal.

func (*ObjectValue) Get

func (o *ObjectValue) Get(name ref.Val) ref.Val

Get returns the value of the specified field.

If the field is set, its value is returned. If the field is not set, the default value for the field is returned thus allowing for safe-traversal and preserving proto-like field traversal semantics for Open API Schema backed types.

func (ObjectValue) GetField

func (sv ObjectValue) GetField(name string) (*Field, bool)

GetField returns a MapField by name if one exists.

func (ObjectValue) IsSet

func (sv ObjectValue) IsSet(key ref.Val) ref.Val

IsSet returns whether the given field, which is defined, has also been set.

func (*ObjectValue) Type

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

Type returns the CEL type value of the object.

func (*ObjectValue) Value

func (o *ObjectValue) Value() interface{}

Value returns the Go-native representation of the object.

type PlainTextValue

type PlainTextValue string

PlainTextValue is a text string literal which must not be treated as an expression.

type Quantity added in v0.28.0

type Quantity struct {
	*resource.Quantity
}

Quantity provdes a CEL representation of a resource.Quantity

func (Quantity) ConvertToNative added in v0.28.0

func (d Quantity) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

func (Quantity) ConvertToType added in v0.28.0

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

func (Quantity) Equal added in v0.28.0

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

func (Quantity) Type added in v0.28.0

func (d Quantity) Type() ref.Type

func (Quantity) Value added in v0.28.0

func (d Quantity) Value() interface{}

type URL

type URL struct {
	*url.URL
}

URL provides a CEL representation of a URL.

func (URL) ConvertToNative

func (d URL) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (URL) ConvertToType

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

ConvertToType implements ref.Val.ConvertToType.

func (URL) Equal

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

Equal implements ref.Val.Equal.

func (URL) Type

func (d URL) Type() ref.Type

Type implements ref.Val.Type.

func (URL) Value

func (d URL) Value() interface{}

Value implements ref.Val.Value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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