func Evaluate(expression string, parameter interface{}, opts ...Language) (interface{}, error)
Evaluate given parameter with given expression in gval full language
▹ Example
▹ Example (Accessor)
▹ Example (Arithmetic)
▹ Example (Array)
▹ Example (ComplexAccessor)
▹ Example (DateComparison)
▹ Example (Encoding)
▹ Example (FlatAccessor)
▹ Example (Float64)
▹ Example (Jsonpath)
▹ Example (NestedAccessor)
▹ Example (NestedParameter)
▹ Example (String)
▹ Example (Strlen)
func EvaluateWithContext(c context.Context, expression string, parameter interface{}, opts ...Language) (interface{}, error)
Evaluate given parameter with given expression in gval full language using a context
Evaluable evaluates given parameter
type Evaluable func(c context.Context, parameter interface{}) (interface{}, error)
▹ Example
func (e Evaluable) EvalBool(c context.Context, parameter interface{}) (bool, error)
EvalBool evaluates given parameter to a bool
▹ Example
func (e Evaluable) EvalFloat64(c context.Context, parameter interface{}) (float64, error)
EvalFloat64 evaluates given parameter to a float64
func (e Evaluable) EvalInt(c context.Context, parameter interface{}) (int, error)
EvalInt evaluates given parameter to an int
▹ Example
func (e Evaluable) EvalString(c context.Context, parameter interface{}) (string, error)
EvalString evaluates given parameter to a string
func (e Evaluable) IsConst() bool
IsConst returns if the Evaluable is a Parser.Const() value
Evaluables is a slice of Evaluable.
type Evaluables []Evaluable
func (evs Evaluables) EvalStrings(c context.Context, parameter interface{}) ([]string, error)
EvalStrings evaluates given parameter to a string slice
Language is an expression language
type Language struct {
// contains filtered or unexported fields
}
▹ Example
func Arithmetic() Language
Arithmetic contains base, plus(+), minus(-), divide(/), power(**), negative(-) and numerical order (<=,<,>,>=)
Arithmetic operators expect float64 operands. Called with unfitting input, they try to convert the input to float64. They can parse strings and convert any type of int or float.
func Base() Language
Base contains equal (==) and not equal (!=), perentheses and general support for variables, constants and functions It contains true, false, (floating point) number, string ("" or “) and char (”) constants
func Bitmask() Language
Bitmask contains base, bitwise and(&), bitwise or(|) and bitwise not(^).
Bitmask operators expect float64 operands. Called with unfitting input they try to convert the input to float64. They can parse strings and convert any type of int or float.
func Constant(name string, value interface{}) Language
Constant returns a Language with given constant
func DecimalArithmetic() Language
DecimalArithmetic contains base, plus(+), minus(-), divide(/), power(**), negative(-) and numerical order (<=,<,>,>=)
DecimalArithmetic operators expect decimal.Decimal operands (github.com/shopspring/decimal) and are used to calculate money/decimal rather than floating point calculations. Called with unfitting input, they try to convert the input to decimal.Decimal. They can parse strings and convert any type of int or float.
func DefaultExtension(ext func(context.Context, *Parser) (Evaluable, error)) Language
DefaultExtension is a language that runs the given function if no other prefix matches.
func Full(extensions ...Language) Language
Full is the union of Arithmetic, Bitmask, Text, PropositionalLogic, and Json
Operator in: a in b is true iff value a is an element of array b Operator ??: a ?? b returns a if a is not false or nil, otherwise n Operator ?: a ? b : c returns b if bool a is true, otherwise b
Function Date: Date(a) parses string a. a must match RFC3339, ISO8601, ruby date, or unix date
func Function(name string, function interface{}) Language
Function returns a Language with given function. Function has no conversion for input types.
If the function returns an error it must be the last return parameter.
If the function has (without the error) more then one return parameter, it returns them as []interface{}.
func Ident() Language
Ident contains support for variables and functions.
func InfixBoolOperator(name string, f func(a, b bool) (interface{}, error)) Language
InfixBoolOperator for two bool values.
func InfixDecimalOperator(name string, f func(a, b decimal.Decimal) (interface{}, error)) Language
InfixDecimalOperator for two decimal values.
func InfixEvalOperator(name string, f func(a, b Evaluable) (Evaluable, error)) Language
InfixEvalOperator operates on the raw operands. Therefore it cannot be combined with operators for other operand types.
func InfixNumberOperator(name string, f func(a, b float64) (interface{}, error)) Language
InfixNumberOperator for two number values.
func InfixOperator(name string, f func(a, b interface{}) (interface{}, error)) Language
InfixOperator for two arbitrary values.
func InfixShortCircuit(name string, f func(a interface{}) (interface{}, bool)) Language
InfixShortCircuit operator is called after the left operand is evaluated.
func InfixTextOperator(name string, f func(a, b string) (interface{}, error)) Language
InfixTextOperator for two text values.
func Init(ext func(context.Context, *Parser) (Evaluable, error)) Language
Init is a language that does no parsing, but invokes the given function when parsing starts. It is incumbent upon the function to call ParseExpression to continue parsing.
This function can be used to customize the parser settings, such as whitespace or ident behavior.
func JSON() Language
JSON contains json objects ({string:expression,...}) and json arrays ([expression, ...])
func NewLanguage(bases ...Language) Language
NewLanguage returns the union of given Languages as new Language.
func Parentheses() Language
Parentheses contains support for parentheses.
func PostfixOperator(name string, ext func(context.Context, *Parser, Evaluable) (Evaluable, error)) Language
PostfixOperator extends a Language.
func Precedence(name string, operatorPrecendence uint8) Language
Precedence of operator. The Operator with higher operatorPrecedence is evaluated first.
func PrefixExtension(r rune, ext func(context.Context, *Parser) (Evaluable, error)) Language
PrefixExtension extends a Language
func PrefixMetaPrefix(r rune, ext func(context.Context, *Parser) (call string, alternative func() (Evaluable, error), err error)) Language
PrefixMetaPrefix chooses a Prefix to be executed
func PrefixOperator(name string, e Evaluable) Language
PrefixOperator returns a Language with given prefix
func PropositionalLogic() Language
PropositionalLogic contains base, not(!), and (&&), or (||) and Base.
Propositional operator expect bool operands. Called with unfitting input they try to convert the input to bool. Numbers other than 0 and the strings "TRUE" and "true" are interpreted as true. 0 and the strings "FALSE" and "false" are interpreted as false.
func Text() Language
Text contains base, lexical order on strings (<=,<,>,>=), regex match (=~) and regex not match (!~)
func VariableSelector(selector func(path Evaluables) Evaluable) Language
VariableSelector returns a Language which uses given variable selector. It must be combined with a Language that uses the vatiable selector. E.g. gval.Base().
▹ Example
func (l Language) Evaluate(expression string, parameter interface{}) (interface{}, error)
Evaluate given parameter with given expression
func (l Language) EvaluateWithContext(c context.Context, expression string, parameter interface{}) (interface{}, error)
Evaluate given parameter with given expression using context
func (l Language) NewEvaluable(expression string) (Evaluable, error)
NewEvaluable returns an Evaluable for given expression in the specified language
func (l Language) NewEvaluableWithContext(c context.Context, expression string) (Evaluable, error)
NewEvaluableWithContext returns an Evaluable for given expression in the specified language using context
Parser parses expressions in a Language into an Evaluable
type Parser struct { Language // contains filtered or unexported fields }
func (p *Parser) Camouflage(unit string, expected ...rune)
Camouflage rewind the last Scan(). The Parser holds the camouflage error until the next Scan() Do not call Rewind() on a camouflaged Parser
func (*Parser) Const(value interface{}) Evaluable
Const Evaluable represents given constant
func (p *Parser) Expected(unit string, expected ...rune) error
Expected returns an error signaling an unexpected Scan() result
func (p *Parser) Next() rune
Next reads and returns the next Unicode character. It returns EOF at the end of the source. Do not call Next() on a camouflaged Parser
func (p *Parser) ParseExpression(c context.Context) (eval Evaluable, err error)
ParseExpression scans an expression into an Evaluable.
func (p *Parser) ParseNextExpression(c context.Context) (eval Evaluable, err error)
ParseNextExpression scans the expression ignoring following operators
func (p *Parser) ParseSublanguage(c context.Context, l Language) (Evaluable, error)
ParseSublanguage sets the next language for this parser to parse and calls its initialization function, usually ParseExpression.
▹ Example
func (p *Parser) Peek() rune
Peek returns the next Unicode character in the source without advancing the scanner. It returns EOF if the scanner's position is at the last character of the source. Do not call Peek() on a camouflaged Parser
func (p *Parser) Scan() rune
Scan reads the next token or Unicode character from source and returns it. It only recognizes tokens t for which the respective Mode bit (1<<-t) is set. It returns scanner.EOF at the end of the source.
func (p *Parser) SetIsIdentRuneFunc(fn func(ch rune, i int) bool)
SetIsIdentRuneFunc sets the function that matches ident characters in the underlying scanner.
func (p *Parser) SetMode(mode uint)
SetMode sets the tokens that the underlying scanner will match.
func (p *Parser) SetWhitespace(chars ...rune)
SetWhitespace sets the behavior of the whitespace matcher. The given characters must be less than or equal to 0x20 (' ').
func (p *Parser) TokenText() string
TokenText returns the string corresponding to the most recently scanned token. Valid after calling Scan().
func (p *Parser) Var(path ...Evaluable) Evaluable
Var Evaluable represents value at given path. It supports with default language VariableSelector:
map[interface{}]interface{}, map[string]interface{} and []interface{} and via reflect struct fields, struct methods, slices and map with int or string key.
Selector allows for custom variable selection from structs
Return value is again handled with variable() until end of the given path
type Selector interface { SelectGVal(c context.Context, key string) (interface{}, error) }
▹ Example