package util
import "github.com/open-policy-agent/opa/v1/util"
Package util provides generic utilities used throughout the policy engine.
Index ¶
- func BFS(t Traversal, f Iter, u T) bool
- func Backoff(base, maxNS, jitter, factor float64, retries int) time.Duration
- func ByteSliceToString(bs []byte) string
- func Close(resp *http.Response)
- func Compare(a, b interface{}) int
- func DFS(t Traversal, f Iter, u T) bool
- func DefaultBackoff(base, maxNS float64, retries int) time.Duration
- func GrowPtrSlice[T any](s []*T, n int) []*T
- func Keys[M ~map[K]V, K comparable, V any](m M) []K
- func KeysSorted[M ~map[K]V, K cmp.Ordered, V any](m M) []K
- func MustMarshalJSON(x interface{}) []byte
- func MustUnmarshalJSON(bs []byte) interface{}
- func NewJSONDecoder(r io.Reader) *json.Decoder
- func NewPtrSlice[T any](n int) []*T
- func NumDigitsInt(n int) int
- func NumDigitsUint(n uint64) int
- func PushFIFO[T any](buffer chan T, data T, metrics metrics.Metrics, metricName string)
- func ReadMaybeCompressedBody(r *http.Request) ([]byte, error)
- func Reference(x interface{}) *interface{}
- func RoundTrip(x *interface{}) error
- func StringToByteSlice[T ~string](s T) []byte
- func TimerWithCancel(delay time.Duration) (*time.Timer, func())
- func Unmarshal(bs []byte, v interface{}) error
- func UnmarshalJSON(bs []byte, x interface{}) error
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- func WaitFunc(fun func() bool, interval, timeout time.Duration) error
- type EnumFlag
- func NewEnumFlag(defaultValue string, vs []string) *EnumFlag
- func (f *EnumFlag) IsSet() bool
- func (f *EnumFlag) Set(s string) error
- func (f *EnumFlag) String() string
- func (f *EnumFlag) Type() string
- type Equals
- type FIFO
- func NewFIFO(ts ...T) *FIFO
- func (s *FIFO) Peek() (T, bool)
- func (s *FIFO) Pop() (T, bool)
- func (s *FIFO) Push(t T)
- func (s *FIFO) Size() int
- type HashMap
- type Hasher
- type HasherMap
- func NewHasherMap[K Hasher, V any](keq func(K, K) bool) *HasherMap[K, V]
- func (h *HasherMap[K, V]) Delete(k K)
- func (h *HasherMap[K, V]) Get(k K) (V, bool)
- func (h *HasherMap[K, V]) Iter(iter func(K, V) bool) bool
- func (h *HasherMap[K, V]) Len() int
- func (h *HasherMap[K, V]) Put(k K, v V)
- type Iter
- type LIFO
- func NewLIFO(ts ...T) *LIFO
- func (s *LIFO) Peek() (T, bool)
- func (s *LIFO) Pop() (T, bool)
- func (s *LIFO) Push(t T)
- func (s *LIFO) Size() int
- type T
- type Traversal
- type TypedHashMap
- 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]
- func (h *TypedHashMap[K, V]) Copy() *TypedHashMap[K, V]
- func (h *TypedHashMap[K, V]) Delete(k K)
- func (h *TypedHashMap[K, V]) Equal(other *TypedHashMap[K, V]) bool
- func (h *TypedHashMap[K, V]) Get(k K) (V, bool)
- func (h *TypedHashMap[K, V]) Hash() int
- func (h *TypedHashMap[K, V]) Iter(iter func(K, V) bool) bool
- func (h *TypedHashMap[K, V]) Len() int
- func (h *TypedHashMap[K, V]) Put(k K, v V)
- func (h *TypedHashMap[K, V]) String() string
- func (h *TypedHashMap[K, V]) Update(other *TypedHashMap[K, V]) *TypedHashMap[K, V]
Functions ¶
func BFS ¶
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 ¶
Backoff returns a delay with an exponential backoff based on the number of retries. Same algorithm used in gRPC.
func ByteSliceToString ¶
Allocation free conversion from []byte to string (unsafe) Note that the byte slice must not be modified after conversion
func Close ¶
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 ¶
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 ¶
DefaultBackoff returns a delay with an exponential backoff based on the number of retries.
func GrowPtrSlice ¶
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 ¶
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 ¶
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 ¶
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 ¶
NumDigitsInt returns the number of digits in n. This is useful for pre-allocating buffers for string conversion.
func NumDigitsUint ¶
NumDigitsUint returns the number of digits in n. This is useful for pre-allocating buffers for string conversion.
func PushFIFO ¶
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 ¶
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 ¶
Allocation free conversion from ~string to []byte (unsafe) Note that the byte slice must not be modified after conversion
func TimerWithCancel ¶
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 ¶
Unmarshal decodes a YAML, JSON or JSON extension value into the specified type.
func UnmarshalJSON ¶
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 ¶
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 ¶
NewEnumFlag returns a new EnumFlag that has a defaultValue and vs enumerated values.
func (*EnumFlag) IsSet ¶
IsSet will return true if the EnumFlag has been set.
func (*EnumFlag) Set ¶
Set sets the enum value. If s is not a valid enum value, an error is returned.
func (*EnumFlag) String ¶
String returns the EnumValue's value as string.
func (*EnumFlag) Type ¶
Type returns the valid enumeration values.
type Equals ¶
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 ¶
NewFIFO returns a new FIFO queue containing elements ts starting with the left-most argument at the front.
func (*FIFO) Peek ¶
Peek returns the top of the LIFO. If LIFO is empty, returns nil, false.
func (*FIFO) Pop ¶
Pop returns the top of the LIFO and removes it. If LIFO is empty returns nil, false.
func (*FIFO) Push ¶
Push adds a new element onto the LIFO.
func (*FIFO) Size ¶
Size returns the size of the LIFO.
type HashMap ¶
type HashMap = TypedHashMap[T, T]
HashMap represents a key/value map.
func NewHashMap ¶
NewHashMap returns a new empty HashMap.
type Hasher ¶
type Hasher interface { Hash() int }
type HasherMap ¶
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 ¶
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 ¶
Get returns the value for k.
func (*HasherMap[K, V]) Iter ¶
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 ¶
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 ¶
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 ¶
NewLIFO returns a new LIFO queue containing elements ts starting with the left-most argument at the bottom.
func (*LIFO) Peek ¶
Peek returns the top of the LIFO. If LIFO is empty, returns nil, false.
func (*LIFO) Pop ¶
Pop returns the top of the LIFO and removes it. If LIFO is empty returns nil, false.
func (*LIFO) Push ¶
Push adds a new element onto the LIFO.
func (*LIFO) Size ¶
Size returns the size of the LIFO.
type T ¶
type T interface{}
T is a concise way to refer to T.
func DFSPath ¶
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 ¶
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 ¶
Path | Synopsis |
---|---|
v1/util/decoding | |
v1/util/test | Package 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.