cue icon indicating copy to clipboard operation
cue copied to clipboard

cue/load: Instances (or similar function) should work like cmd/cue

Open myitcv opened this issue 4 years ago • 3 comments

Is your feature request related to a problem? Please describe.

A number of users of the Go API expect cue/load.Instances() to behave like cmd/cue does, i.e. automatic support for the argument parsing that allows all cue help filetypes to be loaded.

Currently, cue/load.Instances() only works with CUE packages.

Making it easy for people to write CLI applications that mirror the loading UX of cmd/cue also seems to be a beneficial thing because it will make that style more common and therefore more easily loaded from muscle memory.

Describe the solution you'd like

cue/load.Instances() (or a similar function if it doesn't make sense to augment Instances) to mirror loading capabilities described in cue help filetypes.

Describe alternatives you've considered

Every user of cue/load.Instances() that needs to support CUE, Yaml, JSON etc needing to write code similar to https://github.com/cue-lang/cue/issues/1381.

Additional context

It might well be the case that not all CLI applications using cue/load.Instances() want to support all filetypes (encodings) supported by CUE, hence this should be configurable in some way. This could perhaps be controlled via the *Config argument.

myitcv avatar Nov 30 '21 11:11 myitcv

A number of users of the Go API expect cue/load.Instances() to behave like cmd/cue does

There's a good reason for that. The doc comment for load.Instances says:

// Instances returns the instances named by the command line arguments 'args'.

It does not qualify that by saying that it only works on CUE files, so the only reasonable thing for a user to assume is that it would work on command-line arguments as accepted by the various cue subcommands, which all accept JSON, YAML etc.

The implementations of cue subcommands use load.Instances under the hood so it's clear that it's possible to use load.Instances to parse non-CUE files, but it's not clear how to do so. At the least, load.Instances should document what happens if a non-CUE file is included.

rogpeppe avatar Nov 30 '21 11:11 rogpeppe

I think cue/load.Instances() is actually working as expected. It's just that the extra lifting cmd/cue does with the resulting cue/build.Instance is not exposed anywhere (hence the workaround proposed in https://github.com/cue-lang/cue/issues/1381#issuecomment-971148539). So I think this ends up as much being about what API we want/need here, and how cue/load.Instances() (plus other helpers) fit into that picture.

myitcv avatar Nov 30 '21 12:11 myitcv

Linking here from #2262 (and others) as the canonical place for discussing ideas on how cue/load should align with cmd/cue and other places consistently.

myitcv avatar Jun 13 '23 16:06 myitcv