codegen

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmitDBFile

func EmitDBFile(models []ModelInfo, dbPkgName string) string

EmitDBFile generates the aggregated DB struct file that wires up all repositories. dbPkgName is the Go package name for the output file (e.g. "db").

func EmitModelFile

func EmitModelFile(m ModelInfo) string

EmitModelFile generates the complete per-model _drel.go file content.

func EmitModelFileChecked

func EmitModelFileChecked(m ModelInfo) (string, error)

EmitModelFileChecked generates the per-model file, returning an error if the model uses unsupported features.

func Generate

func Generate(configPath string) error

Generate runs the full code generation pipeline: loads config, scans packages, emits per-model files, and emits the aggregated DB file.

func GenerateCreateTable

func GenerateCreateTable(m ModelInfo, fks map[string]string) string

GenerateCreateTable emits a CREATE TABLE statement for a single model. fks maps column names to referenced table names for foreign key constraints.

func GenerateDropSchema

func GenerateDropSchema(models []ModelInfo) string

GenerateDropSchema emits DROP TABLE statements in reverse order to respect foreign key dependencies.

func GenerateSchema

func GenerateSchema(models []ModelInfo) string

GenerateSchema emits the full schema DDL for a slice of models, including enum type definitions, foreign key constraints, and many-to-many pivot tables.

func GoTypeToSQL

func GoTypeToSQL(goType string) string

GoTypeToSQL maps a Go type string to its corresponding Postgres SQL type. Pointer types are unwrapped to their base type. Unknown types default to "text".

Types

type Config

type Config struct {
	Packages []string     `yaml:"packages"`
	Output   OutputConfig `yaml:"output"`
	Dialect  string       `yaml:"dialect"`
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

type FieldInfo

type FieldInfo struct {
	Name           string
	GoType         string
	ColumnName     string
	IsExported     bool
	RelTag         string
	Relation       *RelationFieldInfo
	IsVO           bool   // implements sql.Scanner + driver.Valuer (single-column VO)
	IsMultiColVO   bool   // implements drel.MultiColumnMapper (multi-column VO)
	MultiColPrefix string // db tag used as column prefix for multi-column VOs
	LocalGoType    string // type name without package qualifier, for same-package generated code
	TypePkgPath    string // import path of the field type's package (empty for primitives/same-package)
	IsPointer      bool   // whether the field is a pointer type
	IsEnum         bool
	EnumValues     []string
}

type ModelInfo

type ModelInfo struct {
	Name          string
	PkgPath       string
	PkgName       string
	PKType        string // display type for generated code (e.g., "int", "uuid.UUID")
	PKTypeFull    string // fully qualified (e.g., "github.com/google/uuid.UUID")
	PKTypePkg     string // import path for external PK types (empty for primitives)
	TableName     string
	Fields        []FieldInfo
	HasSoftDelete bool
	HasVersioned  bool
	HasAudit      bool
	Dir           string // filesystem directory of the package
}

func ScanPackages

func ScanPackages(patterns []string, dir ...string) ([]ModelInfo, error)

type OutputConfig

type OutputConfig struct {
	DB         string `yaml:"db"`
	Migrations string `yaml:"migrations"`
}

type RelationFieldInfo

type RelationFieldInfo struct {
	Type        string // "has_many", "has_one", "belongs_to", "many_to_many"
	FK          string // foreign key column
	JoinTable   string // for many_to_many
	RefColumn   string // target FK column in pivot table (many-to-many)
	TargetModel string // target model name for foreign key resolution
}

RelationFieldInfo holds parsed relationship metadata from a `rel:"..."` struct tag.

Jump to

Keyboard shortcuts

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