codegen

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 14 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"
	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.

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. The sqlc and jet ones are the defaults; a project moves them with the sqlc and jet options of the postgres module.

Variables

View Source
var DefaultAPI = APILayout{ProtoDir: ProtoDir, OpenAPI: true}

DefaultAPI is the layout of a project that keeps the defaults.

View Source
var DefaultSqlc = SqlcLayout{Config: SqlcConfig, Queries: QueriesDir, Out: SqlcOut}

DefaultSqlc is the layout of a project that keeps the defaults.

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, l APILayout, 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, l APILayout) 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, target, 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 target, a folder relative to the project root: JetOut unless the project moved it.

func Protos

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

Protos returns the proto files of the project.

func Queries

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

Queries returns the query files of the project.

func Sqlc

func Sqlc(ctx context.Context, dir string, l SqlcLayout, 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, l SqlcLayout) 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

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

type SqlcLayout struct {
	Config  string
	Queries string
	Out     string
}

SqlcLayout is where the sqlc configuration, the queries and the generated code of a project live, relative to the project root.

Jump to

Keyboard shortcuts

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