kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.53k stars 81 forks source link

feat: adding an option to control style of strings #170

Open SGSSGene opened 12 months ago

SGSSGene commented 12 months ago

Add options to control the styling of strings.

This capability is added by prepending a string with a specific control string (e.g. __yq_style_|__). Because of this, this option is turned off by default and can be turned on by setting --yaml-style-strings.

kislyuk commented 11 months ago

Thanks for your contribution! It's an interesting suggestion and I agree it would be nice to have a way to control the string style in an intuitive way.

The __yq__ control sequences were never meant to be exposed to the user through the API - up until now, they have only ever been used internally in the roundtrip process between yq and jq. So my main hesitation with this change is that it actually commits these control sequences into the API (and also uses special character literals for the styles, which may be a pain to escape).

I'll have to think more about the ergonomics of this change. One immediate change that comes to mind is to replace the special character literals with descriptions (__yq_style_single_quoted__, __yq_style_double_quoted__, __yq_style_block_literal__, __yq_style_block_folded__).

SGSSGene commented 11 months ago

Yes, I completely agree.

Another thing I am very unhappy with, is that it is kind of hard to use these special strings. This is why I added the section into the README with the def style(s): function. I would prefer if this function would be available when --yaml-style-strings is set.

If the filter is given on the command line, this is not such a big issue, we could just prepend the function definition to the filter (This is what I did originally). In the case the filter is given via a file, this doesn't work anymore. Any Ideas on how this could be done?

The advantage of having the functions automatically defined is, that then we could use even longer prefixes, avoiding practically any collisions: __yq_style_012323afafaf0123124_quoted__.

SGSSGene commented 1 month ago

rebased to latest tag (v3.4.3)