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

Output query results as JSON #1976

Open
GuillaumeDesforges opened this issue Jun 22, 2024 · 3 comments
Open

Output query results as JSON #1976

GuillaumeDesforges opened this issue Jun 22, 2024 · 3 comments

Comments

@GuillaumeDesforges
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I need to programmatically list top-level fields of a nickel program.

$ nickel query cbuild.ncl
warning: empty query path
 = You queried a value without requesting a specific field path. This operation can't find any metadata, beside listing the fields of a record.
 = Try to query the root configuration and provide a query path instead.
 = For example, instead of querying the expression `(import "config.ncl").module.input` with an empty path, query `config.ncl` with the `module.input` path: 
   `nickel query config.ncl --field module.input


Available fields
• hello
No metadata found for this field.

It is too hard to parse in my script.

Describe the solution you'd like

$ nickel query --json cbuild.ncl
{"fields":["hello"]}

Additional context
I'm calling nickel as a subprocess.

@suimong
Copy link
Contributor

suimong commented Jun 23, 2024

As a workaround, you could write another nickel program extract_toplevel_fields.ncl like this:

let record = import "cbuild.ncl" in {
  fields = record |> std.record.fields   # or std.record.fields_with_opts
}

Then nickel export extract_toplevel_fields.ncl.

In case you'd like to specify the file being imported at runtime, which is not directly supported by nickel, just template the whole program, making the filename part a template variable, use whatever string templating mechanism available in your PL to render the program and pipe into nickel export.

Heck you could even do all that with Nickel alone:

# extract_toplevel_fields.ncl
{
    filename,
    program | force = "let record = import \"%{filename}\" in {fields = record |> std.record.fields}"
}

Then invoke the following:

nickel export toplevel_fields.ncl --field=program --format=raw -- filename="cbuild.ncl" | nickel export

@GuillaumeDesforges
Copy link
Contributor Author

Thanks for the workaround.

I do want to emphasize that facilitating the interoperability of tools like Nickel with scripting would be a good plus.

Since there is a --error-format on most commands and --format for the export command, I think it also makes sense to provide --format for query command.

@suimong
Copy link
Contributor

suimong commented Jun 24, 2024

I second your proposal. It would be very helpful if outputs of nickel query can be easily machine readable. Though this might require a little bit of designing with regard to the "schema" of the output, among other things. What do you think @yannham ?

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