Documentation
¶
Index ¶
- Constants
- Variables
- func FieldTypeToGoType(ft model.FieldType, ref string) string
- func ToSnakeCase(s string) string
- type Exporter
- type FieldInfo
- type Generator
- func (g *Generator) ExportSQL(root string, exporter Exporter) (string, error)
- func (o *Generator) GenerateForFile(infos []StructInfo, sourceFile string) error
- func (g *Generator) GenerateForStruct(structName string, goFile string) error
- func (g *Generator) MainInputFileRelativePath() string
- func (g *Generator) Models(root string) ([]model.Model, error)
- func (g *Generator) Name() string
- func (g *Generator) NewFileEvent(fileName, extension, filePath, event string) error
- func (g *Generator) Run() error
- func (g *Generator) ScanModules(rootDir string) error
- func (g *Generator) SetFinder(f modfind.Discoverer)
- func (g *Generator) SetLog(fn func(messages ...any))
- func (g *Generator) SetRootDir(dir string)
- func (g *Generator) SetSkipTidy(skip bool)
- func (g *Generator) SetSyncer(s SchemaSyncer)
- func (g *Generator) SupportedExtensions() []string
- func (g *Generator) UnobservedFiles() []string
- type ModuleInfo
- type ProbeRunner
- type RulesInfo
- type SchemaSyncer
- type StructInfo
Constants ¶
const ( DefinitionFile = "definition.go" GeneratedSuffix = "_gen.go" )
DefinitionFile is the one file name definitionc reads: every model.Definition of a package lives in it. GeneratedSuffix ends the file it writes next to it (definition.go → definition_gen.go).
const (
GeneratedHeader = "// DO NOT EDIT. generated by webtyp.com/definitionc"
)
Variables ¶
var ErrNoModelsFound = fmt.Err("no", "models", "found")
ErrNoModelsFound is returned by ExportSQL when the target directory has no definition.go files (or none define an exported, non-NoDB model). Signals "nothing to export" distinctly from a successful empty schema.
Functions ¶
func ToSnakeCase ¶
Types ¶
type Exporter ¶
Exporter is implemented by SQL adapter compilers (sqlt, postgres) that can render a full schema export. Defined here — not imported from ddlc — per Go convention: the consumer of a single-method interface owns the interface, not the implementer.
type FieldInfo ¶
type FieldInfo struct {
Name string
ColumnName string
Type model.FieldType
KindConstructor string
KindImportPath string // resolved import path of the constructor's package (empty for model.* builtins)
KindImportAlias string // alias/selector used in the scanned source (e.g. "input" in "input.Email()")
KindArgIdents []string // bare identifiers passed as direct arguments to the constructor call
PK bool
Unique bool
Revision bool // the integer column that counts the row's updates (model.FieldDB.Revision)
NotNull bool
AutoInc bool
Ref string
RefColumn string
OnDelete string
Item model.ItemSlot // list-item slot from Field.Item; ItemNone when absent
Options string // Definition var name from Field.Options (e.g. "ZoneModel"); "" when absent
IsPK bool
OldName string
GoType string
IsPointer bool // true if the original field is *T (only meaningful for FieldStruct)
OmitEmpty bool
Exclude bool
HasDB bool
// Permitted config — populated from validate:"..." tag
Letters bool
Tilde bool
Numbers bool
Spaces bool
Extra []rune
Minimum int
Maximum int
Tags []string // input modifiers e.g. "notilde", "min=2"
}
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is the code generator handler for the definitionc tool.
func New ¶
func New() *Generator
NewOrmc creates a new Definitionc handler with rootDir defaulting to ".".
func (*Generator) ExportSQL ¶
ExportSQL scans the directory for models and returns the full DDL. Requires an injected Exporter (e.g. from sqlt or postgres).
func (*Generator) GenerateForFile ¶
func (o *Generator) GenerateForFile(infos []StructInfo, sourceFile string) error
GenerateForFile writes ORM implementations for all infos into one file.
func (*Generator) GenerateForStruct ¶
GenerateForStruct reads the Go File and generates the ORM implementations for a given struct name.
func (*Generator) MainInputFileRelativePath ¶
MainInputFileRelativePath returns the relative path to the main input file, if any.
func (*Generator) Models ¶
Models scans root for definition.go and returns one model.Model per database struct (NoDB structs skipped), in the order the files and declarations were read — the same root always gives the same order. A model's foreign keys are in its SchemaExt(). It returns ErrNoModelsFound when there is none.
func (*Generator) NewFileEvent ¶
NewFileEvent implements the file-event contract for watchers (e.g. webtyp/app's devwatch).
func (*Generator) ScanModules ¶
ScanModules syncs the DB schema of every discovered module to the injected SchemaSyncer. Called once at startup by the tool (app), after SetSyncer.
- Writable module (main / replace): regenerate <file>_gen.go from definition.go, then sync each DB struct.
- Read-only module (cache): parse the committed definition_gen.go, then sync.
No-op if no syncer is injected (CLI codegen-only path).
func (*Generator) SetFinder ¶
func (g *Generator) SetFinder(f modfind.Discoverer)
SetFinder injects the shared modfind.Finder (one go list across ssr/image/definitionc).
func (*Generator) SetLog ¶
SetLog sets the log function for warnings and informational messages. If not set, messages are silently discarded.
func (*Generator) SetRootDir ¶
SetRootDir sets the root directory that Run() will scan. Defaults to ".". Useful in tests to point to a specific directory without needing os.Chdir.
func (*Generator) SetSkipTidy ¶
SetSkipTidy enables or disables the go mod tidy pass.
func (*Generator) SetSyncer ¶
func (g *Generator) SetSyncer(s SchemaSyncer)
SetSyncer sets the schema syncer for the generator.
func (*Generator) SupportedExtensions ¶
SupportedExtensions returns the list of file extensions this generator handles.
func (*Generator) UnobservedFiles ¶
UnobservedFiles returns a list of files that should be ignored by the watcher.
type ModuleInfo ¶ added in v0.1.40
type ModuleInfo struct {
Name string // Definition.Name, e.g. "patient_directory"
Package string // the Go package of definition.go
Entities []string // the struct name of each listed model (PatientModel → "Patient"), in the order written
}
ModuleInfo is `var Definition = model.ModuleDefinition{…}` as written in definition.go.
type RulesInfo ¶ added in v0.1.40
type RulesInfo struct {
Create, Read, Update, Delete uint8
}
RulesInfo is Definition.Rules as written in definition.go: for each action the value of model.RuleMode (0 = undeclared).
type SchemaSyncer ¶
SchemaSyncer applies a parsed table schema. Implemented by the consumer (webtyp/app) over *orm.DB; definitionc only ever sees this interface.
type StructInfo ¶
type StructInfo struct {
Name string
ModelName string
PackageName string
Fields []FieldInfo
ModelNameDeclared bool
NoDB bool
Wire bool // model.Definition{Wire: true}: a transport format, never a form
ListsOnlyAfterSearch bool // model.Definition{ListsOnlyAfterSearch: true}: definitionc generates ListsOnlyAfterSearch()
Allow uint8
Rules RulesInfo // Definition.Rules as written
MFA uint8 // Definition.MFA as written (a set of actions)
ExternalCalls []model.ExternalCallDefinition
SourceFile string
}