Documentation
¶
Overview ¶
Package options 提供函数选项模式(Functional Options Pattern)的代码生成核心逻辑。 通过解析 Go 源文件中的结构体定义,自动生成对应的 Option 类型、构造函数和 With 函数。
Index ¶
- func BigCamelToSmallCamel(bigCamel string) string
- func CamelToSnake(camelCase string) string
- func CapitalizeFirstLetter(input string) string
- func GetFirstLetter(input string) string
- func SafeName(name string) string
- type FieldInfo
- type Generator
- func (g *Generator) GenerateCodeByTemplate() error
- func (g *Generator) GeneratingOptions() error
- func (g *Generator) OutPath() string
- func (g *Generator) OutputToFile() error
- func (g *Generator) SetMode(mode string)
- func (g *Generator) SetOutPath(outPath string)
- func (g *Generator) SetStyle(style Style)
- func (g *Generator) SetWithPrefix(withPrefix string)
- type StructInfo
- type Style
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BigCamelToSmallCamel ¶
BigCamelToSmallCamel 将大驼峰(PascalCase)转换为小驼峰(camelCase)。 示例:UserAgent → userAgent,HTTPServer → hTTPServer。 使用 rune 安全处理,支持多字节 UTF-8 字符。
func CamelToSnake ¶
CamelToSnake 将驼峰命名(PascalCase/camelCase)转换为蛇形命名(snake_case)。 正确处理连续大写字母:HTTPServer → http_server,UserID → user_id。
func CapitalizeFirstLetter ¶
CapitalizeFirstLetter 将字符串首字母转为大写。 示例:user → User,config → Config。
func GetFirstLetter ¶
GetFirstLetter 获取字符串的小写首字母。 示例:User → u,Config → c。 用于生成接收者变量名。
Types ¶
type FieldInfo ¶
type FieldInfo struct {
Name string // 字段名称,如 "Username"
Type string // 字段类型字符串,如 "string"、"*int"、"map[string]int"
}
FieldInfo 表示结构体字段的名称和类型信息。
type Generator ¶
type Generator struct {
// StructInfo 包含目标结构体的元数据信息。
StructInfo *StructInfo
// Found 标记目标结构体是否在当前目录中找到。
Found bool
// contains filtered or unexported fields
}
Generator 是代码生成器的核心结构体。 负责解析源文件、提取结构体信息、渲染模板并输出生成的代码文件。
func NewGenerator ¶
func NewGenerator() *Generator
NewGenerator 创建一个新的代码生成器实例。 默认使用 interface 风格生成代码。
func (*Generator) GenerateCodeByTemplate ¶
GenerateCodeByTemplate 使用 Go 模板引擎渲染生成代码。 根据当前的 style(interface/closure)和 mode(write/append)选择对应的模板, 将 StructInfo 中的元数据填充到模板中,生成最终的 Go 源代码。
func (*Generator) GeneratingOptions ¶
GeneratingOptions 解析当前目录下的所有 Go 源文件,查找目标结构体。 找到后提取结构体的字段信息、泛型参数等元数据,存储到 g.StructInfo 中。 如果成功找到目标结构体,g.Found 会被设置为 true。
func (*Generator) OutputToFile ¶
OutputToFile 将生成的代码写入输出文件。
- write 模式:格式化代码后覆盖写入(自动创建目录)。
- append 模式:读取现有文件,追加生成的代码,再整体格式化。
使用 golang.org/x/tools/imports 自动管理导入和格式化。
func (*Generator) SetOutPath ¶
SetOutPath 设置输出文件路径。 如果 outPath 为空,使用默认命名约定:opt_<snake_case_struct_name>_gen.go。 例如:结构体 UserConfig → opt_user_config_gen.go。
func (*Generator) SetStyle ¶
SetStyle 设置代码生成风格。
- StyleInterface("interface"):基于接口的选项模式(默认)。
- StyleClosure("closure"):基于闭包的选项模式。
func (*Generator) SetWithPrefix ¶
SetWithPrefix 设置 With 函数的自定义前缀。 例如设置为 "User",字段 Name 将生成 WithUserName 而非 WithName。
type StructInfo ¶
type StructInfo struct {
PackageName string // 包名,如 "example"
StructName string // 结构体名称(大驼峰),如 "UserConfig"
NewStructName string // 结构体名称(小驼峰),如 "userConfig",用作构造函数中的变量名
Fields []FieldInfo // 必填字段列表(标记了 opt:"-" 的字段),作为构造函数的必传参数
OptionalFields []FieldInfo // 可选字段列表(未标记的字段),会生成对应的 With 函数
GenericParams []FieldInfo // 泛型类型参数列表,如 [{Name:"T", Type:"any"}, {Name:"U", Type:"comparable"}]
WithPrefix string // With 函数的自定义前缀,如设置为 "User" 则生成 WithUserName 而非 WithName
}
StructInfo 包含目标结构体的全部元数据,用于模板渲染。