PaesslerAG/gval

Undefined attributes key returns error

ramarahmanda opened this issue · 4 comments

Hi,
I'm trying to create a conditional expression if a variable is nil, but it throws an Error instead.

parameters := make(map[string]interface)
parameters["foo"] = "bar"

expression := "fooz ?? foo"
result, err := gval.Evaluate(expression, parameters)
if err != nil {
  panic(err)
}

Throw error unknown parameter fooz when it should be bar. I've tried to solve this problem by change return error value to a nil value from private function func variable(path ...Evaluable)

Hi,
sorry for the late answer. I've missed your issue.
that is actually not a bug but a matter of how you define the language. You can override this behavior with a custom ident parser. It can be merged into the language with:
gval.PrefixMetaPrefix(scanner.Ident, customParseIdent)

I keep this issue open, since I think. This should be an optional part of the framework.

I've decided returning nil for an undefined variable instead of an error doesn't hurt and made it the default behavior.

I've also added your example to the unit tests.

We have a case where we want to actually error on unknown parameters. It would be great if it was easy to change this behaviour or show in docs how to achieve this with current functionality.

You can achieve this with a custom VariableSelector.
There is an example on how to write a custom VariableSelector
in the godoc: https://godoc.org/github.com/PaesslerAG/gval#VariableSelector

If you have suggestions for better documention feel free to contribute :-)