generator

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const Version = "v1.0.0"

Variables

This section is empty.

Functions

func GenerateFile

func GenerateFile(meta ModelMeta, outDir string) error

func GenerateRelationsFile added in v0.2.0

func GenerateRelationsFile(models []ModelMeta, outDir string) error

GenerateRelationsFile generates relations_gen.go with all relation definitions

func ResolveRelationFields added in v0.3.0

func ResolveRelationFields(models []ModelMeta)

ResolveRelationFields resolves ForeignKeyField across models for hasOne/hasMany relations. For belongsTo, ForeignKeyField is on the parent model (resolved during parsing). For hasOne/hasMany, ForeignKeyField is on the target model and needs cross-model lookup.

Types

type FieldMeta

type FieldMeta struct {
	FieldName    string
	Column       string
	Type         string
	IsPK         bool
	AutoIncr     bool
	IsJSON       bool     // Whether field is a JSON type
	JSONTypeName string   // Name of the JSON struct type (e.g. "UserMetadata")
	Doc          []string // Documentation comments
}

type GenConfig

type GenConfig struct {
	OutPath        string
	IncludeStructs []string
	ExcludeStructs []string
	FieldTypeMap   map[string]string
}

GenConfig holds parsed configuration from config.go

func ParseConfig

func ParseConfig(dir string) (*GenConfig, error)

ParseConfig parses config.go in the given directory for gen.Config

type JSONFieldMeta

type JSONFieldMeta struct {
	FieldName  string         // Name of the field in parent model (e.g. "Metadata")
	TypeName   string         // Name of the JSON struct type (e.g. "UserMetadata")
	ColumnName string         // Database column name
	Paths      []JSONPathMeta // List of paths in this JSON field
}

JSONFieldMeta holds information about a JSON field's path structure

type JSONPathMeta

type JSONPathMeta struct {
	GoName   string // Go field name (e.g. "Name")
	JSONPath string // JSON path (e.g. "$.name")
}

JSONPathMeta holds information about a single JSON path

type ModelMeta

type ModelMeta struct {
	PackageName         string
	ParentPackage       string // For generated code to reference parent package
	ModulePath          string // Module path like github.com/user/project
	PackagePath         string // Package path like models
	ModelName           string
	TableName           string
	Fields              []FieldMeta
	JSONFields          []JSONFieldMeta   // JSON field path definitions
	Relations           []RelationMeta    // Relation definitions
	Doc                 []string          // Documentation comments
	CliVersion          string            // SQLCLI Version
	HasJSON             bool              // Whether imported encoding/json package is needed
	HasJSONField        bool              // Whether any field has type:json tag
	PKFieldName         string            // Cached PK Field Name
	PKColumnName        string            // Cached PK Column Name
	PKFieldType         string            // Cached PK Field Type
	IsAutoIncrementPK   bool              // Cached PK AutoIncrement status
	SchemaStructName    string            // e.g. userSchema
	IsJSONOnly          bool              // True if struct is only used as JSON embed (no db tags/PK)
	HasDBTag            bool              // True if any field has a db tag
	SoftDeleteField     string            // Name of the soft delete field (e.g. "DeletedAt")
	SoftDeleteColumn    string            // Name of the soft delete column (e.g. "deleted_at")
	SoftDeleteFieldType string            // Type of the soft delete field (e.g. "*time.Time")
	TypeAliases         map[string]string // type A int → {"A": "int"}
	FieldTypeMap        map[string]string // User-defined type mappings from config
}

func ParseModels

func ParseModels(dir string) ([]ModelMeta, error)

func (ModelMeta) GetFieldType

func (m ModelMeta) GetFieldType(goType string) string

GetFieldType returns the appropriate field type based on Go type

func (ModelMeta) GoIsNonZero

func (m ModelMeta) GoIsNonZero(fieldName, goType string) string

GoIsNonZero returns the Go expression to check if a field is NOT zero value

func (ModelMeta) IsNumeric

func (m ModelMeta) IsNumeric(goType string) bool

IsNumeric checks if the Go type is numeric

func (ModelMeta) QualifyPKType

func (m ModelMeta) QualifyPKType() string

QualifyPKType returns the qualified type name for the PK field

type RelationMeta added in v0.2.0

type RelationMeta struct {
	FieldName       string // Field name in parent model (e.g., "Posts")
	RelType         string // Relation type: "hasOne", "hasMany", "belongsTo"
	ForeignKey      string // Foreign key column (on child for hasOne/Many, on parent for belongsTo)
	LocalKey        string // Local key column (on parent for hasOne/Many[default id], on child for belongsTo[default id])
	TargetType      string // Target model type name (e.g., "Post")
	TargetSlice     bool   // True if field is a slice (hasMany)
	ForeignKeyField string // Go field name of foreign key (on parent for belongsTo, on target for hasOne/hasMany)
	TargetPKField   string // Go field name of PK on target model (used for belongsTo getForeignKey)
}

RelationMeta holds information about a model relation

type RelationsData added in v0.2.0

type RelationsData struct {
	PackageName   string
	ParentPackage string
	ModulePath    string
	PackagePath   string
	CliVersion    string
	ModelName     string
	PKFieldName   string
	Relations     []RelationMeta
}

RelationsData holds data for generating relations file

Jump to

Keyboard shortcuts

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