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.14k stars 294 forks source link

Cue commands that generate json/other files should have the option --compact #2477

Open rudifa opened 1 year ago

rudifa commented 1 year ago

Is your feature request related to a problem? Please describe. While commands def, export and eval generate indented json files, in some applications the compact (minified) json is desirable, for space and time economy.

Describe the solution you'd like cue export data.cue --compact should produce a compact data.json file.

Describe alternatives you've considered I can reformat the indented json file with jq or any similar tool, but getting it on demand from cue would be more efficient.

Additional context The only mention of compact in CUE issues and in pull requests I could find is in PR #1549, suggested as a possibility by @mpvl and commented on favourably by @rogpeppe and @felixge.

While the desired effect of --compact is obvious for json, for other formats discussed in #1549 probably should be 'minify', i.e. remove anything that conveys no machine readable information.

myitcv commented 1 year ago

Modulo naming, this seems a reasonable feature request.

jpluscplusm commented 1 year ago

Please include the comma-delimited, white-space-removed (comments removed?) single-line form of CUE in the set of output formats being targeted by this Issue's putative option.

myitcv commented 1 year ago

Updated the title to reflect that this request is not specific to JSON, but could/should be applied to all output formats.

rudifa commented 1 year ago

2492 was more a convergent request than a duplicate of this one.

I limited my request to JSON because it corresponds to the immediate need of my project, but

Questions, for my CUEducation:

jpluscplusm commented 1 year ago

[snip]

Questions, for my CUEducation:

Unofficial answers from an enCUEsiast, rather than someone who definitively knows what they're taking about:

  • is "comma-delimited, white-space-removed single-line form of CUE" format currently supported on input to CUE?

Yes. If you use the rudimentary tr|tr|sed I outlined in the duplicate Issue (after having manually removed any CUE comments), you /should/ find it transforms simple enough CUE texts into acceptable single-line equivalents. As noted, it doesn't handle multi-line strings or comments, and I'm not even sure that comments /can/ be handled in a single-line form.

I'm confident there are also other corner cases it fails to deal with.

  • if yes, where is this format specified?

This section of the language guide might be useful: https://cuelang.org/docs/references/spec/#lexical-elements

I didn't use that, however - it was more trial and error :-)

  • or would this be a display-only format?

I'm not sure a display-only form of the language exists. Personally, I'm only interested in a form that can be used as input.

rudifa commented 1 year ago

@jpluscplusm Thank you for the guidance, I'll follow it up (not as my first priority).

By 'display-only format' I meant something like this, for use in testing

    assert.Equal(`{·str: "hello world"·num: 42·flt: 3.14·"k8s.io/annotation": "secure-me"·list: ["a", "b", "c", 1, 2, 3]·obj: {·foo: "bar"·L: ["a", "b", "c", 1, 2, 3]·}·}`,
        myfmt.CompactCueVal(cueval), "-")
yujunz commented 1 year ago

Is there anyone already working on this?