expr

package
v0.0.0-...-5253b6b Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCacheSize = 1024

DefaultCacheSize is the default size for the expression cache.

Variables

This section is empty.

Functions

func ExtractEQMatches

func ExtractEQMatches(src string) (map[string]interface{}, error)

ExtractEQMatches extracts equality sub expressions from the match expression. It only extracts `attribute == literal` type equality matches. It returns a list of <attribute name, value> such that if **any** of these comparisons is false, the expression will evaluate to false. These sub expressions can be hoisted out of the match clause and evaluated separately. For example destination.service == "abc" -- Used to index rules by destination service. context.protocol == "tcp" -- Used to filter rules by context

func FuncMap

func FuncMap() map[string]FuncBase

FuncMap provides inventory of available functions.

Types

type AttributeDescriptorFinder

type AttributeDescriptorFinder interface {
	// GetAttribute finds attribute descriptor in the vocabulary. returns nil if not found.
	GetAttribute(name string) *cfgpb.AttributeManifest_AttributeInfo
}

AttributeDescriptorFinder finds attribute descriptors.

type Constant

type Constant struct {
	StrValue string
	Value    interface{}
	Type     dpb.ValueType
}

Constant models a typed constant.

func (*Constant) String

func (c *Constant) String() string

type Evaluator

type Evaluator interface {
	// Eval evaluates given expression using the attribute bag
	Eval(expr string, attrs attribute.Bag) (interface{}, error)

	// Eval evaluates given expression using the attribute bag to a string
	EvalString(expr string, attrs attribute.Bag) (string, error)

	PredicateEvaluator

	TypeChecker
}

Evaluator evaluates an expression written in the implementation defined expression language. It uses attribute.Bag as variable bindings.

func NewCEXLEvaluator

func NewCEXLEvaluator(cacheSize int) (Evaluator, error)

NewCEXLEvaluator returns a new Evaluator of this type.

type Expression

type Expression struct {
	// Oneof the following
	Const *Constant
	Var   *Variable
	Fn    *Function
}

Expression is a simplified expression AST

func Parse

func Parse(src string) (ex *Expression, err error)

Parse parses a given expression to ast.Expression.

func (*Expression) Eval

func (e *Expression) Eval(attrs attribute.Bag, fMap map[string]FuncBase) (interface{}, error)

Eval evaluates the expression given an attribute bag and a function map.

func (*Expression) EvalType

func (e *Expression) EvalType(attrs AttributeDescriptorFinder, fMap map[string]FuncBase) (valueType dpb.ValueType, err error)

EvalType Function an expression using fMap and attribute vocabulary. Returns the type that this expression evaluates to.

func (*Expression) String

func (e *Expression) String() string

String produces postfix version with all operators converted to function names

type Func

type Func interface {
	FuncBase

	// Call performs the function call. It is guaranteed
	// that call will be made with correct arity.
	// may panic.
	Call(attrs attribute.Bag, args []*Expression, fMap map[string]FuncBase) (interface{}, error)
}

Func implements a function call. It needs to know details about Expressions and attribute bag.

type FuncBase

type FuncBase interface {
	// Name uniquely identifies the function.
	Name() string

	// ReturnType specifies the return type of this function.
	ReturnType() config.ValueType

	// ArgTypes specifies the argument types in order expected by the function.
	ArgTypes() []config.ValueType
}

FuncBase defines the interface that every expression function must implement.

type Function

type Function struct {
	Name string
	Args []*Expression
}

Function models a function with multiple parameters 1st arg can be thought of as the receiver.

func (*Function) EvalType

func (f *Function) EvalType(attrs AttributeDescriptorFinder, fMap map[string]FuncBase) (valueType dpb.ValueType, err error)

EvalType Function using fMap and attribute vocabulary. Return static or computed return type if all args have correct type.

func (*Function) String

func (f *Function) String() string

type PredicateEvaluator

type PredicateEvaluator interface {
	// EvalPredicate evaluates given predicate using the attribute bag
	EvalPredicate(expr string, attrs attribute.Bag) (bool, error)
}

PredicateEvaluator evaluates a predicate to true or false

type TypeChecker

type TypeChecker interface {
	// EvalType produces the type of an expression or an error if the type cannot be evaluated.
	// TODO: we probably want to use a golang type rather than pb.ValueType (a proto).
	EvalType(expr string, finder AttributeDescriptorFinder) (pb.ValueType, error)

	// AssertType evaluates the type of expr using the attribute set; if the evaluated type is equal to
	// the expected type we return nil, and return an error otherwise.
	AssertType(expr string, finder AttributeDescriptorFinder, expectedType pb.ValueType) error
}

TypeChecker validates a given expression for type safety.

type Variable

type Variable struct {
	Name string
}

Variable models a variable.

func (*Variable) Eval

func (v *Variable) Eval(attrs attribute.Bag) (interface{}, error)

Eval returns value of the contained variable or error

func (*Variable) String

func (v *Variable) String() string

Jump to

Keyboard shortcuts

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