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

cmd/cue: generate Go code from CUE #6

Open
cueckoo opened this issue Jul 3, 2021 · 5 comments
Open

cmd/cue: generate Go code from CUE #6

cueckoo opened this issue Jul 3, 2021 · 5 comments
Labels
FeatureRequest New feature or request

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @mpvl in cuelang/cue#6

Or really any programming language, but Go is a good first target.

  • Generate Go structs (with comments) from CUE definitions
  • Generate validation code given the CUE constraints defined on these definitions (well, its the same thing in CUE, but for Go it needs to be split out)
@cueckoo cueckoo added the FeatureRequest New feature or request label Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @tredoe in cuelang/cue#6 (comment)

This would be a great feature.
At my case, I'm exporting the Cue file to Json, for then to use 'jsonutil' (https://github.com/bashtian/jsonutils) to generate the Go struct where I load the Json data.

Now, I'm using Cue to deal with configuration files and it's the best tool that I've tried. But I've in mind to use it at data validation.

Congratulations!

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#6 (comment)

Thanks for your kind words.

Interesting, I expected the code generation aspect would be more desirable. So note that in the mean time there is https://godoc.org/cuelang.org/go/encoding/gocode, which gets you part of the way. But that assumes the schema are already defined.

Generating just the structs should be quite straightforward, though. That would also allow you to retain documentation. It would be implemented using the above-mentioned API. This could then be used to implement a command line feature in the cue tool.

@schmurfy
Copy link

I have started playing with cue and I like it, my idea was to first describe the schema of my inputs as a cue file and from there use it to generate to go struct and then validate the input & fill the go structure but I cannot manage to generate go struct from my cue code.

My first resource was https://cuelang.org/docs/integrations/go/#generate-go-code which is not that helpful since I have no clue what instance is and how to produce it, I made a few attempts but they all fail and there is no documentation about this I could find.

I like the concept of cue but it generated so much frustration that I think I will keep away from it for now until it matures.

@verdverm
Copy link

verdverm commented Dec 12, 2021

This is still a desired feature. It is unclear how the general concept would look with multiple language targets. For example, how does one express OOP vs Go type relations, or relations in the context of databases. What about public vs private visibility? The rules are quite varied across the languages and many have concepts that are not representable in CUE without at least attributes.

I have been using text/template with custom type schemas. It is a little more verbose, i.e. not using CUE's builtin constraints, you have to specify them in the schema.

Some links to what I have been doing:

In the doc you referenced, an instance is a cue.Value. The example will generate a function to validate an existing Go type based on a CUE value. It is likely not what you are looking for if you want to generate Go types directly from CUE values.

@schmurfy
Copy link

Thanks, I will have a look but if this is not currently supported out of the box it should be mentioned explicitly in the documentation, the documentation page I linked is not that.

@myitcv myitcv added the zGarden label Jun 13, 2023
@mvdan mvdan removed the zGarden label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants