search

package
v0.0.0-...-aa18fd0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const Key = "@key"

Key is the special path segment used to represent the name of an attribute.

Variables

This section is empty.

Functions

This section is empty.

Types

type Operator

type Operator int

Operator represents a comparison operator.

const (
	// Cont checks if the attribute is a string that contains the substring given as value.
	Cont Operator = iota

	// Eq check if the attribute is equal to the value.
	Eq

	// Gt checks if the attribute is greater than the value.
	Gt

	// Gte checks if the attribute is greater or equal than the value.
	Gte

	// In checks if the attribute is one of the values.
	In

	// Lt checks if the attribute is less than the value.
	Lt

	// Lte checks if the attribute is less or equal than the value.
	Lte

	// Ncont is the negation of `cont`. It checks if the attribute is a string that does not
	// contain the value.
	Ncont

	// Neq is the negation of `eq`. It checks if the attribute is not equal to the value.
	Neq

	// Nin is the negation of `in`. It checks if the attribute is not one of the values.
	Nin
)

func (Operator) String

func (o Operator) String() string

String generates a string representation of the operator. It panics if used on an unknown operator.

type Path

type Path []string

Path represents the path of an attribute inside a complex data type. Each value of the slice is an attribute name, starting with the outermost field name.

func (Path) Clone

func (p Path) Clone() Path

Clone creates a deep copy of the path.

type PathEvaluator

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

PathEvaluator knows how extract from an object the value of an attribute given its path. Don't create instances of this type directly use the NewPathEvaluator function instead.

func (*PathEvaluator) Evaluate

func (r *PathEvaluator) Evaluate(ctx context.Context, path Path, object any) (result any,
	err error)

Evaluate receives the attribute path and the object and returns the value of that attribute.

type PathEvaluatorBuilder

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

PathEvaluatorBuilder contains the logic and data needed to create attribute path evaluators. Don't create instances of this type directly, use the NewPathEvaluator function instead.

func NewPathEvaluator

func NewPathEvaluator() *PathEvaluatorBuilder

NewPathEvaluator creates a builder that can then be used to configure and create path evaluators.

func (*PathEvaluatorBuilder) Build

func (b *PathEvaluatorBuilder) Build() (result *PathEvaluator, err error)

Build uses the configuration stored in the builder to create a new evaluator.

func (*PathEvaluatorBuilder) SetLogger

func (b *PathEvaluatorBuilder) SetLogger(value *slog.Logger) *PathEvaluatorBuilder

SetLogger sets the logger that the evaluator will use to write log messages. This is mandatory.

type PathsParser

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

PathsParser knows how to parse field paths. Don't create instances of this type directly, use the NewPathsParser function instead.

func (*PathsParser) Parse

func (p *PathsParser) Parse(paths ...string) (result []Path, err error)

Parse parses the given field paths.

type PathsParserBuilder

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

PathsParserBuilder contains the logic and data needed to create field paths parsers. Don't create instances of this type directly, use the NewPathsParser function instead.

func NewPathsParser

func NewPathsParser() *PathsParserBuilder

NewPathsParser creates a builder that can then be used to configure and create path parsers.

func (*PathsParserBuilder) Build

func (b *PathsParserBuilder) Build() (result *PathsParser, err error)

Build uses the configuration stored in the builder to create a new parser.

func (*PathsParserBuilder) SetLogger

func (b *PathsParserBuilder) SetLogger(value *slog.Logger) *PathsParserBuilder

SetLogger sets the logger that the parser will use to write log messages. This is mandatory.

type Projector

type Projector struct {
	// Include is the list of paths that will be included in the result when the projector
	// is evaluated. An empty list means that all paths will be included.
	Include []Path

	// Exclude is the list of paths that will be excluded from the result when the projector
	// is evaluated. An empty list means that no path will be excluded.
	Exclude []Path
}

Projector defines how to remove fields from an object.

func (*Projector) Clone

func (p *Projector) Clone() *Projector

Clone creates a deep copy of the projector.

func (*Projector) Empty

func (p *Projector) Empty() bool

Empty returns true iif the projector has no include or exclude paths.

type ProjectorEvaluator

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

