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

Define format in jq code #1228

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Define format in jq code #1228

wants to merge 5 commits into from

Conversation

dbohdan
Copy link

@dbohdan dbohdan commented Sep 4, 2016

The proposed change makes it easier to extend format with new formats (#1227). The reasoning is that it is often more straightforward to implement a serialization algorithm recursively in a high-level functional language than in C.

I've implemented serialization to Tcl dictionaries (#685) in pure jq code as an example. This version differs slightly from the one in #685 to work around forward slash (\) escaping issues in src/builtin.jq.

Perhaps the function totcl should be renamed _totcl. Edit: Done.

Rename the C builtin "format" to "_format". Define proxy function
"format" in the file builtin.jq. To add a new format at runtime
the user can redefine it.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 85.35% when pulling 19f2e61 on dbohdan:format-in-jq into 0b82185 on stedolan:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 85.35% when pulling 7ec1d73 on dbohdan:format-in-jq into 0b82185 on stedolan:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 85.769% when pulling a1c93d9 on dbohdan:format-in-jq into 0b82185 on stedolan:master.

@nicowilliams
Copy link
Contributor

Hi. This is a wonderful contribution. I may leave totcl that way instead of as _totcl; not sure. I'll try to get to merging this this coming weekend. Thanks!

@dbohdan
Copy link
Author

dbohdan commented Jan 23, 2017

Great!

@dbohdan
Copy link
Author

dbohdan commented Jan 23, 2017

I've realized there is a corner case that can cause @tcl to produce invalid Tcl data. I'll make a test for it and produce a patch shortly.

Mimic how the [list] command in Tcl does it. This both gets rid of
the redundant quoting of "{" and "}" and fixes the lack of necessary
quoting in some cases.

Add more tests for @tcl.
This makes the output format more idiomatic for Tcl.
@dbohdan
Copy link
Author

dbohdan commented Jan 24, 2017

Done. Now @tcl quotes data the way Tcl itself does. I've added tests for some corner cases that the earlier implementation failed to process correctly. I didn't rename _totcl back to totcl to leave that up to you.

I forgot to say that I appreciate you complimenting this change. I am a fan of the purely functional language that jq has grown.

@dbohdan
Copy link
Author

dbohdan commented Sep 16, 2017

Ping. Any updates on this?

@colindean
Copy link

I'd love to see this in jq. I could see this formatter plugin being used to output YAML, hocon, dhall, and some others that are effectively supersets of JSON.

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

Successfully merging this pull request may close these issues.

5 participants