cmd/cue: generate Go code from CUE
Originally opened by @mpvl in https://github.com/cuelang/cue/issues/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)
Original reply by @tredoe in https://github.com/cuelang/cue/issues/6#issuecomment-531558455
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!
Original reply by @mpvl in https://github.com/cuelang/cue/issues/6#issuecomment-533715260
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.
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.
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:
- https://cuetorials.com/first-steps/generate-all-the-things/
- https://github.com/hofstadter-io/hof (CUE powered code gen tool)
- https://github.com/hofstadter-io/hofmod-server ( hof gen for a Golang server, including types )
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.
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.