codegen

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package codegen implements `quark gen`: it parses a user's package with go/packages + go/types (not reflection, so the tool can be `go install`ed and driven from //go:generate) and emits, per package, a quark_gen.go that registers typed implementations with the runtime registry in package quark.

F6-1 ships the pipeline and the registration contract only: the emitted scanner/binder are inert stubs (quark.StubScanner / quark.StubBinder) and the runtime hot paths do not yet consult the registry. The typed fast path is F6-2 (scanning) and F6-3 (binding). See ADR-0014.

Index

Constants

View Source
const GeneratedFileName = "quark_gen.go"

GeneratedFileName is the file each package's registrations are written to.

Variables

This section is empty.

Functions

func Generate

func Generate(patterns ...string) ([]string, error)

Generate loads the packages matching patterns and writes a quark_gen.go into each package that contains models. It returns the paths written.

func Render

func Render(pm PackageModels) ([]byte, error)

Render produces the gofmt'd contents of a package's quark_gen.go. The file stamps the contract version it was generated against (a literal, not a reference to GenContractVersion, so a stale file keeps its old version and the runtime's version gate can detect it), registers per model a version+hash, a typed scanner, and — for integer-PK models — a typed INSERT binder (other models register the stub binder and bind via reflection).

Types

type ModelDef

type ModelDef struct {
	Name   string
	Fields []quark.ModelField
	Hash   string
	// ColTypes holds, parallel to Fields, the Go type of each field rendered
	// for use *inside the model's own package* — i.e. with the local package
	// qualifier stripped ("Status", not "sample.Status"). This is what the
	// generated typed-column accessors (F6-4) parameterise over. It is kept
	// separate from quark.ModelField.GoType, which stays qualified for every
	// package so it matches reflect.Type.String() in the conformance hash.
	ColTypes []string
}

ModelDef is a model struct discovered in a package: a struct with at least one exported field carrying a `db` tag.

type PackageModels

type PackageModels struct {
	PkgPath string
	PkgName string
	Dir     string
	Models  []ModelDef
	// Imports maps import path -> package name for every non-local package
	// referenced by a field's ColType (e.g. "time" -> "time"). The emitter
	// merges these with the always-needed imports so the generated
	// typed-column declarations compile.
	Imports map[string]string
}

PackageModels groups the models found in one package, with the on-disk directory where its quark_gen.go should be written.

func Load

func Load(patterns ...string) ([]PackageModels, error)

Load type-checks the packages matching patterns and returns those that contain at least one model. Patterns use go/packages syntax (e.g. "./...", an import path, or a directory).

Directories

Path Synopsis
Package sample holds a representative model used by the codegen tests: the conformance test loads it via go/packages (AST) and via reflection, and the golden test regenerates its quark_gen.go and compares.
Package sample holds a representative model used by the codegen tests: the conformance test loads it via go/packages (AST) and via reflection, and the golden test regenerates its quark_gen.go and compares.

Jump to

Keyboard shortcuts

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