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

Support |||- from YAML #289

Open
guoshimin opened this issue Jan 18, 2017 · 9 comments · May be fixed by #1175 or google/go-jsonnet#773
Open

Support |||- from YAML #289

guoshimin opened this issue Jan 18, 2017 · 9 comments · May be fixed by #1175 or google/go-jsonnet#773

Comments

@guoshimin
Copy link
Contributor

guoshimin commented Jan 18, 2017

This will send jsonnet spinning, consuming 100% of CPU:

jsonnet -e '|||-
  abc
|||'

Tested with v0.9.0.

I was trying to see if jsonnet supported chomping the final newline in text blocks similar to how it's done in YAML. Consider this both a bug report and a feature request :D

@sparkprime
Copy link
Contributor

Thanks for the report, this is a dup of a previous bug which is fixed on master. Perhaps another release is in order...

$ jsonnet -e '|||-
>   abc
> |||'
STATIC ERROR: <cmdline>:1:1: Text block syntax requires new line after |||.

@sparkprime sparkprime changed the title jsonnet spinning on certain input Support |||- from YAML Jan 19, 2017
@sparkprime
Copy link
Contributor

I'm curious to know why you want this?

@guoshimin
Copy link
Contributor Author

guoshimin commented Jan 19, 2017 via email

@sparkprime
Copy link
Contributor

Pretty feasible I should think. I guess this is only useful if you only have one line?

@sparkprime
Copy link
Contributor

I'm struggling to understand why anyone would want to have a paragraph of text with no terminating \n :)

@guoshimin
Copy link
Contributor Author

The use case is visually concatenating two text blocks together:

local a = ...  // text block a
local b = ...  // text block b
|||
  %s
  %s
||| % [a, b]

If I want b to immediately follow a, I need to chomp the last newline in a.

@guoshimin
Copy link
Contributor Author

(Sorry for the late reply. Github often doesn't email me for issue and PR updates.)

@sparkprime
Copy link
Contributor

Ok that sounds reasonable. It is not hard to add the support. I think a reasonable approach is that the reformatter converts (without consent and with no way to turn it off)

|||-
    foo

|||

to

|||
    foo
|||

With that mode of operation, the presence of a \n in the last byte of the string literal indicates whether |||- should be emitted by the formatter. In other words there is no other state needed to store whether or not |||- was used.

Changes needed are simple:

  1. Modify lexer to accept |||-, suppress terminating \n
  2. Modify reformatter to emit ||| if there is a terminating \n, |||- otherwise

After (1) one should be able to test the JSON output. However without (2) jsonnet fmt will produce code that does not parse.

@vergenzt
Copy link

vergenzt commented Oct 11, 2024

I know this is a pretty old issue... but is there still openness to adding this syntax? I'm aware that std.rstripChars(..., '\n') would accomplish a similar goal, but it does have the unfortunate side effect that any number of trailing newlines are stripped rather than just one trailing newline (which is what I would expect from |||-, and I think is what the documented behavior should be).

I really like the approach from #289 (comment).

Edit: My use case for this is also that I'm trying to concatenate multiple multiline string literals without extra newlines in between.

vergenzt added a commit to vergenzt/jsonnet that referenced this issue Oct 11, 2024
@vergenzt vergenzt linked a pull request Oct 11, 2024 that will close this issue
vergenzt added a commit to vergenzt/go-jsonnet that referenced this issue Oct 11, 2024
CertainLach added a commit to CertainLach/jrsonnet that referenced this issue Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants