eval

package
v0.0.2-0...-4ce78c8 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cache = NewCache(cache.NoExpiration, 0)

Cache declares a default cache for expression evaluation with no key experation

View Source
var (

	// ErrEmptyExpression is returned for empty string used as expression input
	ErrEmptyExpression = errors.New("invalid empty expression")
)

Functions

This section is empty.

Types

type Array

type Array struct {
	Pos lexer.Position

	Values []Value `parser:"\"[\" @@ { \",\" @@ } \"]\""`
	Ident  *string `parser:"| @Ident"`
}

Array provides support for array syntax and may contain any valid Values (mixed allowed)

func (*Array) Evaluate

func (a *Array) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type ArrayComparison

type ArrayComparison struct {
	Pos lexer.Position

	Op *string `parser:"( @( \"in\" | \"not\" \"in\" )"`
	// TODO: likely doesn't work with rhs expression
	Array *Array `parser:"@@ )"`
}

ArrayComparison represents syntax for array comparison

type Call

type Call struct {
	Pos lexer.Position

	Name string        `parser:"@Ident"`
	Args []*Expression `parser:"\"(\" [ @@ { \",\" @@ } ] \")\""`
}

Call implements function call syntax

func (*Call) Evaluate

func (c *Call) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type Comparison

type Comparison struct {
	Pos lexer.Position

	Term             *Term             `parser:"@@"`
	ScalarComparison *ScalarComparison `parser:"[ @@"`
	ArrayComparison  *ArrayComparison  `parser:"| @@ ]"`
}

Comparison represents syntax for comparison operations

func (*Comparison) Evaluate

func (c *Comparison) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type Evaluatable

type Evaluatable interface {
	Evaluate(instance *Instance) (interface{}, error)
}

Evaluatable abstracts part of an expression that can be evaluated for an instance

type Expression

type Expression struct {
	Pos lexer.Position

	Comparison *Comparison `parser:"@@"`
	Op         *string     `parser:"[ @( \"|\" \"|\" | \"&\" \"&\" )"`
	Next       *Expression `parser:"  @@ ]"`
}

Expression represents basic expression syntax that can be evaluated for an Instance

func ParseExpression

func ParseExpression(s string) (*Expression, error)

ParseExpression parses Expression from a string

func (*Expression) BoolEvaluate

func (e *Expression) BoolEvaluate(instance *Instance) (bool, error)

BoolEvaluate evaluates an expression for an instance as a boolean value

func (*Expression) Evaluate

func (e *Expression) Evaluate(instance *Instance) (interface{}, error)

Evaluate evaluates an expression for an instance

type ExpressionCache

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

ExpressionCache implements cached parsing for expressions

func NewCache

func NewCache(defaultExpiration, cleanupInterval time.Duration) *ExpressionCache

NewCache returns a new instance of evalCache

func (*ExpressionCache) ParseExpression

func (c *ExpressionCache) ParseExpression(s string) (*Expression, error)

ParseExpression parses Expression from a string

func (*ExpressionCache) ParseIterable

func (c *ExpressionCache) ParseIterable(s string) (*IterableExpression, error)

ParseIterable parses IterableExpression from a string

func (*ExpressionCache) ParsePath

func (c *ExpressionCache) ParsePath(s string) (*PathExpression, error)

ParsePath parses PathExpression from a string

type Function

type Function func(instance *Instance, args ...interface{}) (interface{}, error)

Function describes a function callable for an instance

type FunctionMap

type FunctionMap map[string]Function

FunctionMap describes a map of functions

type Instance

type Instance struct {
	// Instance functions
	Functions FunctionMap
	// Vars defined during evaluation.
	Vars VarMap
}

Instance for evaluation

type InstanceResult

type InstanceResult struct {
	Instance *Instance
	Passed   bool
}

InstanceResult captures an Instance along with the passed or failed status for the result

type IterableComparison

type IterableComparison struct {
	Pos lexer.Position

	Fn               *string           `parser:"@( \"count\" | \"all\" | \"none\" )"`
	Expression       *Expression       `parser:"\"(\" @@ \")\""`
	ScalarComparison *ScalarComparison `parser:"[ @@ ]"`
}

IterableComparison allows evaluating a builtin pseudo-funciion for an iterable expression

type IterableExpression

type IterableExpression struct {
	Pos lexer.Position

	IterableComparison *IterableComparison `parser:"@@"`
	Expression         *Expression         `parser:"| @@"`
}

IterableExpression represents an iterable expration that can be evaluated for an Iterator

func ParseIterable

func ParseIterable(s string) (*IterableExpression, error)

ParseIterable parses IterableExpression from a string

func (*IterableExpression) Evaluate

func (e *IterableExpression) Evaluate(instance *Instance) (bool, error)

Evaluate evaluates an iterable expression for a single instance

func (*IterableExpression) EvaluateIterator

func (e *IterableExpression) EvaluateIterator(it Iterator, global *Instance) (*InstanceResult, error)

EvaluateIterator evaluates an iterable expression for an iterator

type Iterator

type Iterator interface {
	Next() (*Instance, error)
	Done() bool
}

Iterator abstracts iteration over a set of instances for expression evaluation

type PathExpression

type PathExpression struct {
	Pos lexer.Position

	Path       *string     `parser:"@UnixSystemPath"`
	Expression *Expression `parser:"| @@"`
}

PathExpression represents an expression evaluating to a file path or file glob

func ParsePath

func ParsePath(s string) (*PathExpression, error)

ParsePath parses PathExpression from a string

func (*PathExpression) Evaluate

func (e *PathExpression) Evaluate(instance *Instance) (interface{}, error)

Evaluate evaluates a path expression for an instance

type ScalarComparison

type ScalarComparison struct {
	Pos lexer.Position

	Op   *string     `parser:"@( \">\" \"=\" | \"<\" \"=\" | \">\" | \"<\" | \"!\" \"=\" | \"=\" \"=\" | \"=\" \"~\" | \"!\" \"~\" )"`
	Next *Comparison `parser:"  @@"`
}

ScalarComparison represents syntax for scalar comparison

type Term

type Term struct {
	Pos lexer.Position

	Unary *Unary  `parser:"@@"`
	Op    *string `parser:"[ @( \"&\" | \"|\" | \"^\" | \"+\" )"`
	Next  *Term   `parser:"  @@ ]"`
}

Term is an abstract term allowing optional binary bit operation syntax

func (*Term) Evaluate

func (t *Term) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type Unary

type Unary struct {
	Pos lexer.Position

	Op    *string `parser:"( @( \"!\" | \"-\" | \"^\" )"`
	Unary *Unary  `parser:"  @@ )"`
	Value *Value  `parser:"| @@"`
}

Unary is a unary bit operation syntax

func (*Unary) Evaluate

func (u *Unary) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type Value

type Value struct {
	Pos lexer.Position

	Hex           *string     `parser:"  @Hex"`
	Octal         *string     `parser:"| @Octal"`
	Decimal       *int64      `parser:"| @Decimal"`
	String        *string     `parser:"| @String"`
	Call          *Call       `parser:"| @@"`
	Variable      *string     `parser:"| @Ident"`
	Subexpression *Expression `parser:"| \"(\" @@ \")\""`
}

Value provides support for various value types in expression including integers in various form, strings, function calls, variables and subexpressions

func (*Value) Evaluate

func (v *Value) Evaluate(instance *Instance) (interface{}, error)

Evaluate implements Evaluatable interface

type VarMap

type VarMap map[string]interface{}

VarMap describes a map of variables

Jump to

Keyboard shortcuts

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