options

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package options 提供函数选项模式(Functional Options Pattern)的代码生成核心逻辑。 通过解析 Go 源文件中的结构体定义,自动生成对应的 Option 类型、构造函数和 With 函数。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigCamelToSmallCamel

func BigCamelToSmallCamel(bigCamel string) string

BigCamelToSmallCamel 将大驼峰(PascalCase)转换为小驼峰(camelCase)。 示例:UserAgent → userAgent,HTTPServer → hTTPServer。 使用 rune 安全处理,支持多字节 UTF-8 字符。

func CamelToSnake

func CamelToSnake(camelCase string) string

CamelToSnake 将驼峰命名(PascalCase/camelCase)转换为蛇形命名(snake_case)。 正确处理连续大写字母:HTTPServer → http_server,UserID → user_id。

func CapitalizeFirstLetter

func CapitalizeFirstLetter(input string) string

CapitalizeFirstLetter 将字符串首字母转为大写。 示例:user → User,config → Config。

func GetFirstLetter

func GetFirstLetter(input string) string

GetFirstLetter 获取字符串的小写首字母。 示例:User → u,Config → c。 用于生成接收者变量名。

func SafeName

func SafeName(name string) string

SafeName 确保标识符不与 Go 关键字冲突。 如果 name 是 Go 保留关键字(如 func、map、type 等),自动追加下划线后缀。 示例:func → func_,map → map_,port → port(不变)。

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

func (g *Generator) GenerateCodeByTemplate() error

GenerateCodeByTemplate 使用 Go 模板引擎渲染生成代码。 根据当前的 style(interface/closure)和 mode(write/append)选择对应的模板, 将 StructInfo 中的元数据填充到模板中,生成最终的 Go 源代码。

func (*Generator) GeneratingOptions

func (g *Generator) GeneratingOptions() error

GeneratingOptions 解析当前目录下的所有 Go 源文件,查找目标结构体。 找到后提取结构体的字段信息、泛型参数等元数据,存储到 g.StructInfo 中。 如果成功找到目标结构体,g.Found 会被设置为 true。

func (*Generator) OutPath

func (g *Generator) OutPath() string

OutPath 返回当前配置的输出文件路径。

func (*Generator) OutputToFile

func (g *Generator) OutputToFile() error

OutputToFile 将生成的代码写入输出文件。

  • write 模式:格式化代码后覆盖写入(自动创建目录)。
  • append 模式:读取现有文件,追加生成的代码,再整体格式化。

使用 golang.org/x/tools/imports 自动管理导入和格式化。

func (*Generator) SetMode

func (g *Generator) SetMode(mode string)

SetMode 设置文件写入模式。

  • "write":覆盖或创建新文件(默认)。
  • "append":追加到已有文件末尾。

func (*Generator) SetOutPath

func (g *Generator) SetOutPath(outPath string)

SetOutPath 设置输出文件路径。 如果 outPath 为空,使用默认命名约定:opt_<snake_case_struct_name>_gen.go。 例如:结构体 UserConfig → opt_user_config_gen.go。

func (*Generator) SetStyle

func (g *Generator) SetStyle(style Style)

SetStyle 设置代码生成风格。

  • StyleInterface("interface"):基于接口的选项模式(默认)。
  • StyleClosure("closure"):基于闭包的选项模式。

func (*Generator) SetWithPrefix

func (g *Generator) SetWithPrefix(withPrefix string)

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 包含目标结构体的全部元数据,用于模板渲染。

type Style

type Style string

Style 表示代码生成风格。

const (
	// StyleInterface 生成基于接口的选项模式(默认)。
	// 每个可选字段生成一个实现了 apply 方法的 unexported 结构体。
	StyleInterface Style = "interface"

	// StyleClosure 生成基于闭包的选项模式(兼容 go-optioner)。
	// Option 类型为 func(*Struct),每个 With 函数返回一个闭包。
	StyleClosure Style = "closure"
)

Jump to

Keyboard shortcuts

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