A First Example

In our first example we will build a hof generator for a simple Golang REST server. This subsection will take you through the steps to both create and use code generators.

There are two sides to generators:

  • implementors who write schemas and templates as reusable Cue modules
  • users who write designs, customize output, and develop applications

In the first half of this section, we will take on the role of the generator creator. We will use the generator in the second half.

  • define a generator
  • write a schema for your generator
  • write templates to implement the schema
  • write a design to use the generator
  • use the output and add custom code
  • iterate on applications

You can find the source for this example on GitHub.

The directory layout is as follows

# source location in the website repo

  # The generator module
    gen/         # generator definition
    schema/      # schema for a REST server
    templates/   # templates for files
    partials/    # common partial templates

  # The generator usage
    # inputs to hof
    hof.cue      # entrypoint for hof generation
    design/      # the server design
    # output and custom
    server/      # the server code
    config/      # config for our server
    sercret/     # secrets for our server
    seeds/       # database seed data

  # Snippets used in the progress of this section


Last modified March 18, 2021: commit the current (b4be254)

2021 Hofstadter, Inc