types

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HierarchyType = types.NewTypeValue(hierarchyTypeName,
		traits.IndexerType,
		traits.SizerType,
		traits.ReceiverType)

	HierarchyDeclrations = []*exprpb.Decl{
		decls.NewFunction(hierarchyFn,
			decls.NewOverload(fmt.Sprintf("%s_string", hierarchyFn),
				[]*exprpb.Type{decls.String},
				hierarchyTypeExpr,
			),
			decls.NewOverload(fmt.Sprintf("%s_string_string", hierarchyFn),
				[]*exprpb.Type{decls.String, decls.String},
				hierarchyTypeExpr,
			),
			decls.NewOverload(fmt.Sprintf("%s_stringarray", hierarchyFn),
				[]*exprpb.Type{decls.NewListType(decls.String)},
				hierarchyTypeExpr,
			),
		),

		decls.NewFunction(overloadAncestorOf,
			decls.NewInstanceOverload(overloadAncestorOf,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloadCommonAncestors,
			decls.NewInstanceOverload(overloadCommonAncestors,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				hierarchyTypeExpr,
			),
		),

		decls.NewFunction(overloadDescendentOf,
			decls.NewInstanceOverload(overloadDescendentOf,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloadImmediateChildOf,
			decls.NewInstanceOverload(overloadImmediateChildOf,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloadImmediateParentOf,
			decls.NewInstanceOverload(overloadImmediateParentOf,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloadOverlaps,
			decls.NewInstanceOverload(overloadOverlaps,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloadSiblingOf,
			decls.NewInstanceOverload(overloadSiblingOf,
				[]*exprpb.Type{hierarchyTypeExpr, hierarchyTypeExpr},
				decls.Bool,
			),
		),

		decls.NewFunction(overloads.Size,
			decls.NewInstanceOverload(fmt.Sprintf("%s_size", hierarchyFn),
				[]*exprpb.Type{hierarchyTypeExpr},
				decls.Int,
			),
		),

		decls.NewFunction(operators.Index,
			decls.NewOverload(fmt.Sprintf("%s_index", hierarchyFn),
				[]*exprpb.Type{hierarchyTypeExpr, decls.Int},
				decls.String,
			),
		),
	}

	HierarchyOverload = &functions.Overload{
		Operator: hierarchyFn,
		Unary: func(v ref.Val) ref.Val {
			switch hv := v.(type) {
			case Hierarchy:
				return hv
			case types.String:
				return Hierarchy(strings.Split(string(hv), hierarchyDelim))
			case traits.Lister:
				hieraEls, err := hv.ConvertToNative(reflect.SliceOf(reflect.TypeOf("")))
				if err != nil {
					return types.NewErr("failed to convert list to string slice: %v", err)
				}

				h, ok := hieraEls.([]string)
				if !ok {
					return types.NewErr("expected string slice but got %T", hieraEls)
				}

				return Hierarchy(h)
			default:
				return types.MaybeNoSuchOverloadErr(v)
			}
		},
		Binary: func(v, delim ref.Val) ref.Val {
			vStr, ok := v.(types.String)
			if !ok {
				return types.NoSuchOverloadErr()
			}

			delimStr, ok := delim.(types.String)
			if !ok {
				return types.NoSuchOverloadErr()
			}

			return Hierarchy(strings.Split(string(vStr), string(delimStr)))
		},
	}
)

Functions

func NewCustomCELTypeAdapter

func NewCustomCELTypeAdapter() ref.TypeAdapter

NewCustomCELTypeAdapter provides a CEL type adaptor than can deal with json.Number values returned by Rego.

Types

type Hierarchy

type Hierarchy []string

Hierarchy is a type that represents a dot-separated hierarchy such as a.b.c.d.

func (Hierarchy) ConvertToNative

func (h Hierarchy) ConvertToNative(typeDesc reflect.Type) (interface{}, error)

ConvertToNative implements ref.Val.ConvertToNative.

func (Hierarchy) ConvertToType

func (h Hierarchy) ConvertToType(typeVal ref.Type) ref.Val

ConvertToType implements ref.Val.ConvertToType.

func (Hierarchy) Equal

func (h Hierarchy) Equal(other ref.Val) ref.Val

Equal implements ref.Val.Equal.

func (Hierarchy) Get

func (h Hierarchy) Get(index ref.Val) ref.Val

Get implements traits.Indexer.Get.

func (Hierarchy) Receive

func (h Hierarchy) Receive(function, overload string, args []ref.Val) ref.Val

Receive implements traits.Reciever.Receive.

func (Hierarchy) Size

func (h Hierarchy) Size() ref.Val

Size implements traits.Sizer.Size.

func (Hierarchy) Type

func (h Hierarchy) Type() ref.Type

Type implements ref.Val.Type.

func (Hierarchy) Value

func (h Hierarchy) Value() interface{}

Value implements ref.Val.Value.

Jump to

Keyboard shortcuts

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