PaesslerAG / gval

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

Allow single quotes for Strings to allow parsing JSONPath #97

Open JanHoefelmeyer opened 1 year ago

JanHoefelmeyer commented 1 year ago

As indicated in https://github.com/PaesslerAG/jsonpath/issues/38, Gval has trouble evaluating some JSONPath expressions since its syntax in that point conflicts with Go syntax:

Strings in Go are portrayed by double-quotes "..." and as raw-strings ... . Single-quoted-strings '...' are not known in Go. Instead, they are used to indicate single characters and runes. This means, when Gval encounters single-quotes strings, it will try to parse it as a single character and fail, throwing an error.

This is because Gval uses the text/scanner as is. To solve this issue, there could be an option to allow for alternate parsing, which would take single quotes or similar into account. Instead of the text/scanner package, a custom, similar lexer could be used.

Addtionally, the strconv.Unquote functionality is also based on Go, and as such will not accept single-quoted strings. As such, the parseString-method could be adjusted as well, so that it transforms single-quoted strings into double-quoted strings, allowing them to be parsed.

These changes could be made optional as to not disrupt potential workflows that already depend on jsonpath not evaluating single quoted strings.

bernhardreiter commented 11 months ago

This issue with the corresponding PR#98 is a precondition for fixing https://github.com/PaesslerAG/jsonpath/issues/38 .

@mhengl if you have questions, please let us know! :)