PaesslerAG / gval

Expression evaluation in golang
BSD 3-Clause "New" or "Revised" License
731 stars 82 forks source link

How to get rendered expressions #80

Open StrangeYear opened 2 years ago

StrangeYear commented 2 years ago

Example: expression: a + b > 6 args: {"a":3,"b":4} result: 3 + 4 > 6

Because i need to show the user the calculation process of the expression rather than the direct result. Thanks.

StrangeYear commented 2 years ago

Or if can get the names and locations of all parameters.it's fine too.

skyf0cker commented 2 years ago

I don't think it can be supported appropriately by current implement. If #78 can be implemented, you can use this feature to achieve what you want. If no one work on this issue, i will try to work on it later in my spare time. CC @generikvault

generikvault commented 2 years ago

@skyf0cker yeah sry. gval is a spare time project for me as well. Feel free to make a pull request.

@StrangeYear you can override the calculation itself:

var lang = gval.Full(
  gval.InfixNumberOperator("+", func(a, b float64) (interface{}, error) {
    fmt.Printf("%d + %d\n", a, b)  // or collect it in a more appropiate way
    return a + b, nil
  }),
  gval.InfixNumberOperator(">", func(a, b float64) (interface{}, error) {
    fmt.Printf("%d > %d\n", a, b) 
    return a > b, nil
  }),

but you would have to override every operator for this approach. I don't now wether this helps you or not.

Gigaclank commented 2 years ago

I Have a similar idea going on in my head at the moment.

Ideally I would like to identify where the expression was "triggered"

say I have (foo > bar) || ( foo > baz) I would like to be able to know that it was the left side of the operator or the right side that triggered a result to be true this would help me narrow down the causes of some business logic alerts. but to also then be able to know the evaluated expression for the relative expression would also benefit debugging greatly.

A workaround is to split out the logic and analyze each expression separately.

@generikvault I assume your suggestion here about the language override would be the simplest approach to achieve something like what I am looking for?