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

encoding/*: allow exporting comments #1180

Open
romange opened this issue Aug 1, 2021 · 9 comments · May be fixed by #1549
Open

encoding/*: allow exporting comments #1180

romange opened this issue Aug 1, 2021 · 9 comments · May be fixed by #1549
Labels
FeatureRequest New feature or request NeedsDesign Functionality seems desirable, but not sure how it should look like.

Comments

@romange
Copy link

romange commented Aug 1, 2021

Is your feature request related to a problem? Please describe.
Sometimes 3rd party systems require annotating configurations via comments.
For example, cloud-init requires that its yaml files have a she-bang line like this:

#cloud-config
<some yaml stuff>
......

See here https://cloudinit.readthedocs.io/en/latest/topics/examples.html for more examples.

Describe the solution you'd like
File test.cue

//cloud-config
foo: "bar"

hello: {
        // occupy
        world: "from mars?"
}

when exported via command cue export --out yaml --preserve-comments test.cue should produce something like this:

# cloud-config
foo: bar
hello:
  # occupy
  world: from mars?

Describe alternatives you've considered
There is an easy workaround when wrapping cue-cmd via bash script. It's suboptimal but it's not a blocker.

Additional context
Bonus point: Some comments may be relevant to cue language itself thus we do not really want to export them.
It could be nice to have a convention to export whitelisted comments. For example, only comments starting from /// will be exported.

@romange romange added FeatureRequest New feature or request Triage Requires triage/attention labels Aug 1, 2021
@myitcv myitcv changed the title allow exporting comments from cue into destination language encoding/*: allow exporting comments Aug 1, 2021
@myitcv myitcv removed the Triage Requires triage/attention label Aug 1, 2021
@myitcv
Copy link
Member

myitcv commented Aug 1, 2021

This request seems reasonable to me for one main reason: we import comments, hence we should export them for round-trip-ness' sake if nothing else.

FWIW it's not possible to control the export comments at all (unless I am missing something): neither through cmd/cue, cue cmd, nor encoding/*.

@myitcv myitcv added the Discuss Requires maintainer discussion label Aug 1, 2021
@verdverm
Copy link

verdverm commented Aug 1, 2021

@myitcv I agree that this makes a lot of sense for the round-trip-ness. I also did not see any way to export comments. My example above was a modified cue ( add cue.Docs(true) to this line https://github.com/cue-lang/cue/blob/master/pkg/encoding/yaml/manual.go#L38).

Some thoughts after poking around

  1. The example above adds a space within the comment string, I think we might consider removing the space here: https://github.com/cue-lang/cue/blob/master/internal/encoding/yaml/encode.go#L300 though i
  2. Tt might be worth considering how we might preserve the spacing / padding in the original sources better. Does this need to happen for import as well?
  3. My initial thought was to use the Options variadic func args, much like Syntax from the API since that is used where the change is needed. That being said, I'm not sure it makes sense to open up the Options that much for the encoders, as incorrect Options may cause errors in the encoder itself.
  4. Does it make sense to allow different options depending on the output? I was mainly thinking that the CUE output could support the full set of Options, not sure if there is any overlap with CUE output and the feature request to support single file output for CUE we have seen. Protobuf looks to have some extra flags for exporting already.

@mpvl mpvl added NeedsDesign Functionality seems desirable, but not sure how it should look like. and removed Discuss Requires maintainer discussion labels Nov 24, 2021
@mpvl
Copy link
Member

mpvl commented Nov 24, 2021

Makes a lot of sense. One alternative API is to export comments when used with def. I'm surprised that cue def --out yaml doesn't already export comments, actually.

@mpvl mpvl added the good first issue Good for newcomers label Nov 24, 2021
felixge added a commit to felixge/cue that referenced this issue Feb 24, 2022
@felixge felixge linked a pull request Feb 24, 2022 that will close this issue
felixge added a commit to felixge/cue that referenced this issue Feb 24, 2022
Closes cue-lang#1180

Signed-off-by: Felix Geisendörfer <felix@felixge.de>
@felixge
Copy link

felixge commented Feb 24, 2022

I just ran into this problem and submitted a PR for it based on the approach suggested by @verdverm. I'm happy to adjust it if a different approach is preferred.

#1549

@rogpeppe rogpeppe removed the good first issue Good for newcomers label Apr 28, 2022
@rogpeppe
Copy link
Member

@felixge I was about to land your PR but first needed to fix a test case. In fixing that, I realised that perhaps this issue isn't as straightforward as it might seem.

Here's the test case in question: https://review.gerrithub.io/c/cue-lang/cue/+/536265/2/cmd/cue/cmd/testdata/script/cmd_github.txt

Note all the comments that have appeared in the YAML output for the test (a Github Actions file). Those comments aren't really relevant to the output file - they're really there to describe the schema of the actions file, not the final rendered result. In the final rendered result, I'd imagine that more repository-specific comments would be appropriate.

Choosing a heuristic that decides which comments to be rendered in the final concrete output is tricky and will need some design input. Perhaps we might need to use an annotation hint; it could even be that it's not, after all, appropriate to include comments in rendered output.

So I've left the NeedsDesign label on this issue and removed the GoodFirstIssue label because it's clearly not entirely trivial!

Thanks for your input. Any further thoughts on this would be appreciated.

@felixge
Copy link

felixge commented May 8, 2022

@rogpeppe I agree that this feature would benefit from a sophisticated design. But in the short term, I don't see the harm of letting the user chose to either include comments or not when they invoke the cue command (via a flag). It seems like most use cases would be well served by this?

@nyarly
Copy link

nyarly commented Apr 15, 2023

So long as we're in a NeedsDesign stage - got here because I was looking to produce comments based whether CUE processing of input data had fallen back on default values.

I realize that's a whole extra can of worms, but perhaps something like this could be made to work:

struct: {
  value: 17 @comment("from default")
}

@denniskern
Copy link

Hi Guys - Are there any ideas of how to proceed with this issue? It would be so nice to be able to export comments from a cue file.

@amackenzie
Copy link

This might be slightly out of scope, but...

To add a small comment, the OP brought up shebangs, and there's at least a few places where shebangs are used regardless of their validity in the normal export format. The one that comes to mind is Saltstack -- if you're using anything other than the Jinja|YAML renderer, you need a shebang at the top to designate it, eg, #!json, even though that isn't valid JSON. (And hypothetically you'd put a #!cue line at the top if there's a working cue renderer for Saltstack.)

So maybe in addition to comments there might need to be some consideration or syntax for fixed annotations of whatever format in outputs? It may affect importing if you want to be able to round-trip data, too, though at least in the specific case of shebangs they're generally the first line so it's skippable...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request NeedsDesign Functionality seems desirable, but not sure how it should look like.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants