Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions EvaluableExpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const shortCircuitHolder int = -1
var DUMMY_PARAMETERS = MapParameters(map[string]interface{}{})

/*
EvaluableExpression represents a set of ExpressionTokens which, taken together,
are an expression that can be evaluated down into a single value.
EvaluableExpression represents a set of ExpressionTokens which, taken together,
are an expression that can be evaluated down into a single value.
*/
type EvaluableExpression struct {

Expand All @@ -39,8 +39,8 @@ type EvaluableExpression struct {
}

/*
Parses a new EvaluableExpression from the given [expression] string.
Returns an error if the given expression has invalid syntax.
Parses a new EvaluableExpression from the given [expression] string.
Returns an error if the given expression has invalid syntax.
*/
func NewEvaluableExpression(expression string) (*EvaluableExpression, error) {

Expand All @@ -49,8 +49,8 @@ func NewEvaluableExpression(expression string) (*EvaluableExpression, error) {
}

/*
Similar to [NewEvaluableExpression], except that instead of a string, an already-tokenized expression is given.
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)
Similar to [NewEvaluableExpression], except that instead of a string, an already-tokenized expression is given.
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 NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpression, error) {

Expand Down Expand Up @@ -85,8 +85,8 @@ func NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpre
}

/*
Similar to [NewEvaluableExpression], except enables the use of user-defined functions.
Functions passed into this will be available to the expression.
Similar to [NewEvaluableExpression], except enables the use of user-defined functions.
Functions passed into this will be available to the expression.
*/
func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction) (*EvaluableExpression, error) {

Expand Down Expand Up @@ -127,15 +127,15 @@ func NewEvaluableExpressionWithFunctions(expression string, functions map[string
}

/*
Same as `Eval`, but automatically wraps a map of parameters into a `govalute.Parameters` structure.
Same as `Eval`, but automatically wraps a map of parameters into a `govalute.Parameters` structure.
*/
func (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error) {
func (e EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error) {

if parameters == nil {
return this.Eval(nil)
return e.Eval(nil)
}

return this.Eval(MapParameters(parameters))
return e.Eval(MapParameters(parameters))
}

var sanitizedParamsPool = sync.Pool{
Expand All @@ -145,19 +145,19 @@ var sanitizedParamsPool = sync.Pool{
}

/*
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")`.
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].
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
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 (this EvaluableExpression) Eval(parameters Parameters) (interface{}, error) {
func (e EvaluableExpression) Eval(parameters Parameters) (interface{}, error) {

if this.evaluationStages == nil {
if e.evaluationStages == nil {
return nil, nil
}

Expand All @@ -171,20 +171,20 @@ func (this EvaluableExpression) Eval(parameters Parameters) (interface{}, error)
parameters = DUMMY_PARAMETERS
}

ret, err := this.evaluateStage(this.evaluationStages, parameters)
ret, err := e.evaluateStage(e.evaluationStages, parameters)
if free {
sanitizedParamsPool.Put(parameters)
}
return ret, err
}

func (this EvaluableExpression) evaluateStage(stage *evaluationStage, parameters Parameters) (interface{}, error) {
func (e EvaluableExpression) evaluateStage(stage *evaluationStage, parameters Parameters) (interface{}, error) {

var left, right interface{}
var err error

if stage.leftStage != nil {
left, err = this.evaluateStage(stage.leftStage, parameters)
left, err = e.evaluateStage(stage.leftStage, parameters)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,13 +217,13 @@ func (this EvaluableExpression) evaluateStage(stage *evaluationStage, parameters
}

if right != shortCircuitHolder && stage.rightStage != nil {
right, err = this.evaluateStage(stage.rightStage, parameters)
right, err = e.evaluateStage(stage.rightStage, parameters)
if err != nil {
return nil, err
}
}

if this.ChecksTypes {
if e.ChecksTypes {
if stage.typeCheck == nil {

err = typeCheck(stage.leftTypeCheck, left, stage.symbol, stage.typeErrorFormat)
Expand Down Expand Up @@ -262,27 +262,27 @@ func typeCheck(check stageTypeCheck, value interface{}, symbol OperatorSymbol, f
}

/*
Returns an array representing the ExpressionTokens that make up this expression.
Returns an array representing the ExpressionTokens that make up this expression.
*/
func (this EvaluableExpression) Tokens() []ExpressionToken {
func (e EvaluableExpression) Tokens() []ExpressionToken {

return this.tokens
return e.tokens
}

/*
Returns the original expression used to create this EvaluableExpression.
Returns the original expression used to create this EvaluableExpression.
*/
func (this EvaluableExpression) String() string {
func (e EvaluableExpression) String() string {

return this.inputExpression
return e.inputExpression
}

/*
Returns an array representing the variables contained in this EvaluableExpression.
Returns an array representing the variables contained in this EvaluableExpression.
*/
func (this EvaluableExpression) Vars() []string {
func (e EvaluableExpression) Vars() []string {
var varlist []string
for _, val := range this.Tokens() {
for _, val := range e.Tokens() {
if val.Kind == VARIABLE {
varlist = append(varlist, val.Value.(string))
}
Expand All @@ -293,6 +293,6 @@ func (this EvaluableExpression) Vars() []string {
/*
Removes the tokens from the EvaluableExpression. This will cause the Tokens() and Vars() functions to no longer operate, but will save memory.
*/
func (this *EvaluableExpression) CleanupTokens() {
this.tokens = this.tokens[:0]
func (e *EvaluableExpression) CleanupTokens() {
e.tokens = e.tokens[:0]
}
20 changes: 10 additions & 10 deletions EvaluableExpression_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ Boolean values are considered to be "1" for true, "0" for false.

Times are formatted according to this.QueryDateFormat.
*/
func (this EvaluableExpression) ToSQLQuery() (string, error) {
func (e EvaluableExpression) ToSQLQuery() (string, error) {

var stream *tokenStream
var transactions *expressionOutputStream
var transaction string
var err error

stream = newTokenStream(this.tokens)
stream = newTokenStream(e.tokens)
transactions = new(expressionOutputStream)

for stream.hasNext() {

transaction, err = this.findNextSQLString(stream, transactions)
transaction, err = e.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
Expand All @@ -41,7 +41,7 @@ func (this EvaluableExpression) ToSQLQuery() (string, error) {
return transactions.createString(" "), nil
}

func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transactions *expressionOutputStream) (string, error) {
func (e EvaluableExpression) findNextSQLString(stream *tokenStream, transactions *expressionOutputStream) (string, error) {

var token ExpressionToken
var ret string
Expand All @@ -55,7 +55,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
case PATTERN:
ret = fmt.Sprintf("'%s'", token.Value.(*regexp.Regexp).String())
case TIME:
ret = fmt.Sprintf("'%s'", token.Value.(time.Time).Format(this.QueryDateFormat))
ret = fmt.Sprintf("'%s'", token.Value.(time.Time).Format(e.QueryDateFormat))

case LOGICALOP:
switch logicalSymbols[token.Value.(string)] {
Expand Down Expand Up @@ -101,7 +101,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
case COALESCE:

left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
right, err := e.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
Expand All @@ -110,7 +110,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
case TERNARY_TRUE:
fallthrough
case TERNARY_FALSE:
return "", errors.New("Ternary operators are unsupported in SQL output")
return "", errors.New("ternary operators are unsupported in sql output")
}
case PREFIX:
switch prefixSymbols[token.Value.(string)] {
Expand All @@ -119,7 +119,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
ret = "NOT"
default:

right, err := this.findNextSQLString(stream, transactions)
right, err := e.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
Expand All @@ -133,7 +133,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
case EXPONENT:

left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
right, err := e.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
Expand All @@ -142,7 +142,7 @@ func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transacti
case MODULUS:

left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
right, err := e.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
Expand Down
29 changes: 14 additions & 15 deletions OperatorSymbol.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package govaluate

/*
Represents the valid symbols for operators.

Represents the valid symbols for operators.
*/
type OperatorSymbol int

Expand Down Expand Up @@ -142,9 +141,9 @@ func findOperatorPrecedenceForSymbol(symbol OperatorSymbol) operatorPrecedence {
}

/*
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.
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 comparatorSymbols = map[string]OperatorSymbol{
"==": EQ,
Expand Down Expand Up @@ -221,13 +220,13 @@ var separatorSymbols = map[string]OperatorSymbol{
}

/*
Returns true if this operator is contained by the given array of candidate symbols.
False otherwise.
Returns true if this operator is contained by the given array of candidate symbols.
False otherwise.
*/
func (this OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool {
func (o OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool {

for _, symbolType := range candidate {
if this == symbolType {
if o == symbolType {
return true
}
}
Expand All @@ -236,14 +235,14 @@ func (this OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool {
}

/*
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.
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.
*/
func (this OperatorSymbol) String() string {
func (o OperatorSymbol) String() string {

switch this {
switch o {
case NOOP:
return "NOOP"
case VALUE:
Expand Down
14 changes: 7 additions & 7 deletions dummies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ type dummyParameter struct {
Map map[string]interface{}
}

func (this dummyParameter) Func() string {
func (dummyParameter) Func() string {
return "funk"
}

func (this dummyParameter) Func2() (string, error) {
func (dummyParameter) Func2() (string, error) {
return "frink", nil
}

func (this *dummyParameter) Func3() string {
func (*dummyParameter) Func3() string {
return "fronk"
}

func (this dummyParameter) FuncArgStr(arg1 string) string {
func (dummyParameter) FuncArgStr(arg1 string) string {
return arg1
}

func (this dummyParameter) TestArgs(str string, ui uint, ui8 uint8, ui16 uint16, ui32 uint32, ui64 uint64, i int, i8 int8, i16 int16, i32 int32, i64 int64, f32 float32, f64 float64, b bool) string {
func (dummyParameter) TestArgs(str string, ui uint, ui8 uint8, ui16 uint16, ui32 uint32, ui64 uint64, i int, i8 int8, i16 int16, i32 int32, i64 int64, f32 float32, f64 float64, b bool) string {

var sum float64

Expand All @@ -49,15 +49,15 @@ func (this dummyParameter) TestArgs(str string, ui uint, ui8 uint8, ui16 uint16,
return fmt.Sprintf("%v: %v", str, sum)
}

func (this dummyParameter) AlwaysFail() (interface{}, error) {
func (dummyParameter) AlwaysFail() (interface{}, error) {
return nil, errors.New("function should always fail")
}

type dummyNestedParameter struct {
Funk string
}

func (this dummyNestedParameter) Dunk(arg1 string) string {
func (dummyNestedParameter) Dunk(arg1 string) string {
return arg1 + "dunk"
}

Expand Down
Loading
Loading