Skip to content

Conversation

vikstrous2
Copy link
Contributor

@vikstrous2 vikstrous2 commented Aug 9, 2021

I'm looking for feedback on the naming. It's not great that I had to add Opt to the end of the name of the option because the other options don't have that. It would be great if we can avoid that somehow.

I'm also wondering if there will be other quote related options in the future that we should plan for. It wouldn't be great to have combinatorial explosion of different quote style enums. Maybe I should make this option more specific than "quote style"? Maybe we can just have a bool with something like "SingleQuoteStrings"?

@codecov-commenter
Copy link

codecov-commenter commented Aug 10, 2021

Codecov Report

Merging #242 (7bee600) into master (561cb14) will decrease coverage by 0.80%.
The diff coverage is 40.54%.

@@            Coverage Diff             @@
##           master     #242      +/-   ##
==========================================
- Coverage   78.39%   77.59%   -0.81%     
==========================================
  Files          12       13       +1     
  Lines        3421     3494      +73     
==========================================
+ Hits         2682     2711      +29     
- Misses        505      546      +41     
- Partials      234      237       +3     

@vikstrous2 vikstrous2 changed the title WIP: add quote style Add single and double quote styles Aug 10, 2021
@vikstrous2 vikstrous2 marked this pull request as ready for review August 10, 2021 12:58
case QuoteStyleSingle:
v = quoteWith(v, '\'')
default:
v = strconv.Quote(v)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is pretty much the same as calling quoteWith now. Should we just call quoteWith?

package yaml

import (
"unicode"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original code didn't import unicode to avoid bloating the binaries that import it. Is that a concern for go-yaml too? We can copy their custom isPrint function if necessary.

v = strconv.Quote(v)
switch e.quoteStyle {
case QuoteStyleSingle:
v = quoteWith(v, '\'')
Copy link
Owner

Choose a reason for hiding this comment

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

This is difficult. In the original Issue, the submitter worried that originally defined string in a single quote changes to double quoted string at marshal. In this case, I think it is enough to always be able to select single quote or double quote for "quoted string" ( e.g. contains special character like { or } ) .

For example, when outputting a character containing a new line code ( \n or \r ), I think that using Literal ( | ) is more readable than a quoted character string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, yeah:

  1. I'll mark this PR as not actually closing the linked issue. It solves a different issue: I wanted to be able to turn all double quoted strings in the output into single quoted strings, so definitely not the same as the issue I referenced.
  2. There's a much bigger discussion to be had about how quoting should behave. It seems like right now, the quoting is not very smart and it needs to get much fancier. I'd love to chat about this synchronously rather than over github comments because the discussion would take forever this way. Let me know your preferred method of communication. You can DM me on twitter at @vikstrous

Copy link
Owner

Choose a reason for hiding this comment

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

OK ! I would like to discuss on gophers.slack.com . I created #goccy-go-yaml channel .
Could you join it ?

)

// QuoteStyle specifies how strings should be quoted
func QuoteStyleOpt(qs QuoteStyle) EncodeOption {
Copy link
Owner

Choose a reason for hiding this comment

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

What do you think WithQuote(QuoteStyle) EncodeOption ( or Quote(QuoteStyle) EncodeOption ) as name candidate ?

@vikstrous2
Copy link
Contributor Author

Since we can't talk synchronously, here are my thoughts and questions:

  1. I wasn't aware of UseLiteralStyleIfMultiline at the time when I created this PR, which is silly because there are only 8 options. Fortunately, I think, as written, this PR behaves correctly in combination with UseLiteralStyleIfMultiline. It affects only the quotation mark style used for strings that do end up being quoted.
  2. token.IsNeedQuoted is the "fancy" quoting logic I was referring to. I didn't realize that already existed. How much do you trust token.IsNeedQuoted? Is it based on some sort of standard, a heuristic that come from another project, or something that evolved on its own over time?
  3. Given that this new option I'm adding just controls the use of single quotes over double quotes when we do end up using quotes, what do you think about changing the name to just UseSingleQuote and making it a boolean option? That would make it more obvious that it can be combined with other options, like UseLiteralStyleIfMultiline. I'll add more tests to make sure the two don't interfere with each other in unexpected ways.
  4. After making this change, I'll compare the output from this library with the output from jq, so I might find new differences. I hope that making it close enough to jq is achievable. I just want to have a manageable diff when switching from one to the other. It doesn't have to be perfect :)

@vikstrous2
Copy link
Contributor Author

Ping? What should I do? I want to get this merged somehow.

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