-
Notifications
You must be signed in to change notification settings - Fork 46
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
Print multiline strings #95
Conversation
The one thing I do not like about using multiline strings is what happens when they are the last key in a file. For example: a: true
b: |
hello
This file has a bunch of trailing newlines, many people (like me) have their editors set up to remove trailing whitespace from their files. I think you can mark the end of a YAML document with a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! Looks good to me.
Only thing I'm curious to hear about is why you don't use repr
when "
occurs in str
(see my comment in line 82 of src/writer.jl
).
I guess Kevin needs to decide whether multi-line handling should become the one-and-only type of writing yaml, or whether we need a keyword argument multiline
to make the user choose between a pure repr
-based printing (like before) and your multiline-solution.
@@ -79,7 +79,22 @@ end | |||
|
|||
# _print a single string | |||
_print(io::IO, str::AbstractString, level::Int=0, ignore_level::Bool=false) = | |||
println(io, repr(MIME("text/plain"), str)) # quote and escape | |||
if occursin('\n', strip(str)) || occursin('"', str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use repr
when "
occurs in str
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the following string:
{"a":"b","c":"d","e":"f"}
repr
on this is "{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"}"
, which is much less readable than multiline version.
indent = repeat(" ", level) | ||
for line in split(str, "\n") | ||
println(io, indent, line) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how you print each line separately, thus effectively calling repr
for each separate line.
@test YAML.load(YAML.yaml(Dict("a" => "many\nnls\n\n\n")))["a"] == "many\nnls\n\n\n" | ||
@test YAML.load(YAML.yaml(Dict("a" => "no\ntrailing\nnls")))["a"] == "no\ntrailing\nnls" | ||
@test YAML.load(YAML.yaml(Dict("a" => "foo\n\"bar\\'")))["a"] == "foo\n\"bar\\'" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good you test that the strings are identical. My first tought about src/writer.jl
was, why are newlines handled differently depending on their number? Now I see that this different handling is only to ensure that input and output match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the format is very finnicky 😅
Actually, I do like what happens in this case, as long as the roundtrip julia/yaml/julia is producing identical results :) EDIT: Just to be clear, do the newlines actually come from |
The trailing newlines come from |
Yeah e.g. my editor (Atom) will remove the trailing newlines. Any reason why we shouldn't just put the |
I support always adding the |
@@ -1,6 +1,6 @@ | |||
name = "YAML" | |||
uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" | |||
version = "0.4.3" | |||
version = "0.4.4" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to have version bumps occur in PRs? In my own repos, I tend to make separate commits where the only thing that happens is a version bump.
Doesn't make a huge difference to me, except that, in a case where multiple PRs are happening at the same time, there may be conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually do it. It doesn't conflict if other PRs bump to the same version. But I can totally remove it if you want!
Oh no! Why is this on me?!? 🤣 I think having a Making these options doesn't have to be part of this PR in my opinion, I'm fine with multi-line and trailing Though I should say in all seriousness, it probably doesn't make sense for me to be the one making these decisions, since I rarely use this package - my stewardship is a somewhat strange accident. In the absence of any objection, I'm happy to let y'all have final say. |
The error in CI is
|
Looks like a valid failure, I didn't even realize that you could have strings at the top level... |
Also, I read the spec and we should actually be using |
Codecov Report
@@ Coverage Diff @@
## master #95 +/- ##
==========================================
+ Coverage 80.85% 81.31% +0.46%
==========================================
Files 12 12
Lines 1452 1461 +9
==========================================
+ Hits 1174 1188 +14
+ Misses 278 273 -5
Continue to review full report at Codecov.
|
Good to know! But this PR is ready to merge? Maybe worth opening issues for optional single-line printing and including |
Yeah, this is good to go. |
As discussed in #91, there are some cases where multiline strings are beneficial.
With this implementation, strings are printed on multiple lines if:
I'm using YAML primarily for BrokenRecord.jl where we store HTTP response bodies, which are often JSON. So both cases listed above are very common, and they look much much better as a multiline string that doesn't require escaping.
This is very much open for discussion, if people prefer the simplicity of
repr
then we can, then we can stick with that. But I'd argue that most people use YAML because it's meant to be human-readable :)