Documentation
¶
Overview ¶
Package base 为 toolkit 代码生成工具提供共享的基础能力。
该包负责初始化运行环境(配置文件、数据库连接),读取 MySQL 与 PostgreSQL 的表、字段、索引、主键等元数据,并提供命名转换、类型映射以及 Git 仓库、 Go 模块路径等辅助函数,供 module 等模板生成包复用。
Index ¶
- Variables
- func Add(numberOne, numberTwo any) int
- func ApiToProto(Condition []Column, res, request string, page bool) string
- func CamelStr(name string) string
- func Char(in string) string
- func CommonTemplateFuncs() template.FuncMap
- func Convert(Condition []Column) string
- func GoInstall(path ...string) error
- func Helper(name string) string
- func InMethod(arr []Method, target ...string) bool
- func InitBase() error
- func InitConfig() error
- func InitPath() error
- func InitQuery() error
- func Json(Condition []Column) string
- func ModuleDaoConvertProto(Condition []Column, res, resItem string) string
- func ModulePath(filename string) (string, error)
- func ModuleProtoConvertDao(Condition []Column, res, request string) string
- func ModuleProtoConvertMap(Condition []Column, request string) string
- func ModuleVersion(path string) (string, error)
- func NewDataType() map[string]DataType
- func ParseVCSUrl(repo string) (*url.URL, error)
- func PathChar(str ...string) string
- func Pointer(in string) string
- func Props(table, condition []Column) string
- func PropsSearch(table, condition []Column) string
- func ProtoRequest(condition []Column) string
- func RatelMod() string
- func Rule(table []Column) string
- func SetUrl(url string)
- func SymbolChar() string
- func Tree(path string, dir string)
- func TypeScript(Condition []Column) string
- func TypeScriptCondition(Condition []Column) string
- type Column
- type DaoParam
- type DataType
- type Index
- type Method
- type ModuleParam
- type Repo
- func (r *Repo) Clone(ctx context.Context) error
- func (r *Repo) CopyFrontTo(ctx context.Context, to string, modPath string, ignores []string) error
- func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []string) error
- func (r *Repo) CopyToV2(ctx context.Context, to string, modPath string, ignores, replaces []string) error
- func (r *Repo) Path() string
- func (r *Repo) Pull(ctx context.Context) error
- type Table
Constants ¶
This section is empty.
Variables ¶
var ( // Config 为加载完成的 toolkit.toml 配置实例。 Config *config.Config // Query 为已建立连接的数据库查询句柄。 Query *gorm.DB // Path 为当前项目的工作目录。 Path *string // Url 为通过 SetUrl 设置的模板或仓库地址。 Url *string // Driver 为当前数据库驱动名,默认 mysql。 Driver *string // DefaultDefault 为未在配置中指定驱动时使用的默认驱动名 mysql。 DefaultDefault = "mysql" )
以下包级变量为代码生成流程共享的运行时状态,由 InitBase 系列函数初始化,非并发安全。
var SqlResultConvert = map[string]string{
"int8": "bigint",
"int4": "int",
}
SqlResultConvert 记录需要归一化的数据库类型映射,键为原始类型,值为归一化后的类型。
Functions ¶
func ApiToProto ¶ added in v3.2.31
ApiToProto 生成从请求参数到 Proto 对象的赋值代码片段,page 为 true 时同时写入分页对象。
res 为目标 Proto 对象变量名,request 为请求参数取值函数名,供代码生成模板调用。
func CommonTemplateFuncs ¶ added in v3.8.56
CommonTemplateFuncs 返回公共模板函数映射的浅拷贝;调用方可以在返回的映射上追加本生成器 专属函数而不会污染包级公共映射。
func InitBase ¶ added in v3.2.16
func InitBase() error
InitBase 按顺序初始化工作目录、配置文件与数据库连接,任一步骤失败时直接返回该步骤的错误。
func InitConfig ¶ added in v3.2.16
func InitConfig() error
InitConfig 读取工作目录下的 toolkit.toml 并加载到包级变量 Config;配置文件缺失或解析失败时返回错误。
func InitPath ¶ added in v3.2.16
func InitPath() error
InitPath 初始化当前工作目录 Path,并将数据库驱动重置为默认驱动;获取当前目录失败时返回错误。
func InitQuery ¶ added in v3.2.16
func InitQuery() error
InitQuery 依据 Config 中的 db.* 配置项创建数据库客户端并保存到 Query;创建失败时返回错误。
指标采集、链路追踪与调试日志在此流程中固定关闭。
func ModuleDaoConvertProto ¶ added in v3.2.31
ModuleDaoConvertProto 生成 DAO 对象到 Proto 对象的赋值代码片段,可空字段按 null 包类型取值。
res 为目标 Proto 对象变量名,resItem 为源 DAO 对象变量名,供代码生成模板调用。
func ModulePath ¶
ModulePath 读取指定 go.mod 文件并返回其 module 声明路径;文件读取失败时返回错误。
func ModuleProtoConvertDao ¶ added in v3.2.31
ModuleProtoConvertDao 生成 Proto 对象到 DAO 对象的赋值代码片段,可空字段按 null 包类型还原。
res 为目标 DAO 对象变量名,request 为源 Proto 对象变量名,供代码生成模板调用。
func ModuleProtoConvertMap ¶ added in v3.2.31
ModuleProtoConvertMap 生成 Proto 请求到 condition 映射表的赋值代码片段,供代码生成模板调用。
func ModuleVersion ¶
ModuleVersion 通过 go mod graph 在模块依赖图中查找指定模块的路径与版本;未找到或命令失败时返回错误。
func NewDataType ¶ added in v3.2.16
NewDataType 返回数据库类型到各目标语言类型映射的完整对照表,调用方不应修改返回结果。
func ParseVCSUrl ¶
ParseVCSUrl 将给定的仓库地址解析为标准的 url.URL,支持 SCP 风格与常见 Git 协议地址。
协议不在支持列表内时返回错误;解析过程不校验地址是否可访问。
参考:Go 源码实现 https://github.com/golang/go/blob/master/src/cmd/go/internal/vcs/vcs.go Git 地址规范 https://git-scm.com/docs/git-clone#_git_urls
func PropsSearch ¶ added in v3.8.46
PropsSearch 依据表字段与查询条件生成前端搜索表单字段片段,供代码生成模板调用。
func ProtoRequest ¶ added in v3.6.0
ProtoRequest 依据条件字段生成 Proto 请求消息的字段声明片段,供代码生成模板调用。
func RatelMod ¶
func RatelMod() string
RatelMod 返回当前环境中 ratel 框架源码或模块缓存的本地目录路径;无法获取版本时回退到 GOPATH 源码目录。
func TypeScript ¶ added in v3.7.21
TypeScript 依据字段可空性与类型生成 TypeScript 接口属性声明片段,供代码生成模板调用。
func TypeScriptCondition ¶ added in v3.7.21
TypeScriptCondition 依据条件字段生成 TypeScript 查询条件对象的可选属性片段,供代码生成模板调用。
Types ¶
type Column ¶ added in v3.2.16
type Column struct {
// ColumnName 为字段名。
ColumnName string `gorm:"column:COLUMN_NAME"`
// IsNullable 表示字段是否允许为 NULL,取值 YES 或 NO。
IsNullable string `gorm:"column:IS_NULLABLE"`
// DataType 为归一化后的数据库字段类型。
DataType string `gorm:"column:DATA_TYPE"`
// ColumnKey 为索引键类型,主键为 PRI,唯一索引为 UNIQUE。
ColumnKey string `gorm:"column:COLUMN_KEY"`
// ColumnComment 为字段注释。
ColumnComment string `gorm:"column:COLUMN_COMMENT"`
// PosiTion 为字段在生成代码中的位置序号。
PosiTion int64 `gorm:"-"`
// DataTypeMap 为该字段对应的多语言类型映射。
DataTypeMap DataType `gorm:"-"`
// AlisaColumnName 为字段别名,主键字段固定为 id。
AlisaColumnName string `gorm:"-"`
// CamelName 为 ColumnName 的大驼峰形式,元数据加载阶段预计算一次,供各渲染 helper 复用(PERF-8-4 [P1])。
CamelName string `gorm:"-"`
// LowerName 为 ColumnName 的小驼峰形式,元数据加载阶段预计算一次,供各渲染 helper 复用(PERF-8-4 [P1])。
LowerName string `gorm:"-"`
}
Column 描述表中一个字段的元信息及其在各目标语言下的类型映射。
func TableColumn ¶ added in v3.2.16
TableColumn 查询指定表的全部字段信息,并补全类型映射与字段位置序号;SQL 执行失败时返回错误。
type DataType ¶ added in v3.2.16
type DataType struct {
// Default 为非空场景使用的 Go 类型。
Default string
// Empty 为允许为空场景使用的 null 包类型。
Empty string
// Proto 为 gRPC Proto 类型名。
Proto string
// OptionProto 表示是否以 optional 形式生成 Proto 字段。
OptionProto bool
// TypeScript 为 TypeScript 类型名。
TypeScript string
// Json 为 JSON 序列化时使用的默认值。
Json string
}
DataType 描述单个数据库类型在 Go、Proto、TypeScript、JSON 各场景下的类型表示。
type Index ¶ added in v3.2.16
type Index struct {
// IndexName 为索引名称。
IndexName string `gorm:"column:INDEX_NAME"`
// Field 为索引覆盖的字段,多个字段以逗号分隔。
Field string `gorm:"column:FIELD"`
}
Index 描述表上一个索引的元信息。
type Method ¶ added in v3.2.16
type Method struct {
// Type 为方法类型,取值 Create、Update、Delete、Show、Item、List、Recover、Drop。
Type string
// Name 为生成的函数名。
Name string
// Alias 为函数别名。
Alias string
// Condition 为该方法查询条件的字段列表。
Condition []Column
// ConditionTotal 为查询条件字段数量。
ConditionTotal int
// Table 为绑定的表信息。
Table Table
// TableColumn 为表结构信息。
TableColumn []Column
// Pkg 为包名。
Pkg string
// PkgPath 为包在模块内的相对路径。
PkgPath string
// Primary 为主键字段信息。
Primary Column
// ModName 为 go.mod 的模块路径。
ModName string
// Page 表示是否分页。
Page bool
// SoftDelete 表示是否软删除。
SoftDelete bool
}
Method 描述待生成的一个数据访问方法及其绑定的表上下文。
type ModuleParam ¶ added in v3.2.16
type ModuleParam struct {
// Table 为表信息。
Table Table
// TableColumn 为表字段列表。
TableColumn []Column
// Method 为待生成的方法列表。
Method []Method
// Pkg 为生成代码的包名(路径末级)。
Pkg string
// PkgPath 为包在模块内的相对路径。
PkgPath string
// Primary 为主键字段信息。
Primary Column
// ModName 为当前 go.mod 的模块路径。
ModName string
// Page 表示方法是否需要分页参数。
Page bool
// SoftDelete 表示是否使用软删除。
SoftDelete bool
// Condition 为构造查询条件所需的字段列表。
Condition []Column
}
ModuleParam 承载生成一个模块所需的表、方法与路径上下文,由命令行交互流程填充。
type Repo ¶
type Repo struct {
// contains filtered or unexported fields
}
Repo 封装一个 Git 仓库的本地缓存路径、远程地址与分支,供模板拉取与拷贝使用。
Repo 由 NewRepo 创建,内部字段不对外暴露;方法不保证并发安全,同一实例不应被并发调用。
func (*Repo) Clone ¶
Clone 将仓库克隆到本地缓存路径;路径已存在时改为调用 Pull 拉取更新。
指定分支时通过 git clone -b 检出该分支,克隆失败时返回对应错误。
func (*Repo) CopyFrontTo ¶ added in v3.5.3
CopyFrontTo 先确保仓库已克隆到本地,再将前端模板仓库拷贝到项目目录,不做模块路径替换。