maja42 / goval

Expression evaluation in golang
MIT License
157 stars 24 forks source link

Issues with structs as variables #5

Closed hoshsadiq closed 4 years ago

hoshsadiq commented 5 years ago

Hello,

I'm trying to use this library as I prefer it over Knetic's library based on features described, however, I'm struggling to get this working with structs. Consider the following scenario.

type Testing struct {
    Title string
}
obj := &Testing{Title: "hello"}
eval := goval.NewEvaluator()
newrs, err := eval.Evaluate(`obj.Title == "hello"`, map[string]interface{}{"obj": obj}, nil)
# err == "syntax error: cannot access fields on type <unknown type>"
fmt.Printf(newrs.(string))

Where this scenario works fine:

type Testing struct {
    Title string
}
//obj := &Testing{Title: "hello"}
eval := goval.NewEvaluator()
newrs, err := eval.Evaluate(`obj == "hello"`, map[string]interface{}{"obj": "hello"}, nil)
fmt.Printf(newrs.(string))

Any ideas how I can resolve this?

maja42 commented 4 years ago

Unfortunately, structs are not supported by this library right now, and I'm not planning to add support for it in the near future. To add support for structs, a different approach would be needed (creating an AST instead of evaluating-while-parsing). See my comment on #6.