I am trying to build a rule engine using govaluate: https://github.com/Knetic/govaluate
Here is my code:
package main
import (
"github.com/Knetic/govaluate"
"fmt"
)
func main() {
expression, err := govaluate.NewEvaluableExpression("first_name == 'Julian' && emp_id == 302 && birth_date >= '2016-12-12' && birth_date <= '2016-12-25'")
parameters := make(map[string]interface{}, 8)
parameters["first_name"] = "Julian"
parameters["emp_id"] = 302
parameters["birth_date"] = "2016-12-15"
result, err := expression.Evaluate(parameters)
if err != nil {
fmt.Println("Error while evaluating the expression")
}
if result.(bool) {
fmt.Println("Condition is evaluated to true")
} else {
fmt.Println("Condition is evaluated to false")
}
}
While browsing through govaluate Types documentation (https://github.com/Knetic/govaluate/blob/master/MANUAL.md), I noticed this statement "Any string literal (not parameter) which is interpretable as a date will be converted to a float64 representation of that date's unix time."
When i execute the above program, i see an issue with result being null:
Error while evaluating the expression
panic: interface conversion: interface is nil, not bool
My question is whether date value can be passed in as a parameter? As i want to compare date values in runtime, this functionality would be needed.
Is there a way to achieve it? May be my usage pattern is wrong. Please suggest.
Edit:
expression, err := govaluate.NewEvaluableExpression("first_name == 'Julian' && emp_id == 302 && birth_date >= '2016-12-12' && birth_date <= '2016-12-25'")
dateFloat, _ := strconv.ParseFloat("2016-12-15", 64)
parameters["birth_date"] = dateFloat
result, err := expression.Evaluate(parameters)
Changed the date to float format and invoked Evaluate function. Now the condition is evaluated to false. But logically, 2016-12-15 falls between 2016-12-12 and 2016-12-25. The expression should be evaluated to true.
Thanks in advance!