ProjectorEvaluator knows how to evaluate field projection. Don't create instances of this type directly, use the NewProjectionEvaluator function instead.

func (*ProjectorEvaluator) Evaluate

func (e *ProjectorEvaluator) Evaluate(ctx context.Context, projector *Projector,
	object any) (result map[string]any, err error)

Evaluate evaluates the projector on the given object. It returns a map containing only the fields that match the projector.

type ProjectorEvaluatorBuilder

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

ProjectorEvaluatorBuilder contains the logic and data needed to create field projection evaluators. Don't create instances of this type directly, use the NewProjectorEvaluator function instead.

func NewProjectorEvaluator

func NewProjectorEvaluator() *ProjectorEvaluatorBuilder

NewProjectorEvaluator creates a builder that can then be used to configure and create field selector evaluators.

func (*ProjectorEvaluatorBuilder) Build

func (b *ProjectorEvaluatorBuilder) Build() (result *ProjectorEvaluator, err error)

Build uses the configuration stored in the builder to create a new evaluator.

func (*ProjectorEvaluatorBuilder) SetLogger

SetLogger sets the logger that the evaluator will use to write log messages. This is mandatory.

func (*ProjectorEvaluatorBuilder) SetPathEvaluator

func (b *ProjectorEvaluatorBuilder) SetPathEvaluator(
	value func(context.Context, Path, any) (any, error)) *ProjectorEvaluatorBuilder

SetPathEvaluator sets the function that will be used to extract values of attributes from the object. This is mandatory.

The path evaluator function receives the attribute path and the object and should return the value of that attribute. For example, for a simple struct like this:

type Person struct {
	Name string
	Age  int
}

The path evaluator function could be like this:

