codegen

package
v0.5.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
const (
	ProtoDir    = "proto"
	APIGenDir   = "internal/api/gen"
	OpenAPIFile = "api/openapi/openapi.yaml"
)

Paths of the API generation, relative to the project root.

View Source
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.

Variables

View Source
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

func Buf(ctx context.Context, dir string, out io.Writer) error

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

func BufCheck(ctx context.Context, dir string) error

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

func DSN(dir, flag string) (string, error)

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

func Jet(ctx context.Context, dir, dsn string, out io.Writer) error

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 Protos

func Protos(dir string) ([]string, error)

Protos returns the proto files of the project.

func Queries

func Queries(dir string) ([]string, error)

Queries returns the query files of the project.

func Sqlc

func Sqlc(ctx context.Context, dir string, out io.Writer) error

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) error

SqlcCheck fails when the sqlc output does not match the queries: for CI.

func Templ added in v0.4.0

func Templ(ctx context.Context, dir string, out io.Writer) error

Templ regenerates the Go code of the templ files and removes the code of deleted ones.

func TemplCheck added in v0.4.0

func TemplCheck(ctx context.Context, dir string) error

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.

func Templates added in v0.4.0

func Templates(dir string) ([]string, error)

Templates returns the templ files of the project.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL