Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing parameters to the function #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions evaluationStage.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ func makeFunctionStage(function ExpressionFunction) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {

if right == nil {
return function()
return function(parameters)
}

switch right.(type) {
case []interface{}:
return function(right.([]interface{})...)
return function(parameters, right.([]interface{})...)
default:
return function(right)
return function(parameters, right)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion expressionFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package govaluate
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 ExpressionFunction func(arguments ...interface{}) (interface{}, error)
type ExpressionFunction func(parameters Parameters, arguments ...interface{}) (interface{}, error)
41 changes: 41 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package govaluate

import "math"

const (
EFMax = "Max"
EFMin = "Min"
EFRound = "Round"
)

func BuiltinFunctions() map[string]ExpressionFunction {
return map[string]ExpressionFunction{
EFMax: ExpFuncMax,
EFMin: ExpFuncMin,
EFRound: ExpFuncRound,
}
}

func ExpFuncMax(parameters Parameters, args ...interface{}) (interface{}, error) {
val := 0.0
for _, a := range args {
if a.(float64) > val {
val = a.(float64)
}
}
return val, nil
}

func ExpFuncMin(parameters Parameters, args ...interface{}) (interface{}, error) {
val := math.MaxFloat64
for _, a := range args {
if a.(float64) < val {
val = a.(float64)
}
}
return val, nil
}

func ExpFuncRound(parameters Parameters, args ...interface{}) (interface{}, error) {
return math.Round(args[0].(float64)), nil
}
1 change: 0 additions & 1 deletion lexerState.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ var validLexerStates = []lexerState{
STRING,
TIME,
CLAUSE,
CLAUSE_CLOSE,
},
},
lexerState{
Expand Down
5 changes: 5 additions & 0 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Parameters interface {
Failure to find the given parameter should be indicated by returning an error.
*/
Get(name string) (interface{}, error)
Set(name string, value interface{})
}

type MapParameters map[string]interface{}
Expand All @@ -30,3 +31,7 @@ func (p MapParameters) Get(name string) (interface{}, error) {

return value, nil
}

func (p MapParameters) Set(name string, value interface{}) {
p[name] = value
}
6 changes: 6 additions & 0 deletions parsingFailure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func TestParsingFailure(test *testing.T) {
Input: "10 > 5 &&",
Expected: UNEXPECTED_END,
},
ParsingFailureTest{

Name: "Hanging logical operation, followed by clause-close (#92)",
Input: "(amount > '100' &&) == false",
Expected: INVALID_TOKEN_TRANSITION,
},
ParsingFailureTest{

Name: "Premature end to expression, via ternary operator",
Expand Down
4 changes: 4 additions & 0 deletions sanitizedParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (p sanitizedParameters) Get(key string) (interface{}, error) {
return castToFloat64(value), nil
}

func (p sanitizedParameters) Set(key string, value interface{}) {
p.orig.Set(key, value)
}

func castToFloat64(value interface{}) interface{} {
switch value.(type) {
case uint8:
Expand Down