func personPathEvaluator(ctx context.Context, path Path, object any) (result any, err error) {
	person, ok := object.(*Person)
	if !ok {
		err = fmt.Errorf("expected person, but got '%T'", object)
		return
	}
	if len(path) != 1 {
		err = fmt.Errorf("expected exactly one path segment, but got %d", len(path))
		return
	}
	segment := path[0]
	switch segment {
	case "name":
		result = person.Name
	case "age":
		result = person.Age
	default:
		err = fmt.Errorf(
			"unknown attribute '%', valid attributes are 'name' and 'age'",
			segment,
		)
	}
	return

The path evaluator function should return an error if the object isn't of the expected type, of if the path doesn't correspond to a valid attribute.

The path evaluator function should return nil if the path corresponds to a valid optional attribute that hasn't a value.

type ProjectorParser

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

ProjectorParser knows how to parse field selectors. Don't create instances of this type directly, use the NewProjectorParser function instead.

func (*ProjectorParser) Parse

func (p *ProjectorParser) Parse(include, exclude string) (result *Projector, err error)

Parse parses the give field selector. If it succeeds it returns the object selector. If it fails it returns an error.

type ProjectorParserBuilder

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

ProjectorParserBuilder contains the logic and data needed to create field selection parsers. Don't create instances of this type directly, use the NewProjectorParser function instead.

func NewProjectorParser

func NewProjectorParser() *ProjectorParserBuilder

NewProjectorParser creates a builder that can then be used to configure and create field selector parsers.

func (*ProjectorParserBuilder) Build

func (b *ProjectorParserBuilder) Build() (result *ProjectorParser, err error)

Build uses the configuration stored in the builder to create a new parser.

func (*ProjectorParserBuilder) SetLogger

SetLogger sets the logger that the parser will use to write log messages. This is mandatory.

type Selector

type Selector struct {
	Terms []*Term
}

Selector represents an attribute-based filter expression as defined in section 5.2 of ETSI GS NFV-SOL 013.

func (*Selector) String

func (e *Selector) String() string

String generates a string representation of the selector.

type SelectorEvaluator

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

SelectorEvaluator knows how to evaluate filter expressions. Don't create instances of this type directly, use the NewSelectorEvaluator function instead.

func (*SelectorEvaluator) Evaluate

func (e *SelectorEvaluator) Evaluate(ctx context.Context, selector *Selector,
	object any) (result bool, err error)

Evaluate evaluates the filter expression on the given object. It returns true if the object matches the expression, and false otherwise.

type SelectorEvaluatorBuilder

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

SelectorEvaluatorBuilder contains the logic and data needed to create filter expression evaluators. Don't create instances of this type directly, use the NewSelectorEvaluator function instead.

func NewSelectorEvaluator

func NewSelectorEvaluator() *SelectorEvaluatorBuilder

NewSelectorEvaluator creates a builder that can then be used to configure and create expression filter evaluators.

func (*SelectorEvaluatorBuilder) Build

func (b *SelectorEvaluatorBuilder) Build() (result *SelectorEvaluator, err error)

Build uses the configuration stored in the builder to create a new evaluator.

func (*SelectorEvaluatorBuilder) SetLogger

SetLogger sets the logger that the evaluator will use to write log messages. This is mandatory.

func (*SelectorEvaluatorBuilder) SetPathEvaluator

func (b *SelectorEvaluatorBuilder) SetPathEvaluator(
	value func(context.Context, Path, any) (any, error)) *SelectorEvaluatorBuilder

SetPathEvaluator sets the function that will be used to extract values of attributes from the object. This is mandatory.

The path evaluator function receives the attribute path and the object and should return the value of that attribute. For example, for a simple struct like this:

type Person struct {
	Name string
	Age  int
}

The path evaluator function could be like this:

func personPathEvaluator(ctx context.Context, path []string, object any) (result any, err error) {
	person, ok := object.(*Person)
	if !ok {
		err = fmt.Errorf("expected person, but got '%T'", object)
		return
	}
	if len(path) != 1 {
		err = fmt.Errorf("expected exactly one path segment, but got %d", len(path))
		return
	}
	segment := path[0]
	switch segment {
	case "name":
		result = person.Name
	case "age":
		result = person.Age
	default:
		err = fmt.Errorf(
			"unknown attribute '%', valid attributes are 'name' and 'age'",
			segment,
		)
	}
	return

The path evaluator function should return an error if the object isn't of the expected type, of if the path doesn't correspond to a valid attribute.

The path evaluator function should return nil if the path corresponds to a valid optional attribute that hasn't a value.

type SelectorParser

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

SelectorParser knows how to parse filter expressions. Don't create instances of this type directly, use the NewSelectorParser function instead.

func (*SelectorParser) Parse

func (p *SelectorParser) Parse(text string) (selector *Selector, err error)

Parse parses the give filter expression. If it succeeds it returns the object representing that expression. If it fails it returns an error.

type SelectorParserBuilder

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

SelectorParserBuilder contains the logic and data needed to create filter expression parsers. Don't create instances of this type directly, use the NewSelectorParser function instead.

func NewSelectorParser

func NewSelectorParser() *SelectorParserBuilder

NewSelectorParser creates a builder that can then be used to configure and create expression filter // parsers. The builder can be reused to create multiple parsers with identical configuration.

func (*SelectorParserBuilder) Build

func (b *SelectorParserBuilder) Build() (result *SelectorParser, err error)

Build uses the configuration stored in the builder to create a new parser.

func (*SelectorParserBuilder) SetLogger

SetLogger sets the logger that the parser will use to write log messages. This is mandatory.

type Term

type Term struct {
	// Operator is the operator that used to compare the attribute to the value.
	Operator Operator

	// Path is the path of the attribute. This will always contain at least one segment. Each
	// segment is the name of the attribute. For example in a complex object like this:
	//
	//	{
	//		"extensions": {
	//			"user": "myuser",
	//			"password": "mypassword"
	//		}
	//	}
	//
	// The path to the attribute containing the password will be:
	//
	//	[]string{
	//		"extensions",
	//		"password",
	//	}
	Path []string

	// Values is the list of values that the attribute will be compared to. It will contain
	// only one value for unary operators like `eq` or `neq`, and multiple values for
	// operators like `in` or `nin`.
	Values []any
}

Term is each of the terms that compose a elector The selector will match an object when all the terms match it -in other words, terms are connected by the and logical directive.

func (*Term) String

func (t *Term) String() string

String generates a string representation of the term.

Jump to

Keyboard shortcuts

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