feat: 代码生成

This commit is contained in:
Blizzard
2026-04-28 10:29:02 +08:00
parent 7e282b36d7
commit b343856b58
38 changed files with 2199 additions and 51 deletions
+67
View File
@@ -0,0 +1,67 @@
package codegen
// GenConfig 代码生成完整配置
type GenConfig struct {
DbConfig DbConfig `json:"dbConfig"`
OutputDir string `json:"outputDir"` // 后端输出目录,绝对路径,为空则输出到项目根目录
FrontendOutputDir string `json:"frontendOutputDir"` // 前端输出目录,绝对路径,为空则跳过前端生成
Overwrite bool `json:"overwrite"` // true=覆盖已有文件 false=增量模式(跳过已存在文件)
Modules []Module `json:"modules"`
}
// DbConfig 目标数据库连接配置
type DbConfig struct {
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
DbName string `json:"dbName"`
DbType string `json:"dbType"` // mysql / postgres / sqlite
}
// Module 模块定义(对应一组功能)
type Module struct {
Name string `json:"name"` // 模块名(PascalCase),如 "Order"
PackageName string `json:"packageName"` // 包名(lowercase),如 "order"
Description string `json:"description"`
Features []Feature `json:"features"`
}
// Feature 功能单元(对应一张表 / 一个实体)
type Feature struct {
Name string `json:"name"` // 实体名(PascalCase),如 "Product"
TableName string `json:"tableName"` // 数据库表名,如 "product"
Comment string `json:"comment"`
Fields []Field `json:"fields"`
Relations []Relation `json:"relations"`
}
// Field 字段定义
type Field struct {
Name string `json:"name"` // Go 字段名(PascalCase),如 "OrderNo"
ColumnName string `json:"columnName"` // 数据库列名(snake_case),如 "order_no"
Type string `json:"type"` // Go 类型,如 string / int / float64 / bool / time.Time
GormTag string `json:"gormTag"` // gorm tag 附加内容,如 "size:100;not null"
JsonTag string `json:"jsonTag"` // json tag,如 "orderNo"
Comment string `json:"comment"` // 字段注释
Required bool `json:"required"` // 是否必填(影响 not null
}
// Relation 表关系定义
type Relation struct {
Type string `json:"type"` // OneToOne / OneToMany / ManyToMany
TargetFeature string `json:"targetFeature"` // 目标实体名(PascalCase
ForeignKey string `json:"foreignKey"` // 外键字段名,如 "UserId"
JoinTable string `json:"joinTable"` // ManyToMany 中间表名
FieldName string `json:"fieldName"` // Go 中关联字段名,如 "Tags"
}
// TestConnectionReq 测试连接请求
type TestConnectionReq struct {
DbConfig DbConfig `json:"dbConfig"`
}
// PreviewReq 预览代码请求
type PreviewReq struct {
GenConfig
}
+15
View File
@@ -0,0 +1,15 @@
package codegen
// PreviewFile 预览的单个文件
type PreviewFile struct {
FilePath string `json:"filePath"` // 相对于输出目录的路径
Content string `json:"content"` // 文件内容
AlwaysOverwrite bool `json:"alwaysOverwrite,omitempty"` // 聚合文件(如 enter.go),始终覆盖
}
// GenResult 代码生成结果
type GenResult struct {
OutputDir string `json:"outputDir"` // 实际输出目录
Files []PreviewFile `json:"files"` // 生成的文件列表
Message string `json:"message"` // 生成结果说明
}