Skip to content

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

Closed
jnyi wants to merge 1 commit intodatabricks:masterfrom
jnyi:master
Closed

Implement quote_keys flag for std functions manifestYamlDoc/manifestY…#143
jnyi wants to merge 1 commit intodatabricks:masterfrom
jnyi:master

Conversation

@jnyi
Copy link
Copy Markdown
Contributor

@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 lihaoyi-databricks requested review from szeiger and removed request for lihaoyi-databricks January 21, 2022 11:23
@lihaoyi-databricks
Copy link
Copy Markdown
Contributor

Passing the review to @szeiger


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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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.

@stephenamar-db
Copy link
Copy Markdown
Collaborator

I cloned your PR and finished the work in #231

stephenamar-db added a commit that referenced this pull request Dec 12, 2024
Forked from PR #143

---------

Co-authored-by: Yi Jin <yi.jin@databricks.com>
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.

4 participants