Skip to content

feat: add --wrap flag to apply a jsonnet expression to the top-level value#869

Closed
vivainio wants to merge 2 commits intogoogle:masterfrom
vivainio:wrap-command
Closed

feat: add --wrap flag to apply a jsonnet expression to the top-level value#869
vivainio wants to merge 2 commits intogoogle:masterfrom
vivainio:wrap-command

Conversation

@vivainio
Copy link
Copy Markdown

@vivainio vivainio commented Apr 3, 2026

Closes #195.

Motivation

My primary use case is emitting YAML output directly from jsonnet, without piping through an external converter. The --wrap flag makes this possible with --wrap std.manifestYamlDoc, while also being general enough to support any output format via stdlib or custom functions.

Summary

Adds a --wrap <expr> flag that applies a jsonnet expression to the top-level evaluated value before output, with string output mode implied.

This is the "simple wrapping" variant discussed in #195 — it constructs a snippet (expr)(value) and evaluates it with string output. This is equivalent to the existing jsonnet -S -e 'std.manifestINI(import "file.jsonnet")' pattern but more convenient.

Known limitation: stack traces

As discussed in #195, when an error occurs during manifestation through a stdlib function like std.manifestYamlDoc, the stack trace includes stdlib internals, which is noisier than errors during normal evaluation. A future enhancement could add a "deep evaluation" variant that fully evaluates the value before passing it to the manifester, producing cleaner error traces for the custom manifester use case.

Examples

# YAML output
jsonnet --wrap std.manifestYamlDoc file.jsonnet

# YAML without quoted keys
jsonnet --wrap 'function(x) std.manifestYamlDoc(x, quote_keys=false)' file.jsonnet

# Pretty JSON via stdlib
jsonnet --wrap std.manifestJson file.jsonnet

# Extract a single field for use in shell scripts
jsonnet --wrap 'function(x) x.version' file.jsonnet

Notes

  • --wrap is incompatible with --multi, --yaml-stream, and --string
  • For file input, builds (expr)(import @'file') as a snippet
  • For -e / stdin, inlines the input as (expr)(input)
  • The wrap expression must evaluate to a function of one argument

Test plan

  • jsonnet --wrap std.manifestYamlDoc file.jsonnet produces YAML
  • jsonnet --wrap 'function(x) std.manifestYamlDoc(x, quote_keys=false)' file.jsonnet produces unquoted-key YAML
  • Works with stdin (-) and -e code input
  • --wrap + --multi / --yaml-stream / --string each produce a clear error

vivainio added 2 commits April 3, 2026 09:17
…value

Closes google#195. The --wrap flag takes a jsonnet expression (typically a
function) and applies it to the evaluated top-level value before output,
with string output mode implied.

Examples:
  jsonnet --wrap std.manifestYamlDoc file.jsonnet
  jsonnet --wrap 'function(x) std.manifestYamlDoc(x, quote_keys=false)' file.jsonnet
  jsonnet --wrap std.manifestJson file.jsonnet

--wrap is incompatible with --multi, --yaml-stream, and --string.
@He-Pin
Copy link
Copy Markdown
Contributor

He-Pin commented Apr 4, 2026

is there a pr in the cpp implementation too?

@vivainio
Copy link
Copy Markdown
Author

vivainio commented Apr 5, 2026

No, I wasn't aware this is needed

@vivainio
Copy link
Copy Markdown
Author

I am closing this for lack of personal need (using jrsonnet for this)

@vivainio vivainio closed this Apr 11, 2026
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.

A commandline option for output format

2 participants