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

Evaluating strings with backslashes raises parsing errors #89

Open
tjerman opened this issue Nov 4, 2022 · 1 comment
Open

Evaluating strings with backslashes raises parsing errors #89

tjerman opened this issue Nov 4, 2022 · 1 comment

Comments

@tjerman
Copy link

tjerman commented Nov 4, 2022

Env

This is on github.com/PaesslerAG/gval v1.2.1

Issue

We have a custom handler for regular expressions (match(value, regex)) where the regex can be configured from the front-end (web application).

For example, inputting match(variable, "[a-z]\d") would be presented as (when dumping the value) "match(variable, \"[a-z]\\d\")" (which seems correct to me), but when passed to gval, it raises parsing error: match(variable, "[a-z]\d") :1:11 - 1:20 could not parse string: invalid syntax.

If I bypass all of the surrounding code and run it directly as so x, err := gval.Evaluate("match(variable, \"[a-z]\\d\")", map[string]any{"a": "b"}) the error is the same.

Another example would be running this x, err := gval.Evaluate("\"\\\"", map[string]any{"a": "b"}) -- same error in regards to the invalid string (parsing error: "\" :1:1 - 1:4 could not parse string: invalid syntax).

But then, if I do this x, err := gval.Evaluate("\"apple\\banana\"", map[string]any{"a": "b"}), it passes and outputs (when running spew.Dump(x, err))

(string) (len=11) "apple\banana"
(interface {}) <nil>

Conclusion

Am I doing something wrong with my strings, or is this a (un)intentional edge case?

If this needs more investigation I can take a look through the code and propose a fix as well, but I'd appreciate some notes on where would be a good place to start/your suspicions about what's wrong

@generikvault
Copy link
Collaborator

gval handles strings exactly as go. So you need to write the regex string like this match(variable, "[a-z]\\d") or put the regex in a variable.

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

2 participants