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

[lang] Add escape syntax for field names #146

Merged
merged 1 commit into from
Jul 23, 2021
Merged

Conversation

wfchandler
Copy link
Contributor

Related to #99

Currently field names containing a space or period, e.g. date received or grpc.method, cannot be parsed. This could be worked around using jq or similar tools to rewrite the field name, but that's a pain.

This commit adds an escaped field name syntax of ["<FIELD>"]. This is based on the Object Identifier-Index syntax used by jq, so it should be somewhat familiar to many people who parse JSON on the command line.

The more obvious option of delimiting with just quotes, e.g. "date received", creates an ambiguity between string literals and escaped field names. For example, does where foo == "date received" mean field foo matches field date received, or field foo matches the string "date received"?

Example query:

agrind '* | json | where ["grpc.method"] == "Foo" | count by ["date received"]'

@rcoh
Copy link
Owner

rcoh commented Jul 23, 2021

this is an awesome idea and a problem I've run into a bunch personally! Thanks for tackling this

src/lang.rs Outdated
start: take_while1!(starts_ident) >>
rest: take_while!(is_ident) >>
(start.fragment.0.to_owned() + rest.fragment.0)
));

named!(escaped_ident<Span, String>, do_parse!(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you use the quoted_string construction instead? that will handle considerably more flexible characters (including escaping quotes inside of the string)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I've swapped in quoted_string as you suggested and added a new test case in parse_quoted_ident.

Technically I think JSON and logfmt require double quotes for strings, but I think it's probably better to be permissive since we can receive unstructured input.

Related to rcoh#99

Currently field names containing a space, period, or escaped quote,
e.g. `date received` or `grpc.method`, cannot be parsed. This could
be worked around using `jq` or similar tools to rewrite the field
name, but that's a pain.

This commit adds an escaped field name syntax of `["<FIELD>"]`. This is
based on the Object Identifier-Index syntax[0] used by `jq`, so it
should be somewhat familiar to many people who parse JSON on the
command line.

The more obvious option of delimiting with just quotes, e.g.
"date received", creates an ambiguity between string literals and
escaped field names. For example, does `where foo == "date received"`
mean field `foo` matches field `date received`, or field `foo` matches
the string "date received"?

Example query:

```
* | json | where ["grpc.method"] == "Foo" | count by ["date received"]
```

[0]
https://stedolan.github.io/jq/manual/#ObjectIdentifier-Index:.foo,.foo.bar
Copy link
Owner

@rcoh rcoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! thanks this is awesome

@rcoh rcoh merged commit a513dcb into rcoh:main Jul 23, 2021
@wfchandler wfchandler deleted the escaped_idents branch July 23, 2021 19:18
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

Successfully merging this pull request may close these issues.

None yet

2 participants