multimatcher

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expr

type Expr interface {
	Evaluate(Labels) bool
}

Expr represents an expression that returns a bool decision based on labels.

Note that even though this interface is public, it's recommended to use Exprs from this package, as multimatcher can optimize them better.

(we're not doing it right now, but in future we might want to perform some optimizations like common expression elimination or running all regexes in one go (e.g. with engine like hyperscan)).

func All

func All(exprs []Expr) Expr

All returns an expression that checks if all the given expressions are true.

func Any

func Any(exprs []Expr) Expr

Any returns an expression that checks if any of the given expressions are true.

func LabelEquals

func LabelEquals(label string, value string) Expr

LabelEquals returns an expression that checks if the given label is exactly equal to the value.

func LabelExists

func LabelExists(label string) Expr

LabelExists returns an expression that checks if the given label exists.

func LabelMatchesRegex

func LabelMatchesRegex(key string, pattern string) (Expr, error)

LabelMatchesRegex compiles a regex and returns an expression that checks if the given label contains any match of the regex.

func Not

func Not(expr Expr) Expr

Not returns an expression that negates the given expression.

type Labels

type Labels map[string]string

Labels is a map from label to value.

type MatchCallback

type MatchCallback[ResultCollection any] func(ResultCollection) ResultCollection

MatchCallback is called on entry in case of successful match. It should modify result collection and return changed one (e.g. by appending some object).

func Appender

func Appender[T any](value T) MatchCallback[[]T]

Appender can be used to create a simple MatchCallback in case the ResultCollection is []T.

type MultiMatcher

type MultiMatcher[Key comparable, ResultCollection any] struct {
	// contains filtered or unexported fields
}

MultiMatcher is a database of items (entries) and their corresponding LabelSelector. Provides APIs to Add/Remove entries, Match against labels.

Key is the type that identifies entries – used in adding / deleting, e.g. string. ResultCollection is a collection type that will be used in collecting results from Match, e.g. slice. Zero value of the ResultCollection should represent an empty one.

func New

func New[Key comparable, ResultCollection any]() *MultiMatcher[Key, ResultCollection]

New returns a new multi matcher object.

func (*MultiMatcher[Key, ResultCollection]) AddEntry

func (mm *MultiMatcher[Key, ResultCollection]) AddEntry(key Key, matchExpr Expr, mc MatchCallback[ResultCollection]) error

AddEntry adds an entry to the MultiMatcher.

Key is an arbitrary key to map this entry – should be unique, can be used to remove the entry. MatchCallback is a callback used by Match in case of successful match. The entry is considered matching if matchExpr.Evaluate(labels) returns true.

func (*MultiMatcher[_, _]) Length

func (mm *MultiMatcher[_, _]) Length() int

Length returns number of entries in the MultiMatcher.

func (*MultiMatcher[_, ResultCollection]) Match

func (mm *MultiMatcher[_, ResultCollection]) Match(labels Labels) ResultCollection

Match runs matching against labels and returns a collection of results.

Starting with empty ResultCollection, calls MatchCallback for every entry which matches given labels, and returns resulting ResultCollection.

func (*MultiMatcher[Key, _]) RemoveEntry

func (mm *MultiMatcher[Key, _]) RemoveEntry(key Key) error

RemoveEntry removes an entry in MultiMatcher at key.

Jump to

Keyboard shortcuts

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