util

package
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 13 Imported by: 281

Documentation

Overview

Package util provides generic utilities used throughout the policy engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BFS added in v0.4.9

func BFS(t Traversal, f Iter, u T) bool

BFS performs a breadth first traversal calling f for each node starting from u. If f returns true, traversal stops and BFS returns true.

func Backoff added in v0.8.0

func Backoff(base, max, jitter, factor float64, retries int) time.Duration

Backoff returns a delay with an exponential backoff based on the number of retries. Same algorithm used in gRPC.

func Close added in v0.8.0

func Close(resp *http.Response)

Close reads the remaining bytes from the response and then closes it to ensure that the connection is freed. If the body is not read and closed, a leak can occur.

func Compare

func Compare(a, b interface{}) int

Compare returns 0 if a equals b, -1 if a is less than b, and 1 if b is than a.

For comparison between values of different types, the following ordering is used: nil < bool < int, float64 < string < []interface{} < map[string]interface{}. Slices and maps are compared recursively. If one slice or map is a subset of the other slice or map it is considered "less than". Nil is always equal to nil.

func DFS

func DFS(t Traversal, f Iter, u T) bool

DFS performs a depth first traversal calling f for each node starting from u. If f returns true, traversal stops and DFS returns true.

func DefaultBackoff added in v0.8.0

func DefaultBackoff(base, max float64, retries int) time.Duration

DefaultBackoff returns a delay with an exponential backoff based on the number of retries.

func MustMarshalJSON added in v0.4.1

func MustMarshalJSON(x interface{}) []byte

MustMarshalJSON returns the JSON encoding of x

If the data cannot be encoded, this function will panic. This function is for test purposes.

func MustUnmarshalJSON added in v0.4.1

func MustUnmarshalJSON(bs []byte) interface{}

MustUnmarshalJSON parse the JSON encoded data and returns the result.

If the data cannot be decoded, this function will panic. This function is for test purposes.

func NewJSONDecoder added in v0.3.0

func NewJSONDecoder(r io.Reader) *json.Decoder

NewJSONDecoder returns a new decoder that reads from r.

This function is intended to be used in place of the standard json.NewDecoder when json.Number is required.

func Reference added in v0.8.2

func Reference(x interface{}) *interface{}

Reference returns a pointer to its argument unless the argument already is a pointer. If the argument is **t, or ***t, etc, it will return *t.

Used for preparing Go types (including pointers to structs) into values to be put through util.RoundTrip().

func RoundTrip added in v0.8.0

func RoundTrip(x *interface{}) error

RoundTrip encodes to JSON, and decodes the result again.

Thereby, it is converting its argument to the representation expected by rego.Input and inmem's Write operations. Works with both references and values.

func Unmarshal added in v0.8.0

func Unmarshal(bs []byte, v interface{}) error

Unmarshal decodes a YAML, JSON or JSON extension value into the specified type.

func UnmarshalJSON added in v0.3.0

func UnmarshalJSON(bs []byte, x interface{}) error

UnmarshalJSON parses the JSON encoded data and stores the result in the value pointed to by x.

This function is intended to be used in place of the standard json.Marshal function when json.Number is required.

func WaitFunc added in v0.23.0

func WaitFunc(fun func() bool, interval, timeout time.Duration) error

WaitFunc will call passed function at an interval and return nil as soon this function returns true. If timeout is reached before the passed in function returns true an error is returned.

Types

type EnumFlag added in v0.4.5

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

EnumFlag implements the pflag.Value interface to provide enumerated command line parameter values.

func NewEnumFlag added in v0.4.5

func NewEnumFlag(defaultValue string, vs []string) *EnumFlag

NewEnumFlag returns a new EnumFlag that has a defaultValue and vs enumerated values.

func (*EnumFlag) IsSet added in v0.19.0

func (f *EnumFlag) IsSet() bool

IsSet will return true if the EnumFlag has been set.

func (*EnumFlag) Set added in v0.4.5

func (f *EnumFlag) Set(s string) error

Set sets the enum value. If s is not a valid enum value, an error is returned.

func (*EnumFlag) String added in v0.4.5

func (f *EnumFlag) String() string

String returns the EnumValue's value as string.

func (*EnumFlag) Type added in v0.4.5

func (f *EnumFlag) Type() string

Type returns the valid enumeration values.

type Equals added in v0.4.9

