maja42 / goval

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

How to get non valid #15

Closed yahya077 closed 7 months ago

yahya077 commented 7 months ago

Hi, I'm using eval to validate expressions like "lorem > 0 && ipsum < 2". However, when one of the conditions is wrong, it only returns false. I need to know which one is set to false. Is there any help?

maja42 commented 7 months ago

No, detecting the results of sub-queries is not possible. You would need to execute them independently.

While you could use custom functions like this:

var result bool
func isGreaterThan(args ...interface{}) (interface{}, error) {
    arg1 := args[0].(int)
    arg2 := args[1].(int)
    result = arg1 > arg2
    return result, nil
}

Query:
    isGreaterThan(lorem, 0) && isLessThen(ipsum, 2)

...(which stores the intermediate result in a global variable or closure-variable), it is very ugly and unreliable.