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

Implement quote_keys flag for std functions manifestYamlDoc/manifestY… #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jnyi
Copy link

@jnyi jnyi commented Jan 21, 2022

…amlStream to match official jsonnet doc

Purpose

This PR creates a flag documented in jsonnet official site std library: https://jsonnet.org/ref/stdlib.html for function
manifestYamlDoc and manifestYamlStream, it will allow us to generate yaml without quoting the keys.

Test

  • Tested via unit tests.
  • compile sjsonnet using ./mill -i show "sjsonnet[2.13.4]".jvm.assembly and run again a locally created test.jsonnet to output YAML via flag --yaml-out

@lihaoyi-databricks
Copy link
Contributor

Passing the review to @szeiger

@@ -30,6 +30,15 @@ class YamlRenderer(_out: StringWriter = new java.io.StringWriter(), indentArrayI
}
}

private[this] def removeQuoteKey(): Unit = {
// refer to quote_keys parameter in https://jsonnet.org/ref/stdlib.html, only unquote keys
if (!quoteKeys && !afterKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. quote_keys=false is supposed to omit the quotes if possible, so there should be a check that determines which strings are allowed to be unquoted.

Patching the buffer after writing is a bit awkward. Can we also move this code into writeString so we can determine quoting first (according to the correct criteria, not just the flag) and then emit a quoted or non-quoted string? The new code for non-quoted writing should only end up in a single path because the other ones won't qualify for quote removal anyway.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested the behavior of jsonnet's quote_keys=false which removes quotes of the yaml keys, so my understanding !afterKeys indicates we are dealing with key right now, as for patching the buffer, agree it is a bit counter-intuitive, let me see if I can do something to writeString, found it is a bit harder to modify that function interfaces which touches more places.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should only have to touch the last branch in writeString. There other 2 should not be applicable for quote removal.

@nicklan nicklan removed their request for review October 26, 2022 00:31
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.

3 participants