cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.02k stars 286 forks source link

Allow for controlling quote style when exporting to YAML #2727

Open willramirezdev opened 9 months ago

willramirezdev commented 9 months ago

Is your feature request related to a problem? Please describe. This builds on the conversation in 1368.

When using CUE to export to YAML, there are times when controlling the quote style is required. In this playground example, the wildcard entry is always export wrapped in single quotes ('*.exmaple.com'). In our use case, there are downstream tools which require double quotes ("*.example.com").

Describe the solution you'd like Allow some way to control quote style. In 1368 there were some discussions about controlling this at the file level, but even some sort of field-level controls would would in our case.

Describe alternatives you've considered As far as I'm aware, there are no ways to control this using CUE itself. There are some recommendations in Slack to implement some sort of post processing: find and replace on magic strings, use another format.

Additional context N/A

mvdan commented 9 months ago

In our use case, there are downstream tools which require double quotes ("*.example.com").

Can you please expand on this a bit? If there's a program that takes in YAML, are you saying that it only accepts double quotes and not single quotes? I don't think I've heard of partial YAML support like that before.

In #1368, Marcel also suggests eporting as JSON - it always double quotes strings, and YAML is a superset of JSON, so presumably that could be enough to satisfy another program which only knows how to consume double-quoted YAML strings.

willramirezdev commented 9 months ago

@mvdan yeah, in our case the tool downstream is setup with a linter to only allow a specific sub-set of YAML.

To be clear: I don't think CUE is doing anything "wrong", but I suspect there will continue to be edge cases as long as there's no control over quote style. #2720 is another example where the fix was editing a specific regex within CUE to enforce quotes.

In our case we followed Marcel's advice and changed our tooling to export JSON, so we have a workaround for now.

ryepesg commented 2 months ago

I am having the same issue. My use case is a regex that OpenTelemetry interprets different when the quotes are removed from the regex used in https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/fc363988211d90ee2ae7c75eda6ce90c04bae868/processor/redactionprocessor/config.go#L25

mvdan commented 3 weeks ago

In our case we followed Marcel's advice and changed our tooling to export JSON, so we have a workaround for now.

I would suggest this as the better approach for naive consumers/parsers on the other side which don't actually know how to parse YAML correctly. JSON is a much simpler syntax, so it fits that use case better, and it is a valid subset of YAML so it shouldn't break any valid YAML parsers either.