With CUE

Code Gen with CUE

  • There are several types of “code gen” in relation to CUE
  • data can use export with –out or –outfile .
  • arbitrary uses same text/template
  • cue export -e content –out text > file.txt
  • write with tool/os


  • formatting
  • diff3 & custom code
  • expanded template helper functions
  • shared partial templates, can still use partials defined within the template
  • always writes, important when you use hot reload or make that checks FS events to compile

There are several types of “code gen” in relation to CUE

We should talk about these before focusing on hof.

Configuration or data (yaml,json)

  • Export: CUE -> data (cue export)
  • Import: data -> CUE (cue import)

Language types:

  • Get Go: Go -> CUE (cue get go)
  • TypeGen: CUE -> Go (… custom …)

some notes: (1) CUE only has support for importing Go right now (2) CUE only imports types from Go (3) CUE does not have functions, so we cannot represent those in CUE without a DSL

Translating CUE to type (class or struct) is a challenging problem generally.

It really can’t capture all of the nuances in the vanilla form. We need to do something more complex.

The Hof Method

Some helpful links

Once first-example is done, several production versions of the concepts therein will be made into hofmods

With CUE

There are also ways to generate types without hof

  • one could do essentially the same thing hof is doing in pure CUE, though less sophisticated
  • If you are willing to write Go, you can walk the AST and/or cue.Value to do some things. Attributes would make this more interesting.
  • hof leaves more to the data model schema + generator, so it can be more flexible without modifying the tool. This should also be composable. I plan to have an example of that in the first-example/using-a-database or maybe another section

There are also discussion and issues on CUE GitHub.

TODO, respond to