Documentation ¶
Index ¶
- Variables
- func MarshalJSON()
- func Random(n int) bool
- type EvaluableExpression
- func NewEvaluableExpression(expression string) (*EvaluableExpression, error)
- func NewEvaluableExpressionFromAST(ast *EvaluationTree) (expr *EvaluableExpression, err error)
- func NewEvaluableExpressionFromASTWithFunctions(ast *EvaluationTree, functions map[string]ExpressionFunction) (expr *EvaluableExpression, err error)
- func NewEvaluableExpressionFromJSON(ast []byte) (expr *EvaluableExpression, err error)
- func NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpression, error)
- func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction) (*EvaluableExpression, error)
- func (expr EvaluableExpression) Eval(parameters Parameters) (interface{}, error)
- func (expr EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
- func (expr EvaluableExpression) String() string
- func (expr EvaluableExpression) ToSQLQuery() (string, error)
- func (expr EvaluableExpression) Tokens() []ExpressionToken
- func (expr EvaluableExpression) Vars() []string
- type EvaluationParameter
- type EvaluationTree
- type ExpressionFunction
- type ExpressionToken
- type Function
- type MapParameters
- type OperatorSymbol
- type Parameters
- type Reference
- type TokenKind
Constants ¶
This section is empty.
Variables ¶
var (
Builtins = map[string]ExpressionFunction{
"ageAt": ageAt,
"after": after,
"before": before,
"empty": empty,
"next": next,
"notnull": notnull,
"now": now,
"null": null,
"one": one,
"random": random,
"round": round,
}
)
var DUMMY_PARAMETERS = MapParameters(map[string]interface{}{})
Functions ¶
Types ¶
type EvaluableExpression ¶
type EvaluableExpression struct { QueryDateFormat string ChecksTypes bool // contains filtered or unexported fields }
EvaluableExpression represents a set of ExpressionTokens which, taken together, are an expression that can be evaluated down into a single value.
func NewEvaluableExpression ¶
func NewEvaluableExpression(expression string) (*EvaluableExpression, error)
NewEvaluableExpression parses a new EvaluableExpression from the given [expression] string. Returns an error if the given expression has invalid syntax.
func NewEvaluableExpressionFromAST ¶ added in v1.1.0
func NewEvaluableExpressionFromAST(ast *EvaluationTree) (expr *EvaluableExpression, err error)
NewEvaluableExpressionFromAST constructs an EvaluableExpression from a JSON object representation of an Abstract Syntax Tree [ast] This is useful in cases where an outside processor is generating an AST.
func NewEvaluableExpressionFromASTWithFunctions ¶ added in v1.1.0
func NewEvaluableExpressionFromASTWithFunctions(ast *EvaluationTree, functions map[string]ExpressionFunction) (expr *EvaluableExpression, err error)
NewEvaluableExpressionFromASTWithFunctions constructs an EvaluableExpression from a JSON object representation of an Abstract Syntax Tree [ast] This is useful in cases where an outside processor is generating an AST.
func NewEvaluableExpressionFromJSON ¶ added in v1.1.0
func NewEvaluableExpressionFromJSON(ast []byte) (expr *EvaluableExpression, err error)
NewEvaluableExpressionFromJSON constructs an EvaluableExpression from a JSON object representation of an Abstract Syntax Tree [ast] This is useful in cases where an outside processor is generating an AST.
func NewEvaluableExpressionFromTokens ¶
func NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpression, error)
NewEvaluableExpressionFromTokens constructs an EvaluableExpression from an already-tokenized expression [tokens]. This is useful in cases where you may be generating an expression automatically, or using some other parser (e.g., to parse from a query language)
func NewEvaluableExpressionWithFunctions ¶
func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction) (*EvaluableExpression, error)
NewEvaluableExpressionWithFunctions Similar to NewEvaluableExpression, except enables the use of user-defined functions. Functions passed into this will be available to the expression.
func (EvaluableExpression) Eval ¶
func (expr EvaluableExpression) Eval(parameters Parameters) (interface{}, error)
Eval runs the entire expression using the given [parameters]. e.g., If the expression contains a reference to the variable "foo", it will be taken from `parameters.Get("foo")`.
This function returns errors if the combination of expression and parameters cannot be run, such as if a variable in the expression is not present in [parameters].
In all non-error circumstances, this returns the single value result of the expression and parameters given. e.g., if the expression is "1 + 1", this will return 2.0. e.g., if the expression is "foo + 1" and parameters contains "foo" = 2, this will return 3.0
func (EvaluableExpression) Evaluate ¶
func (expr EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
Evaluate Same as `Eval`, but automatically wraps a map of parameters into a `eval.Parameters` structure.
func (EvaluableExpression) String ¶
func (expr EvaluableExpression) String() string
String Returns the original expression used to create this EvaluableExpression.
func (EvaluableExpression) ToSQLQuery ¶
func (expr EvaluableExpression) ToSQLQuery() (string, error)
ToSQLQuery returns a string representing this expression as if it were written in SQL. This function assumes that all parameters exist within the same table, and that the table essentially represents a serialized object of some sort (e.g., hibernate). If your data model is more normalized, you may need to consider iterating through each actual token given by `Tokens()` to create your query.
Boolean values are considered to be "1" for true, "0" for false.
Times are formatted according to this.QueryDateFormat.
func (EvaluableExpression) Tokens ¶
func (expr EvaluableExpression) Tokens() []ExpressionToken
Tokens returns an array representing the ExpressionTokens that make up this expression.
func (EvaluableExpression) Vars ¶
func (expr EvaluableExpression) Vars() []string
Vars returns an array representing the variables contained in this EvaluableExpression.
type EvaluationParameter ¶ added in v1.1.0
type EvaluationParameter struct { Name string Value interface{} }
type EvaluationTree ¶ added in v1.1.0
type EvaluationTree struct {
// contains filtered or unexported fields
}
func (*EvaluationTree) MarshalJSON ¶ added in v1.1.0
func (t *EvaluationTree) MarshalJSON() (data []byte, err error)
MarshalJSON marshals evaluation tree [t] returning its JSON representation in [data]
func (*EvaluationTree) String ¶ added in v1.1.0
func (t *EvaluationTree) String() string
func (*EvaluationTree) UnmarshalJSON ¶ added in v1.1.0
func (t *EvaluationTree) UnmarshalJSON(data []byte) error
UnmarshalJSON unmarshals a JSON evaluation tree representation [data] to [t]
type ExpressionFunction ¶
type ExpressionFunction func(arguments ...interface{}) (interface{}, error)
ExpressionFunction represents a function that can be called from within an expression. This method must return an error if, for any reason, it is unable to produce exactly one unambiguous result. An error returned will halt execution of the expression.
type ExpressionToken ¶
type ExpressionToken struct { Kind TokenKind Value interface{} }
ExpressionToken represents a single parsed token.
type MapParameters ¶
type MapParameters map[string]interface{}
func (MapParameters) Get ¶
func (p MapParameters) Get(name string) (interface{}, error)
type OperatorSymbol ¶
type OperatorSymbol int
OperatorSymbol represents the valid symbols for operators.
const ( VALUE OperatorSymbol = iota LITERAL NOOP EQ NEQ GT LT GTE LTE REQ NREQ IN AND OR PLUS MINUS BITWISE_AND BITWISE_OR BITWISE_XOR BITWISE_LSHIFT BITWISE_RSHIFT MULTIPLY DIVIDE MODULUS EXPONENT NEGATE INVERT BITWISE_NOT TERNARY_TRUE TERNARY_FALSE COALESCE FUNCTIONAL ACCESS SEPARATE CONTAINS_IN ENDSWITH_IN STARTSWITH_IN )
func Symbol ¶ added in v1.1.0
func Symbol(symbol string) (op OperatorSymbol, err error)
func (OperatorSymbol) IsModifierType ¶
func (sym OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool
IsModifierType returns true if this operator is contained by the given array of candidate symbols. False otherwise.
func (OperatorSymbol) String ¶
func (sym OperatorSymbol) String() string
String is generally used when formatting type check errors. We could store the stringified symbol somewhere else and not require a duplicated codeblock to translate OperatorSymbol to string, but that would require more memory, and another field somewhere. Adding operators is rare enough that we just stringify it here instead.
type Parameters ¶
type Parameters interface { // Get gets the parameter of the given name, or an error if the parameter is unavailable. // Failure to find the given parameter should be indicated by returning an error. Get(name string) (interface{}, error) }
Parameters is a collection of named parameters that can be used by an EvaluableExpression to retrieve parameters when an expression tries to use them.
type Reference ¶ added in v1.1.0
type Reference struct { Name string `json:"name"` Type string `json:"type"` Nullable bool `json:"nullable"` References []Reference `json:"refs"` }
func References ¶ added in v1.1.0
References constructs a Reference object representing the valid references for [x]
Source Files ¶
- EvaluableExpression.go
- EvaluableExpression_sql.go
- ExpressionToken.go
- OperatorSymbol.go
- TokenKind.go
- astEval.go
- builtins.go
- evaluationTree.go
- expressionFunctions.go
- expressionOutputStream.go
- lexerState.go
- lexerStream.go
- parameter.go
- parameters.go
- parsing.go
- references.go
- sanitizedParameters.go
- stagePlanner.go
- tokenStream.go