package util

import "github.com/open-policy-agent/opa/v1/util"

Package util provides generic utilities used throughout the policy engine.

Index

Functions

func BFS

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

func Backoff(base, maxNS, 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 ByteSliceToString

func ByteSliceToString(bs []byte) string

Allocation free conversion from []byte to string (unsafe) Note that the byte slice must not be modified after conversion

func Close

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

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

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

func GrowPtrSlice

func GrowPtrSlice[T any](s []*T, n int) []*T

GrowPtrSlice appends n elements to the slice, each pointing to a newly-allocated T. The resulting slice has length equal to len(s)+n.

It performs at most 2 allocations, regardless of n.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns a slice of keys from any map.

func KeysSorted

func KeysSorted[M ~map[K]V, K cmp.Ordered, V any](m M) []K

KeysSorted returns a slice of keys from any map, sorted in ascending order.

func MustMarshalJSON

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

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

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 NewPtrSlice

func NewPtrSlice[T any](n int) []*T

NewPtrSlice returns a slice of pointers to T with length n, with only 2 allocations performed no matter the size of n. See: https://gist.github.com/CAFxX/e96e8a5c3841d152f16d266a1fe7f8bd#slices-of-pointers

func NumDigitsInt

func NumDigitsInt(n int) int

NumDigitsInt returns the number of digits in n. This is useful for pre-allocating buffers for string conversion.

func NumDigitsUint

func NumDigitsUint(n uint64) int

NumDigitsUint returns the number of digits in n. This is useful for pre-allocating buffers for string conversion.

func PushFIFO

func PushFIFO[T any](buffer chan T, data T, metrics metrics.Metrics, metricName string)

PushFIFO pushes data into a buffered channel without blocking when full, making room by dropping the oldest data. An optional metric can be recorded when data is dropped.

func ReadMaybeCompressedBody

func ReadMaybeCompressedBody(r *http.Request) ([]byte, error)

Note(philipc): Originally taken from server/server.go The DecodingLimitHandler handles validating that the gzip payload is within the allowed max size limit. Thus, in the event of a forged payload size trailer, the worst that can happen is that we waste memory up to the allowed max gzip payload size, but not an unbounded amount of memory, as was potentially possible before.

func Reference

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

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 StringToByteSlice

func StringToByteSlice[T ~string](s T) []byte

Allocation free conversion from ~string to []byte (unsafe) Note that the byte slice must not be modified after conversion

func TimerWithCancel

func TimerWithCancel(delay time.Duration) (*time.Timer, func())

TimerWithCancel exists because of memory leaks when using time.After in select statements. Instead, we now manually create timers, wait on them, and manually free them.

See this for more details: https://www.arangodb.com/2020/09/a-story-of-a-memory-leak-in-go-how-to-properly-use-time-after/

Note: This issue is fixed in Go 1.23, but this fix helps us until then.

Warning: the cancel cannot be done concurrent to reading, everything should work in the same goroutine.

Example:

for retries := 0; true; retries++ {

	...main logic...

	timer, cancel := utils.TimerWithCancel(utils.Backoff(retries))
	select {
	case <-ctx.Done():
		cancel()
		return ctx.Err()
	case <-timer.C:
		continue
	}
}

func Unmarshal

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

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

func UnmarshalJSON

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 Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns a slice of values from any map. Copied from golang.org/x/exp/maps.

func WaitFunc

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

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

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

func NewEnumFlag

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

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

func (*EnumFlag) IsSet

func (f *EnumFlag) IsSet() bool

IsSet will return true if the EnumFlag has been set.

func (*EnumFlag) Set

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

func (f *EnumFlag) String() string

String returns the EnumValue's value as string.

func (*EnumFlag) Type

func (f *EnumFlag) Type() string

Type returns the valid enumeration values.

type Equals

type Equals func(u T, v T) bool

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

type FIFO

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

FIFO represents a simple FIFO queue.

func NewFIFO

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

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

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

func (*FIFO) Pop

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

func (s *FIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*FIFO) Size

func (s *FIFO) Size() int

Size returns the size of the LIFO.

type HashMap

type HashMap = TypedHashMap[T, T]

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.

type Hasher

type Hasher interface {
	Hash() int
}

type HasherMap

type HasherMap[K Hasher, V any] struct {
	// contains filtered or unexported fields
}

HasherMap represents a simpler version of TypedHashMap that uses Hasher's for keys, and requires only an equality function for keys. Ideally we'd have and Equal method for all key types too, and we could get rid of that requirement.

func NewHasherMap

func NewHasherMap[K Hasher, V any](keq func(K, K) bool) *HasherMap[K, V]

NewHasherMap returns a new empty HasherMap.

func (*HasherMap[K, V]) Delete

func (h *HasherMap[K, V]) Delete(k K)

Delete removes the key k.

func (*HasherMap[K, V]) Get

func (h *HasherMap[K, V]) Get(k K) (V, bool)

Get returns the value for k.

func (*HasherMap[K, V]) Iter

func (h *HasherMap[K, V]) Iter(iter func(K, V) bool) bool

Iter invokes the iter function for each element in the HasherMap. 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 (*HasherMap[K, V]) Len

func (h *HasherMap[K, V]) Len() int

Len returns the current size of this HashMap.

func (*HasherMap[K, V]) Put

func (h *HasherMap[K, V]) Put(k K, v V)

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

type Iter

type Iter func(u T) bool

Iter should return true to indicate stop.

type LIFO

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

LIFO represents a simple LIFO queue.

func NewLIFO

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

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

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

func (*LIFO) Pop

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

func (s *LIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*LIFO) Size

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

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

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.

type TypedHashMap

type TypedHashMap[K any, V any] struct {
	// contains filtered or unexported fields
}

TypedHashMap represents a key/value map.

func NewTypedHashMap

func NewTypedHashMap[K any, V any](keq func(K, K) bool, veq func(V, V) bool, khash func(K) int, vhash func(V) int, def V) *TypedHashMap[K, V]

NewTypedHashMap returns a new empty TypedHashMap.

func (*TypedHashMap[K, V]) Copy

func (h *TypedHashMap[K, V]) Copy() *TypedHashMap[K, V]

Copy returns a shallow copy of this HashMap.

func (*TypedHashMap[K, V]) Delete

func (h *TypedHashMap[K, V]) Delete(k K)

Delete removes the key k.

func (*TypedHashMap[K, V]) Equal

func (h *TypedHashMap[K, V]) Equal(other *TypedHashMap[K, V]) 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 (*TypedHashMap[K, V]) Get

func (h *TypedHashMap[K, V]) Get(k K) (V, bool)

Get returns the value for k.

func (*TypedHashMap[K, V]) Hash

func (h *TypedHashMap[K, V]) Hash() int

Hash returns the hash code for this hash map.

func (*TypedHashMap[K, V]) Iter

func (h *TypedHashMap[K, V]) Iter(iter func(K, V) 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 (*TypedHashMap[K, V]) Len

func (h *TypedHashMap[K, V]) Len() int

Len returns the current size of this HashMap.

func (*TypedHashMap[K, V]) Put

func (h *TypedHashMap[K, V]) Put(k K, v V)

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

func (*TypedHashMap[K, V]) String

func (h *TypedHashMap[K, V]) String() string

func (*TypedHashMap[K, V]) Update

func (h *TypedHashMap[K, V]) Update(other *TypedHashMap[K, V]) *TypedHashMap[K, V]

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.

Source Files

backoff.go channel.go close.go compare.go doc.go enumflag.go graph.go hashmap.go json.go maps.go performance.go queue.go read_gzip_body.go time.go wait.go

Directories

PathSynopsis
v1/util/decoding
v1/util/testPackage test contains utilities used in the policy engine's test suite.
Version
v1.4.2 (latest)
Published
May 2, 2025
Platform
linux/amd64
Imports
22 packages
Last checked
4 hours ago

Tools for package owners.