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 for roundtripping YAML comments #106

Open
paul-hammant opened this issue Oct 18, 2020 · 7 comments
Open

Support for roundtripping YAML comments #106

paul-hammant opened this issue Oct 18, 2020 · 7 comments

Comments

@paul-hammant
Copy link

YAML has comments, JSON doesn't jq strips comments and the -Y -y flags doesn't get them back again.

@kislyuk
Copy link
Owner

kislyuk commented Oct 20, 2020

Thanks for reporting - I agree this should be documented (or implemented in -Y). PRs are welcome.

@jtagcat
Copy link

jtagcat commented Apr 1, 2021

Duplicate of #28

Easiest way to do this seems to move the comment to a json object.

# this is yaml
i_am_yaml: # here is comment on same line
  bugbug: false # an another one
{
"yq_yaml_xml_toml_processor_metakey_making_this_long_preventing_keyname_collisions¤comment_singleline": "this is yaml"
"i_am_yaml": [
  "bugbug": false
  "yq_yaml_xml_toml_processor_metakey_making_this_long_preventing_keyname_collisions¤comment_sameline_bugbug": "an another one"
  ],
 "yq_yaml_xml_toml_processor_metakey_making_this_long_preventing_keyname_collisions¤comment_sameline_i_am_yaml": "here is comment on same line"
}

There're two flaws I can think of:

  • Keyname collisions. Just exit with error, if yq_yaml_xml_toml_processor_metakey_making_this_long_preventing_keyname_collisions¤ is encountered in yaml input.
  • (keyname lenght limit?)
  • jq filters; additional hacks to make jq pass through yq_yaml_xml_toml_processor_metakey_making_this_long_preventing_keyname_collisions¤ could be made.

There's probably a better way to solve this, but this is what came to head.

@kislyuk

This comment was marked as resolved.

@kislyuk
Copy link
Owner

kislyuk commented Feb 13, 2022

@jtagcat thanks for your suggestion. I agree with your approach and have previously tried to implement it. However the issue I am facing is that there is no obvious place to hook into the PyYAML parser to retrieve comments from the input stream (they get discarded too early in the process) and no obvious data model to emit the comments in the dumper.

I know ruamel.yaml has comment round-tripping, but I'm not sure if the parser/dumper modifications made there to accomplish this would be portable into our customizations of PyYAML. Ultimately this seems possible but might require the use of the slow pure-Python tokenizer in the parser, and might require extensive customization. With that said, PRs are welcome.

@kislyuk
Copy link
Owner

kislyuk commented Mar 13, 2022

@andrewcrook thanks for expanding upon your original comment, it is now legible as a contribution to this discussion.

While I appreciate the pointer to JSONC, the comment you linked on the jq wiki outlines why jq will not support it. Rewriting, repackaging, or replacing jq is outside the scope of this project, so none of the suggestions related to that are productive. You can check the comments by @jtagcat above for pointers to the course of action that we consider most likely to be productive. PRs are welcome.

@andry81
Copy link

andry81 commented Aug 8, 2022

A workaround for this issue through the bash scripts based on diff+patch approach: mikefarah/yq#515 (comment)

@kislyuk
Copy link
Owner

kislyuk commented May 27, 2024

For reference, this is where PyYAML throws out comments (this happens in the tokenizer, before any of the parsing or constructor steps that we hook into):

https://github.com/yaml/pyyaml/blob/48838a3c768e3d1bcab44197d800145cfd0719d6/lib/yaml/scanner.py#L778-L780

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

No branches or pull requests

4 participants