types

package
v1.25.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: Apache-2.0 Imports: 13 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AnyType     = registerBasicType("any", cel.DynType, func(value any) (any, error) { return value, nil })
	BooleanType = registerBasicType("bool", cel.BoolType, requireType[bool])
	StringType  = registerBasicType("string", cel.StringType, requireType[string])
	IntType     = registerBasicType("int", cel.IntType, convertNumericType[int64])
	UIntType    = registerBasicType("uint", cel.IntType, convertNumericType[uint64])
	DoubleType  = registerBasicType("double", cel.DoubleType, convertNumericType[float64])

	BytesType = registerBasicType("bytes", cel.BytesType, func(value any) (any, error) {
		vle, ok := value.(string)
		if !ok {
			return nil, fmt.Errorf("bytes requires a base64 unicode string, found: %T `%v`", value, value)
		}

		decoded, err := base64.StdEncoding.DecodeString(vle)
		if err != nil {
			return nil, fmt.Errorf("bytes requires a base64 encoded string: %w", err)
		}

		return decoded, nil
	})

	DurationType = registerBasicType("duration", cel.DurationType, func(value any) (any, error) {
		vle, ok := value.(string)
		if !ok {
			return nil, fmt.Errorf("durations requires a duration string, found: %T", value)
		}

		d, err := time.ParseDuration(vle)
		if err != nil {
			return nil, fmt.Errorf("could not parse duration string `%s`: %w", vle, err)
		}

		return d, nil
	})

	TimestampType = registerBasicType("timestamp", cel.TimestampType, func(value any) (any, error) {
		vle, ok := value.(string)
		if !ok {
			return nil, fmt.Errorf("timestamps requires a RFC 3339 formatted timestamp string, found: %T `%v`", value, value)
		}

		d, err := time.Parse(time.RFC3339, vle)
		if err != nil {
			return nil, fmt.Errorf("could not parse RFC 3339 formatted timestamp string `%s`: %w", vle, err)
		}

		return d, nil
	})

	ListType = registerGenericType("list", 1,
		func(childTypes []VariableType) VariableType {
			return VariableType{
				localName:  "list",
				celType:    cel.ListType(childTypes[0].celType),
				childTypes: childTypes,
				converter: func(value any) (any, error) {
					vle, ok := value.([]any)
					if !ok {
						return nil, fmt.Errorf("list requires a list, found: %T", value)
					}

					converted := make([]any, 0, len(vle))
					for index, item := range vle {
						convertedItem, err := childTypes[0].ConvertValue(item)
						if err != nil {
							return nil, fmt.Errorf("found an invalid value for item at index %d: %w", index, err)
						}
						converted = append(converted, convertedItem)
					}

					return converted, nil
				},
			}
		})

	MapType = registerGenericType("map", 1,
		func(childTypes []VariableType) VariableType {
			return VariableType{
				localName:  "map",
				celType:    cel.MapType(cel.StringType, childTypes[0].celType),
				childTypes: childTypes,
				converter: func(value any) (any, error) {
					vle, ok := value.(map[string]any)
					if !ok {
						return nil, fmt.Errorf("map requires a map, found: %T", value)
					}

					converted := make(map[string]any, len(vle))
					for key, item := range vle {
						convertedItem, err := childTypes[0].ConvertValue(item)
						if err != nil {
							return nil, fmt.Errorf("found an invalid value for key `%s`: %w", key, err)
						}

						converted[key] = convertedItem
					}

					return converted, nil
				},
			}
		},
	)
)
View Source
var CustomMethodsOnTypes []cel.EnvOption

CustomMethodsOnTypes holds a set of new methods applied over defined types. This is exported so that the CEL environment construction can apply the necessary env options to support these methods

View Source
var CustomTypes = map[string][]cel.EnvOption{}

CustomTypes holds the set of custom types defined and exported by this package. This is exported so that the CEL environment construction can apply the necessary env options for the custom types.

