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

A new output format: Tcl dictionaries #685

Open
dbohdan opened this issue Jan 30, 2015 · 3 comments
Open

A new output format: Tcl dictionaries #685

dbohdan opened this issue Jan 30, 2015 · 3 comments

Comments

@dbohdan
Copy link

dbohdan commented Jan 30, 2015

I've implemented a function that produces Tcl dictionary output from jq (see man n tcl for an overview of the format). You will find it below.

If possible, I'd like to have it ship as part of jq (e.g., as @tcl). I've looked and all the existing export formats (@csv, @tsv, etc.) seem to be implemented in C rather than jq itself, so before I start working on a pull request I would like to ask: would be acceptable if I implemented @tcl as a call from f_format to a function stored in jq_builtins or should I avoid that and translate it to plain C?

def totcl:
    if type == "array" then
        # Convert array to object with keys 0, 1, 2... and process
        # it as object.
        [range(0;length) as $i
            | {key: $i | tostring, value: .[$i]}]
        | from_entries
        | totcl
    elif type == "object" then
        .
        | to_entries
        | map("{\(.key)} {\(.value | totcl)}")
        | join(" ")
    else
        tostring
        | gsub("{";"\\{")
        | gsub("}";"\\}")
    end;
. | totcl # Use example.
@nicowilliams
Copy link
Contributor

Hey there, this is cool! Yes, perhaps format should be jq-coded, calling out to the C-coded form only for the C-coded formats. This should be easy to do, I think, and would be something like:

def format(fmt): if fmt == "@tcl" then ... else _format(fmt) end;

@nicowilliams
Copy link
Contributor

We'll have to think about ways of making this something that can be delivered by modules.

@dbohdan
Copy link
Author

dbohdan commented Feb 1, 2015

@nicowilliams The ability to define new formats this way would be great!

I've updated the code to use gsub rather than split/join, which doesn't work for strings like "{something}".

@dbohdan dbohdan changed the title A new output format: Tcl dictionaries Make it possible to define new @foo formats in jq, not C Sep 4, 2016
@dbohdan dbohdan changed the title Make it possible to define new @foo formats in jq, not C A new output format: Tcl dictionaries Sep 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants