modelinspect

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: 18 Imported by: 0

Documentation

Overview

Package modelinspect inspects a project's models by running them. It builds a program into the project's module that loads the model packages, has gorm parse every model and reports what only the compiled models can tell: the database columns behind the fields, for the column references gg gen writes, and for gg check what the index declarations violate on MySQL, which framework columns a model renames and which query names it declares ambiguously. Both lines of gg depend on this package and on nothing of each other's. An inspection is cached by its inputs under the user cache directory, so a project one command inspected is not built again by the other.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheDir

func CacheDir() (string, error)

CacheDir returns the directory holding the cached inspection results of the project in the working directory.

Types

type ColumnInfo

type ColumnInfo struct {
	GoName      string            `json:"go_name"`
	DBName      string            `json:"db_name"`
	TypeExpr    string            `json:"type_expr"`    // Source-level type expression, empty when the type cannot be reproduced.
	TypeImports map[string]string `json:"type_imports"` // The imports TypeExpr needs, by path, each under the package name it qualifies the type with; none for builtin or same-package types.
	TypeName    string            `json:"type_name"`    // Original type, recorded in a comment when TypeExpr is empty.
	Numeric     bool              `json:"numeric"`      // modelschema.ClassifyColumn calls the column numeric, so the reference gains SUM and AVG.
	Time        bool              `json:"time"`         // modelschema.ClassifyColumn calls the column a time, so the reference gains time bucketing.
	JSONStrings bool              `json:"json_strings"` // Column type stores a JSON array of strings, which gets the JSONStringsColumn reference.
}

ColumnInfo is one column of a model, as the inspection program reports it.

type ModelColumns

type ModelColumns struct {
	PkgPath                  string       `json:"pkg_path"`
	PkgName                  string       `json:"pkg_name"`
	Name                     string       `json:"name"`
	Columns                  []ColumnInfo `json:"columns"`
	IndexViolation           string       `json:"index_violation"`
	TenantIndexViolation     string       `json:"tenant_index_violation"`
	FrameworkColumnViolation string       `json:"framework_column_violation"`
	QueryNameViolation       string       `json:"query_name_violation"`
	IDAccessorViolation      string       `json:"id_accessor_violation"`
}

ModelColumns is one model as the inspection program reports it: its columns, what model.ValidateIndexes refused in its index declarations on MySQL, what modelschema.ValidateTenantIndexes refused in its unique indexes, what modelschema.ValidateFrameworkColumns refused in its columns, what urlquery.ValidateQueryNames refused in its query tags and what modelregistry.ValidateIDAccessors refused in its ID accessors, each "" when nothing.

func Inspect

func Inspect(module string, modelDir string, models []*modelinfo.Model, ignore gghelper.ProjectIgnore, overlay func() (map[string]string, error)) ([]ModelColumns, bool, error)

Inspect inspects the project's models by running them: the inspection program (see buildProgram) is built into the project's module and reports every model's columns, what model.ValidateIndexes refused in its index declarations on MySQL, what modelschema.ValidateTenantIndexes refused in its unique indexes, what modelschema.ValidateFrameworkColumns refused in its columns, what urlquery.ValidateQueryNames refused in its query tags and what modelregistry.ValidateIDAccessors refused in its ID accessors. The result comes from the cache when every input of the inspection is unchanged, otherwise from a build and run of the program. The build takes the files overlay returns in place of the project's own, which gg gen uses to stub out the column files it is about to rewrite; a nil overlay builds the project as it is. A project that does not depend on the framework yet cannot be inspected and reports inspected false: the program imports the project's models against the framework, and such a project cannot hold column references either.

Jump to

Keyboard shortcuts

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