Documentation ¶
Index ¶
- Variables
- func GetTokenKindString(kind TokenKind) string
- type EvaluableExpression
- func (this EvaluableExpression) Eval(parameters Parameters) (interface{}, error)
- func (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
- func (this EvaluableExpression) String() string
- func (this EvaluableExpression) ToSQLQuery() (string, error)
- func (this EvaluableExpression) Tokens() []ExpressionToken
- type ExpressionToken
- type MapParameters
- type OperatorSymbol
- type Parameters
- type TokenKind
Constants ¶
This section is empty.
Variables ¶
var ADDITIVE_MODIFIERS = []OperatorSymbol{ PLUS, MINUS, }
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 = MapParameters(map[string]interface{}{})
var EXPONENTIAL_MODIFIERS = []OperatorSymbol{ EXPONENT, }
var LOGICAL_SYMBOLS = map[string]OperatorSymbol{ "&&": AND, "||": OR, }
var MODIFIER_SYMBOLS = map[string]OperatorSymbol{ "+": PLUS, "-": MINUS, "*": MULTIPLY, "/": DIVIDE, "%": MODULUS, "^": EXPONENT, }
var MULTIPLICATIVE_MODIFIERS = []OperatorSymbol{ MULTIPLY, DIVIDE, MODULUS, }
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, }
var TERNARY_SYMBOLS = map[string]OperatorSymbol{ "?": TERNARY_TRUE, ":": TERNARY_FALSE, }
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, are an expression that can be evaluated down into a single value.
func NewEvaluableExpression ¶
func NewEvaluableExpression(expression string) (*EvaluableExpression, error)
Parses a new EvaluableExpression from the given [expression] string. Returns an error if the given expression has invalid syntax.
func (EvaluableExpression) Eval ¶ added in v1.5.0
func (this EvaluableExpression) Eval(parameters Parameters) (interface{}, error)
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 (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error)
Same as `Eval`, but automatically wraps a map of parameters into a `govalute.Parameters` structure.
func (EvaluableExpression) String ¶
func (this EvaluableExpression) String() string
Returns the original expression used to create this EvaluableExpression.
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 MapParameters ¶ added in v1.5.0
type MapParameters map[string]interface{}
MapParameters is an implementation of the Parameters interface using a map.
func (MapParameters) Get ¶ added in v1.5.0
func (p MapParameters) Get(name string) (interface{}, error)
Get implemetns the method from Parameters
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 TERNARY_TRUE TERNARY_FALSE )
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.
type Parameters ¶ added in v1.5.0
type Parameters interface { // Get gets the parameter of the given name Get(name string) (interface{}, error) }
Parameters is a collection of named parameters that are accessible via the Get method.