gen

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(info *StructInfo) ([]byte, error)

Render generates the Go source code for a single StructInfo. The returned bytes are formatted by gofmt.

func RenderFile

func RenderFile(infos []*StructInfo, opt RenderOption) ([]byte, error)

RenderFile generates a single Go source file for all given StructInfos. The returned bytes are formatted by gofmt.

Types

type FieldInfo

type FieldInfo struct {
	Name       string // Go field name, e.g. "ID"
	Column     string // DB column name from `db:"id"` tag
	GoType     string // Go type as string, e.g. "int", "string", "time.Time"
	PrimaryKey bool   // true if tag contains "primaryKey"
	CreatedAt  bool   // true if this is a createdAt timestamp field
	UpdatedAt  bool   // true if this is an updatedAt timestamp field
}

FieldInfo holds parsed metadata for one struct field.

type RelationInfo

type RelationInfo struct {
	FieldName        string // Go field name, e.g. "Posts" or "User"
	TargetType       string // Target struct name, e.g. "Post" or "User"
	TargetPkgAlias   string // Source file import alias (e.g. "amodel"). Empty for same-package types.
	TargetImportPath string // Full import path (e.g. "github.com/.../auth/model"). Empty for same-package types.
	RelType          string // "has_many", "belongs_to", "has_one", or "many_to_many"
	ForeignKey       string // FK column name, e.g. "user_id"
	IsSlice          bool   // true for has_many / many_to_many ([]Post)
	IsPointer        bool   // true for belongs_to / has_one (*User)
	JoinTable        string // many_to_many only: join table name, e.g. "user_tags"
	References       string // many_to_many only: target FK in join table, e.g. "tag_id"
}

RelationInfo holds parsed metadata for a relation field.

type RenderOption

type RenderOption struct {
	DestPkg      string        // output package name (empty = same as source)
	SourceImport string        // import path for source package (required when DestPkg is set)
	PeerInfos    []*StructInfo // other structs in the same package (for join scan field lookups)
}

RenderOption controls the output of RenderFile.

type StructInfo

type StructInfo struct {
	Name      string         // Go struct name, e.g. "User"
	Package   string         // Package name, e.g. "model"
	Fields    []FieldInfo    // Non-skipped db fields
	Relations []RelationInfo // Parsed rel tags
	TableName string         // Set by the caller (from CLI flag)
}

StructInfo holds parsed metadata for the target struct.

func Parse

func Parse(filePath string) ([]*StructInfo, error)

Parse reads the Go file at path and returns StructInfo for every struct that has at least one field with a db tag.

func (*StructInfo) PrimaryKeyField

func (s *StructInfo) PrimaryKeyField() (*FieldInfo, error)

PrimaryKeyField returns the primary key field, or an error if none or multiple are defined.

Jump to

Keyboard shortcuts

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