Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined attributes key returns error #1

Closed
ramarahmanda opened this issue May 21, 2018 · 4 comments
Closed

Undefined attributes key returns error #1

ramarahmanda opened this issue May 21, 2018 · 4 comments

Comments

@ramarahmanda
Copy link

ramarahmanda commented May 21, 2018

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)

@generikvault
Copy link
Collaborator

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.

@generikvault
Copy link
Collaborator

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.

@Koriit
Copy link

Koriit commented Mar 13, 2019

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.

@generikvault
Copy link
Collaborator

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 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants