Documentation
¶
Index ¶
- Variables
- func GetTokenKindString(kind TokenKind) string
- type EvaluableExpression
- func (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
- func (this EvaluableExpression) String() string
- func (this EvaluableExpression) ToMongoQuery() (string, error)
- func (this EvaluableExpression) ToSQLQuery() (string, error)
- func (this EvaluableExpression) Tokens() []ExpressionToken
- type ExpressionToken
- type OperatorSymbol
- type TokenKind
Constants ¶
This section is empty.
Variables ¶
var ADDITIVE_MODIFIERS = []OperatorSymbol{ PLUS, MINUS, }
Convenience array that describes all symbols that count as "additive", which is a subset of modifiers that is evaluated last if a sequence of modifiers are used.
var COMPARATOR_SYMBOLS = map[string]OperatorSymbol{ "==": EQ, "!=": NEQ, ">": GT, ">=": GTE, "<": LT, "<=": LTE, "=~": REQ, "!~": NREQ, }
Map of all valid comparators, and their string equivalents. Used during parsing of expressions to determine if a symbol is, in fact, a comparator. Also used during evaluation to determine exactly which comparator is being used.
var DUMMY_PARAMETERS = map[string]interface{}{}
var EXPONENTIAL_MODIFIERS = []OperatorSymbol{ EXPONENT, }
Convenience array that describes all symbols that count as "additive", which is a subset of modifiers that is evaluated first if a sequence of modifiers are used.
var LOGICAL_SYMBOLS = map[string]OperatorSymbol{ "&&": AND, "||": OR, }
Map of all valid logical operators, and their string equivalents. Used during parsing of expressions to determine if a symbol is, in fact, a logical operator. Also used during evaluation to determine exactly which logical operator is being used.
var MODIFIER_SYMBOLS = map[string]OperatorSymbol{ "+": PLUS, "-": MINUS, "*": MULTIPLY, "/": DIVIDE, "%": MODULUS, "^": EXPONENT, }
Map of all valid modifiers, and their string equivalents. Used during parsing of expressions to determine if a symbol is, in fact, a modifier. Also used during evaluation to determine exactly which modifier is being used.
var MULTIPLICATIVE_MODIFIERS = []OperatorSymbol{ MULTIPLY, DIVIDE, MODULUS, }
Convenience array that describes all symbols that count as "additive", which is a subset of modifiers that is evaluated second if a sequence of modifiers are used.
var NUMERIC_COMPARATORS = []OperatorSymbol{ GT, GTE, LT, LTE, }
var PREFIX_MODIFIERS = []OperatorSymbol{ NEGATE, INVERT, }
var PREFIX_SYMBOLS = map[string]OperatorSymbol{ "-": NEGATE, "!": INVERT, }
var STRING_COMPARATORS = []OperatorSymbol{ REQ, NREQ, }
Functions ¶
func GetTokenKindString ¶
GetTokenKindString returns a string that describes the given TokenKind. e.g., when passed the NUMERIC TokenKind, this returns the string "NUMERIC".
Types ¶
type EvaluableExpression ¶
type EvaluableExpression struct { /* Represents the query format used to output dates. Typically only used when creating SQL or Mongo queries from an expression. Defaults to the complete ISO8601 format, including nanoseconds. */ QueryDateFormat string // contains filtered or unexported fields }
EvaluableExpression represents a set of ExpressionTokens which, taken together, represent an arbitrary expression that can be evaluated down into a single value.
func NewEvaluableExpression ¶
func NewEvaluableExpression(expression string) (*EvaluableExpression, error)
Creates a new EvaluableExpression from the given [expression] string. Returns an error if the given expression has invalid syntax.
func (EvaluableExpression) Evaluate ¶
func (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
Evaluate runs the entire expression using the given [parameters]. Each parameter is mapped from a string to a value, such as "foo" = 1.0. If the expression contains a reference to the variable "foo", it will be taken from parameters["foo"].
This function returns errors if the combination of expression and parameters cannot be run, such as if a string parameter is given in an expression that expects it to be a boolean. e.g., "foo == true", where foo is any string. These errors are almost exclusively returned for parameters not being present, or being of the wrong type. Structural problems with the expression (unexpected tokens, unexpected end of expression, etc) are discovered during parsing of the expression in NewEvaluableExpression.
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", Evaluate will return 2.0. e.g., if the expression is "foo + 1" and parameters contains "foo" = 2, Evaluate will return 3.0
func (EvaluableExpression) String ¶
func (this EvaluableExpression) String() string
Returns the original expression used to create this EvaluableExpression.
func (EvaluableExpression) ToMongoQuery ¶ added in v1.2.0
func (this EvaluableExpression) ToMongoQuery() (string, error)
Returns a string representing this expression as if it were written as a Mongo query.
func (EvaluableExpression) ToSQLQuery ¶ added in v1.2.0
func (this EvaluableExpression) ToSQLQuery() (string, error)
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 (this EvaluableExpression) Tokens() []ExpressionToken
Returns an array representing the ExpressionTokens that make up this expression.
type ExpressionToken ¶
type ExpressionToken struct { Kind TokenKind Value interface{} }
Represents a single parsed token.
type OperatorSymbol ¶
type OperatorSymbol int
Represents the valid symbols for operators.
const ( EQ OperatorSymbol = iota NEQ GT LT GTE LTE REQ NREQ AND OR PLUS MINUS MULTIPLY DIVIDE MODULUS EXPONENT NEGATE INVERT )
func (OperatorSymbol) IsModifierType ¶ added in v1.4.0
func (this OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool
Returns true if this operator is contained by the given array of candidate symbols. False otherwise.