feat: add --wrap flag to apply a jsonnet expression to the top-level value#869
Closed
vivainio wants to merge 2 commits intogoogle:masterfrom
Closed
feat: add --wrap flag to apply a jsonnet expression to the top-level value#869vivainio wants to merge 2 commits intogoogle:masterfrom
vivainio wants to merge 2 commits intogoogle:masterfrom
Conversation
…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.
Contributor
|
is there a pr in the cpp implementation too? |
Author
|
No, I wasn't aware this is needed |
Author
|
I am closing this for lack of personal need (using jrsonnet for this) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #195.
Motivation
My primary use case is emitting YAML output directly from jsonnet, without piping through an external converter. The
--wrapflag 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 existingjsonnet -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
Notes
--wrapis incompatible with--multi,--yaml-stream, and--string(expr)(import @'file')as a snippet-e/ stdin, inlines the input as(expr)(input)Test plan
jsonnet --wrap std.manifestYamlDoc file.jsonnetproduces YAMLjsonnet --wrap 'function(x) std.manifestYamlDoc(x, quote_keys=false)' file.jsonnetproduces unquoted-key YAML-) and-ecode input--wrap+--multi/--yaml-stream/--stringeach produce a clear error