Documentation
¶
Index ¶
- Constants
- 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 OperatorPrecedence
- type OperatorSymbol
- type Parameters
- type TokenKind
Constants ¶
const ( TYPEERROR_LOGICAL string = "Value '%v' cannot be used with the logical operator '%v', it is not a bool" TYPEERROR_MODIFIER string = "Value '%v' cannot be used with the modifier '%v', it is not a number" TYPEERROR_COMPARATOR string = "Value '%v' cannot be used with the comparator '%v', it is not a number" TYPEERROR_TERNARY string = "Value '%v' cannot be used with the ternary operator '%v', it is not a bool" TYPEERROR_PREFIX string = "Value '%v' cannot be used with the prefix '%v'" )
Variables ¶
var ADDITIVE_MODIFIERS = []OperatorSymbol{ PLUS, MINUS, }
var ADDITIVE_SYMBOLS = map[string]OperatorSymbol{ "+": PLUS, "-": MINUS, }
var BITWISE_MODIFIERS = []OperatorSymbol{ BITWISE_AND, BITWISE_OR, BITWISE_XOR, }
var BITWISE_SHIFT_MODIFIERS = []OperatorSymbol{ BITWISE_LSHIFT, BITWISE_RSHIFT, }
var BITWISE_SHIFT_SYMBOLS = map[string]OperatorSymbol{ ">>": BITWISE_RSHIFT, "<<": BITWISE_LSHIFT, }
var BITWISE_SYMBOLS = map[string]OperatorSymbol{ "^": BITWISE_XOR, "&": BITWISE_AND, "|": BITWISE_OR, }
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 EXPONENTIAL_SYMBOLS = map[string]OperatorSymbol{ "**": EXPONENT, }
var LOGICAL_SYMBOLS = map[string]OperatorSymbol{ "&&": AND, "||": OR, }
var MODIFIER_SYMBOLS = map[string]OperatorSymbol{ "+": PLUS, "-": MINUS, "*": MULTIPLY, "/": DIVIDE, "%": MODULUS, "**": EXPONENT, "&": BITWISE_AND, "|": BITWISE_OR, "^": BITWISE_XOR, ">>": BITWISE_RSHIFT, "<<": BITWISE_LSHIFT, }
this is defined separately from ADDITIVE_SYMBOLS et al because it's needed for parsing, not stage planning.
var MULTIPLICATIVE_MODIFIERS = []OperatorSymbol{ MULTIPLY, DIVIDE, MODULUS, }
var MULTIPLICATIVE_SYMBOLS = map[string]OperatorSymbol{ "*": MULTIPLY, "/": DIVIDE, "%": MODULUS, }
var NUMERIC_COMPARATORS = []OperatorSymbol{ GT, GTE, LT, LTE, }
var PREFIX_MODIFIERS = []OperatorSymbol{ NEGATE, INVERT, BITWISE_NOT, }
var PREFIX_SYMBOLS = map[string]OperatorSymbol{ "-": NEGATE, "!": INVERT, "~": BITWISE_NOT, }
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 OperatorPrecedence ¶
type OperatorPrecedence int
const ( VALUE_PRECEDENCE OperatorPrecedence = iota PREFIX_PRECEDENCE EXPONENTIAL_PRECEDENCE ADDITIVE_PRECEDENCE BITWISE_PRECEDENCE BITWISE_SHIFTPRECEDENCE MULTIPLICATIVE_PRECEDENCE COMPARATOR_PRECEDENCE TERNARY_PRECEDENCE LOGICAL_PRECEDENCE )
type OperatorSymbol ¶
type OperatorSymbol int
Represents the valid symbols for operators.
const ( NOOP OperatorSymbol = iota EQ NEQ GT LT GTE LTE REQ NREQ 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 )
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.
func (OperatorSymbol) String ¶
func (this OperatorSymbol) String() string
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 ¶ 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.