columns

package
v0.0.0-...-8c930d8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package columns generates the typed column references of a project's models, so filters name columns through the compiler instead of through string literals. Every model file declaring a model with columns gets a generated file of the same name declaring a <Model>Cols var per such model: model/sample/record.go, declaring Record, gets model/sample/record.gen.go with RecordCols, and a model without columns, such as one embedding model.Empty, gets no var. The columns come from gorm's own schema parser, run inside the project's module against the project's real model types by the inspection of package modelinspect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InspectionOverlay

func InspectionOverlay(module string, modelDir string, models []*modelinfo.Model, ignore gghelper.ProjectIgnore) (map[string]string, error)

InspectionOverlay returns the build overlay a program compiling the model packages before this run writes its generated files uses: the column inspection program (see Generate), and the type check the protobuf derivation runs for gg gen and gg prune.

Previously generated files — the column files, and the registration and api doc files of the model directory — are replaced by stubs holding only their package clause: they may carry an API shape older than the running gg or name a model deleted since, and the build must not choke on the very files the run rewrites. A first run has no generated files at all. Either way, handwritten model code that reads the column references, such as a hook updating its model through the Cols var, would fail the build that produces them.

The overlay breaks that cycle the way a type checker skipping function bodies does: every declaration that depends on a generated column reference, directly or through other package-level declarations, is left out of the inspection build. Leaving it out cannot change the resolved columns, which would otherwise depend on themselves. A function keeps its signature and gets a body that panics should the inspection call it; an init function gets an empty body, since the runtime runs every init; a package-level var is dropped, which leaves out whatever reads it in turn. Imports only the left-out code used become blank imports, keeping their initialization side effects. Every rewrite keeps the line breaks it replaces, so compiler messages about the remaining code point at the right lines.

Dependence is decided by name, without type information. A local variable that happens to share an omitted name leaves its function out too, which costs nothing: the inspection calls project code only through the few model methods it reaches through interfaces. Methods are never omitted by name, since a method is selected through a value whose type is unknown here, so a package-level var or an init function calling a left-out method reaches its placeholder. Only the model directory is rewritten, the same scope the inspection cache key covers.

Types

type Result

type Result struct {
	// Written lists the files written because they were missing or their
	// content changed; a file that is already current is not rewritten.
	Written []string

	// Removed lists the generated files removed because their source file
	// declares no model with columns any more (see removeOrphanColumnFiles).
	Removed []string
}

Result lists the column files a Generate run changed on disk.

func Generate

func Generate(module string, modelDir string, models []*modelinfo.Model, ignore gghelper.ProjectIgnore) (Result, error)

Generate writes one .gen.go file per model source file declaring a model with columns, removes the generated files of the sources that no longer declare one, and returns the files it wrote and removed. Generated files are framework-owned for their whole life cycle: projects never create, edit, or clean them up. A model file of the model directory named like one of the directory's own generated files is refused before anything is written, see registrationFileOwner. When it fails, the result still lists what it wrote and removed before the failure.

Jump to

Keyboard shortcuts

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