Documentation
¶
Overview ¶
Package codegen runs the external generators of the postgres module: sqlc for static queries and jet for dynamic ones.
Both run through go tool, at the versions the project pins in go.mod, so every developer and CI produce the same code.
Index ¶
- Constants
- Variables
- func Buf(ctx context.Context, dir string, l APILayout, out io.Writer) error
- func BufCheck(ctx context.Context, dir string, l APILayout) error
- func DSN(dir, flag string) (string, error)
- func Jet(ctx context.Context, dir, dsn string, out io.Writer) error
- func Protos(dir string, l APILayout) ([]string, error)
- func Queries(dir string, l SqlcLayout) ([]string, error)
- func Sqlc(ctx context.Context, dir string, l SqlcLayout, out io.Writer) error
- func SqlcCheck(ctx context.Context, dir string, l SqlcLayout) error
- func Templ(ctx context.Context, dir string, out io.Writer) error
- func TemplCheck(ctx context.Context, dir string) error
- func Templates(dir string) ([]string, error)
- type APILayout
- type SqlcLayout
Constants ¶
const ( ProtoDir = "proto" APIGenDir = "internal/api/gen" OpenAPIFile = "api/openapi/openapi.yaml" OpenAPIDir = "api/openapi" // OpenAPIMark starts every file of the platform's OpenAPI generator, which writes a // folder of them: generation removes the marked files before buf writes the // current ones, so the yaml of a deleted proto file goes with it. OpenAPIMark = "# Code generated by protoc-gen-platform-openapi." )
Paths of the API generation, relative to the project root.
const ( QueriesDir = "db/queries" MigrationsDir = "db/migrations" SqlcConfig = "db/sqlc.yaml" SqlcOut = "internal/db/sqlcgen" JetOut = "internal/db/jetgen" )
Paths of the query generation, relative to the project root. The sqlc ones are the defaults; a project moves them with the sqlc option of the postgres module.
Variables ¶
var DefaultAPI = APILayout{ProtoDir: ProtoDir, OpenAPI: true}
DefaultAPI is the layout of a project that keeps the defaults.
var DefaultSqlc = SqlcLayout{Config: SqlcConfig, Queries: QueriesDir, Out: SqlcOut}
DefaultSqlc is the layout of a project that keeps the defaults.
var GoCommand = func(ctx context.Context, dir string, out io.Writer, args ...string) error { cmd := exec.CommandContext(ctx, "go", args...) cmd.Dir = dir var stderr bytes.Buffer cmd.Stdout = out cmd.Stderr = &stderr if err := cmd.Run(); err != nil { msg := strings.TrimSpace(stderr.String()) if strings.Contains(msg, "missing go.sum entry") || strings.Contains(msg, "no required module provides") { msg += "\nrun platformgo apply to add the generators to go.mod and go.sum" } return fmt.Errorf("go %s: %w\n%s", strings.Join(args, " "), err, msg) } return nil }
GoCommand runs the go command. Tests replace it.
Functions ¶
func Buf ¶
Buf regenerates the Go code and the OpenAPI description of the proto files. Without proto files it only removes what was generated before.
func BufCheck ¶
BufCheck fails when the generated API code does not match the proto files: for CI. It generates into a scratch directory and compares, so the project is not touched.
func DSN ¶
DSN finds the database address for generation: the flag, then DATABASE_URL, then the .env and .env.example of the project — the same order make run uses.
func Jet ¶
Jet regenerates the dynamic query builder from the database at dsn. The schema must already be migrated; the tables of goose, River and platform modules are left out.
jet writes into <path>/<database>/<schema>. The database name differs between developers, and it would end up in import paths, so the output is generated into a scratch directory and its packages are moved straight under internal/db/jetgen.
func Queries ¶
func Queries(dir string, l SqlcLayout) ([]string, error)
Queries returns the query files of the project.
func Sqlc ¶
Sqlc regenerates the static queries. Without query files it only removes what sqlc generated before, so deleting the last query leaves nothing behind.
func SqlcCheck ¶
func SqlcCheck(ctx context.Context, dir string, l SqlcLayout) error
SqlcCheck fails when the sqlc output does not match the queries: for CI.
func Templ ¶ added in v0.4.0
Templ regenerates the Go code of the templ files and removes the code of deleted ones.
func TemplCheck ¶ added in v0.4.0
TemplCheck fails when the Go code of a template is stale or belongs to a deleted one. It generates a copy of the templates in a scratch directory — the generated code names each template by its path under the generated root, so the copy keeps the layout — and compares, so the project is not touched.
Types ¶
type APILayout ¶ added in v0.9.0
type APILayout struct {
ProtoDir string // relative to the project root
OpenAPI bool // buf writes api/openapi/openapi.yaml; false leaves it to the project
Split bool // the platform's generator: marked yaml files all over api/openapi
}
APILayout is where the proto files of a project are and whether generation writes the OpenAPI description.
type SqlcLayout ¶ added in v0.9.0
SqlcLayout is where the sqlc configuration, the queries and the generated code of a project live, relative to the project root.