View Source
var IPAddressType = registerCustomType[IPAddress](
	"ipaddress",
	cel.ObjectType("IPAddress"),
	func(value any) (any, error) {
		ipvalue, ok := value.(IPAddress)
		if ok {
			return ipvalue, nil
		}

		vle, ok := value.(string)
		if !ok {
			return nil, fmt.Errorf("ipaddress requires an ipaddress string, found: %T `%v`", value, value)
		}

		d, err := ParseIPAddress(vle)
		if err != nil {
			return nil, fmt.Errorf("could not parse ip address string `%s`: %w", vle, err)
		}

		return d, nil
	},
	cel.Function("in_cidr",
		cel.MemberOverload("ipaddress_in_cidr_string",
			[]*cel.Type{cel.ObjectType("IPAddress"), cel.StringType},
			cel.BoolType,
			cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
				cidr, ok := rhs.Value().(string)
				if !ok {
					return types.NewErr("expected CIDR string")
				}

				network, err := netip.ParsePrefix(cidr)
				if err != nil {
					return types.NewErr("invalid CIDR string: `%s`", cidr)
				}

				return types.Bool(network.Contains(lhs.(IPAddress).ip))
			}),
		),
	))

Functions

func DecodeParameterTypes added in v1.24.0

func DecodeParameterTypes(parameters map[string]*core.CaveatTypeReference) (map[string]VariableType, error)

DecodeParameterTypes decodes the core caveat parameter types into internal caveat types.

func EncodeParameterType

func EncodeParameterType(varType VariableType) *core.CaveatTypeReference

EncodeParameterType converts an internal caveat type into a storable core type.

func EncodeParameterTypes

func EncodeParameterTypes(parametersAndTypes map[string]VariableType) map[string]*core.CaveatTypeReference

EncodeParameterTypes converts the map of internal caveat types into a map of types for storing the caveat in the core.

func TypeKeywords

func TypeKeywords() []string

TypeKeywords returns all keywords associated with types.

Types

type CustomType added in v1.21.0

type CustomType interface {
	// SerializedString returns the serialized string form of the data within
	// this instance of the type.
	SerializedString() string
}

CustomType is the interface for custom-defined types.

type IPAddress

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

IPAddress defines a custom type for representing an IP Address in caveats.

func MustParseIPAddress

func MustParseIPAddress(ip string) IPAddress

MustParseIPAddress parses the string form of an IP Address into an IPAddress object type.

func ParseIPAddress

func ParseIPAddress(ip string) (IPAddress, error)

ParseIPAddress parses the string form of an IP Address into an IPAddress object type.

func (IPAddress) ConvertToNative

func (ipa IPAddress) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

func (IPAddress) ConvertToType

func (ipa IPAddress) ConvertToType(typeVal ref.Type) ref.Val

func (IPAddress) Equal

func (ipa IPAddress) Equal(other ref.Val) ref.Val

func (IPAddress) SerializedString added in v1.21.0

func (ipa IPAddress) SerializedString() string

func (IPAddress) Type

func (ipa IPAddress) Type() ref.Type

func (IPAddress) Value

func (ipa IPAddress) Value() interface{}

type VariableType

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

VariableType defines the supported types of variables in caveats.

func BuildType

func BuildType(name string, childTypes []VariableType) (*VariableType, error)

BuildType builds a variable type from its name and child types.

func DecodeParameterType

func DecodeParameterType(parameterType *core.CaveatTypeReference) (*VariableType, error)

DecodeParameterType decodes the core caveat parameter type into an internal caveat type.

func MustListType added in v1.16.0

func MustListType(childTypes ...VariableType) VariableType

func MustMapType added in v1.16.0

func MustMapType(childTypes ...VariableType) VariableType

func (VariableType) CelType

func (vt VariableType) CelType() *cel.Type

CelType returns the underlying CEL type for the variable type.

func (VariableType) ConvertValue

func (vt VariableType) ConvertValue(value any) (any, error)

ConvertValue converts the given value into one expected by this variable type.

func (VariableType) String

func (vt VariableType) String() string

Jump to

Keyboard shortcuts

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