type Equals func(u T, v T) bool

Equals should return true if node "u" equals node "v".

type FIFO added in v0.4.9

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

FIFO represents a simple FIFO queue.

func NewFIFO added in v0.4.9

func NewFIFO(ts ...T) *FIFO

NewFIFO returns a new FIFO queue containing elements ts starting with the left-most argument at the front.

func (*FIFO) Peek added in v0.4.9

func (s *FIFO) Peek() (T, bool)

Peek returns the top of the LIFO. If LIFO is empty, returns nil, false.

func (*FIFO) Pop added in v0.4.9

func (s *FIFO) Pop() (T, bool)

Pop returns the top of the LIFO and removes it. If LIFO is empty returns nil, false.

func (*FIFO) Push added in v0.4.9

func (s *FIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*FIFO) Size added in v0.4.9

func (s *FIFO) Size() int

Size returns the size of the LIFO.

type HashMap

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

HashMap represents a key/value map.

func NewHashMap

func NewHashMap(eq func(T, T) bool, hash func(T) int) *HashMap

NewHashMap returns a new empty HashMap.

func (*HashMap) Copy

func (h *HashMap) Copy() *HashMap

Copy returns a shallow copy of this HashMap.

func (*HashMap) Delete

func (h *HashMap) Delete(k T)

Delete removes the key k.

func (*HashMap) Equal

func (h *HashMap) Equal(other *HashMap) bool

Equal returns true if this HashMap equals the other HashMap. Two hash maps are equal if they contain the same key/value pairs.

func (*HashMap) Get

func (h *HashMap) Get(k T) (T, bool)

Get returns the value for k.

func (*HashMap) Hash

func (h *HashMap) Hash() int

Hash returns the hash code for this hash map.

func (*HashMap) Iter

func (h *HashMap) Iter(iter func(T, T) bool) bool

Iter invokes the iter function for each element in the HashMap. If the iter function returns true, iteration stops and the return value is true. If the iter function never returns true, iteration proceeds through all elements and the return value is false.

func (*HashMap) Len

func (h *HashMap) Len() int

Len returns the current size of this HashMap.

func (*HashMap) Put

func (h *HashMap) Put(k T, v T)

Put inserts a key/value pair into this HashMap. If the key is already present, the existing value is overwritten.

func (*HashMap) String

func (h *HashMap) String() string

func (*HashMap) Update

func (h *HashMap) Update(other *HashMap) *HashMap

Update returns a new HashMap with elements from the other HashMap put into this HashMap. If the other HashMap contains elements with the same key as this HashMap, the value from the other HashMap overwrites the value from this HashMap.

type Iter added in v0.4.9

type Iter func(u T) bool

Iter should return true to indicate stop.

type LIFO added in v0.4.9

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

LIFO represents a simple LIFO queue.

func NewLIFO added in v0.4.9

func NewLIFO(ts ...T) *LIFO

NewLIFO returns a new LIFO queue containing elements ts starting with the left-most argument at the bottom.

func (*LIFO) Peek added in v0.4.9

func (s *LIFO) Peek() (T, bool)

Peek returns the top of the LIFO. If LIFO is empty, returns nil, false.

func (*LIFO) Pop added in v0.4.9

func (s *LIFO) Pop() (T, bool)

Pop returns the top of the LIFO and removes it. If LIFO is empty returns nil, false.

func (*LIFO) Push added in v0.4.9

func (s *LIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*LIFO) Size added in v0.4.9

func (s *LIFO) Size() int

Size returns the size of the LIFO.

type T

type T interface{}

T is a concise way to refer to T.

func DFSPath added in v0.4.9

func DFSPath(t Traversal, eq Equals, a, z T) []T

DFSPath returns a path from node a to node z found by performing a depth first traversal. If no path is found, an empty slice is returned.

type Traversal added in v0.4.9

type Traversal interface {

	// Edges should return the neighbours of node "u".
	Edges(u T) []T

	// Visited should return true if node "u" has already been visited in this
	// traversal. If the same traversal is used multiple times, the state that
	// tracks visited nodes should be reset.
	Visited(u T) bool
}

Traversal defines a basic interface to perform traversals.

Directories

Path Synopsis
Package test contains utilities used in the policy engine's test suite.
Package test contains utilities used in the policy engine's test suite.

Jump to

Keyboard shortcuts

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