gval icon indicating copy to clipboard operation
gval copied to clipboard

How to get rendered expressions

Open StrangeYear opened this issue 3 years ago • 4 comments

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 avatar Apr 25 '22 02:04 StrangeYear

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

StrangeYear avatar Apr 25 '22 02:04 StrangeYear

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

skyf0cker avatar Apr 25 '22 03:04 skyf0cker

@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.

generikvault avatar Apr 25 '22 15:04 generikvault

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?

Gigaclank avatar Jun 17 '22 09:06 Gigaclank