opa: github.com/open-policy-agent/opa/util Index | Files | Directories

package util

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

Package util provides generic utilities used throughout the policy engine.

Index

Package Files

backoff.go close.go compare.go doc.go enumflag.go graph.go hashmap.go json.go queue.go

func BFS Uses

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 Uses

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 Uses

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 Uses

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 < 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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func UnmarshalJSON Uses

func UnmarshalJSON(bs []byte, x interface{}) (err 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.

type EnumFlag Uses

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

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

func NewEnumFlag Uses

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

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

func (*EnumFlag) IsSet Uses

func (f *EnumFlag) IsSet() bool

IsSet will return true if the EnumFlag has been set.

func (*EnumFlag) Set Uses

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 Uses

func (f *EnumFlag) String() string

String returns the EnumValue's value as string.

func (*EnumFlag) Type Uses

func (f *EnumFlag) Type() string

Type returns the valid enumeration values.

type Equals Uses

type Equals func(u T, v T) bool

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

type FIFO Uses

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

FIFO represents a simple FIFO queue.

func NewFIFO Uses

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 Uses

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

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

func (*FIFO) Pop Uses

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 Uses

func (s *FIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*FIFO) Size Uses

func (s *FIFO) Size() int

Size returns the size of the LIFO.

type HashMap Uses

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

HashMap represents a key/value map.

func NewHashMap Uses

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

NewHashMap returns a new empty HashMap.

func (*HashMap) Copy Uses

func (h *HashMap) Copy() *HashMap

Copy returns a shallow copy of this HashMap.

func (*HashMap) Delete Uses

func (h *HashMap) Delete(k T)

Delete removes the the key k.

func (*HashMap) Equal Uses

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 Uses

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

Get returns the value for k.

func (*HashMap) Hash Uses

func (h *HashMap) Hash() int

Hash returns the hash code for this hash map.

func (*HashMap) Iter Uses

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 Uses

func (h *HashMap) Len() int

Len returns the current size of this HashMap.

func (*HashMap) Put Uses

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 Uses

func (h *HashMap) String() string

func (*HashMap) Update Uses

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 Uses

type Iter func(u T) bool

Iter should return true to indicate stop.

type LIFO Uses

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

LIFO represents a simple LIFO queue.

func NewLIFO Uses

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 Uses

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

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

func (*LIFO) Pop Uses

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 Uses

func (s *LIFO) Push(t T)

Push adds a new element onto the LIFO.

func (*LIFO) Size Uses

func (s *LIFO) Size() int

Size returns the size of the LIFO.

type T Uses

type T interface{}

T is a concise way to refer to T.

func DFSPath Uses

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 Uses

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

PathSynopsis
testPackage test contains utilities used in the policy engine's test suite.

Package util imports 13 packages (graph) and is imported by 95 packages. Updated 2020-04-06. Refresh now. Tools for package owners.