A Full Example



We expand our first example into hof module form. We can import and resuse the generator. We will also see typical workflow patterns when working with hof code generation.

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.

Implementor
  • define a generator
  • write a schema for your generator
  • write templates to implement the schema
user
  • 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

project layout

# source location in the website repo
code/getting-started/first-example/

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

  # The generator usage
  app/
    # 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
  tmp/

Subsections


2021 Hofstadter, Inc