Help us shape the future of Go! Take the 2026 Go Developer Survey

generator

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APISpec

type APISpec struct {
	Name        string                 `json:"name"`
	Version     string                 `json:"version"`
	Description string                 `json:"description"`
	Endpoints   []Endpoint             `json:"endpoints"`
	Models      []Model                `json:"models"`
	Middleware  []Middleware           `json:"middleware"`
	Services    []Service              `json:"services"`
	Metadata    map[string]interface{} `json:"metadata"`
}

APISpec API规范

type BaseGenerator

type BaseGenerator struct {
	// contains filtered or unexported fields
}

BaseGenerator 基础生成器

func NewBaseGenerator

func NewBaseGenerator(name string, supportedFormats []string) *BaseGenerator

NewBaseGenerator 创建基础生成器

func (*BaseGenerator) GetName

func (g *BaseGenerator) GetName() string

GetName 获取生成器名称

func (*BaseGenerator) GetSupportedFormats

func (g *BaseGenerator) GetSupportedFormats() []string

GetSupportedFormats 获取支持的格式

func (*BaseGenerator) SetFileGenerator

func (g *BaseGenerator) SetFileGenerator(fg FileGenerator)

SetFileGenerator 设置文件生成器

func (*BaseGenerator) SetFormatter

func (g *BaseGenerator) SetFormatter(formatter CodeFormatter)

SetFormatter 设置格式化器

func (*BaseGenerator) SetTemplateEngine

func (g *BaseGenerator) SetTemplateEngine(engine TemplateEngine)

SetTemplateEngine 设置模板引擎

type CodeFormatter

type CodeFormatter interface {
	// Format 格式化代码
	Format(code string, language string) (string, error)

	// FormatFile 格式化文件
	FormatFile(filePath string, language string) error
}

CodeFormatter 代码格式化器接口

type CodeGenerationManager

type CodeGenerationManager struct {
	// contains filtered or unexported fields
}

CodeGenerationManager 代码生成管理器

func NewCodeGenerationManager

func NewCodeGenerationManager() *CodeGenerationManager

NewCodeGenerationManager 创建代码生成管理器

func (*CodeGenerationManager) CreateProjectStructure

func (m *CodeGenerationManager) CreateProjectStructure(options *GenerateOptions) error

CreateProjectStructure 创建项目结构

func (*CodeGenerationManager) Generate

func (m *CodeGenerationManager) Generate(ctx context.Context, spec *APISpec, options *GenerateOptions) error

Generate 生成代码

func (*CodeGenerationManager) GenerateConfig

func (m *CodeGenerationManager) GenerateConfig(spec *APISpec, options *GenerateOptions) error

GenerateConfig 生成配置文件

func (*CodeGenerationManager) GenerateFromOpenAPI

func (m *CodeGenerationManager) GenerateFromOpenAPI(ctx context.Context, openAPIFile string, options *GenerateOptions) error

GenerateFromOpenAPI 从OpenAPI规范生成代码

func (*CodeGenerationManager) GenerateFromProtobuf

func (m *CodeGenerationManager) GenerateFromProtobuf(ctx context.Context, protoFiles []string, options *GenerateOptions) error

GenerateFromProtobuf 从protobuf文件生成代码

func (*CodeGenerationManager) GetGenerator

func (m *CodeGenerationManager) GetGenerator(name string) (CodeGenerator, bool)

GetGenerator 获取指定生成器

func (*CodeGenerationManager) ListGenerators

func (m *CodeGenerationManager) ListGenerators() []CodeGenerator

ListGenerators 列出所有生成器

func (*CodeGenerationManager) RegisterGenerator

func (m *CodeGenerationManager) RegisterGenerator(generator CodeGenerator)

RegisterGenerator 注册生成器

func (*CodeGenerationManager) SetVerbose

func (m *CodeGenerationManager) SetVerbose(verbose bool)

SetVerbose 设置详细模式

type CodeGenerator

type CodeGenerator interface {
	// GenerateAPI 生成API相关代码
	GenerateAPI(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GenerateClient 生成客户端代码
	GenerateClient(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GenerateTests 生成测试代码
	GenerateTests(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GenerateDocs 生成文档
	GenerateDocs(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GenerateService 生成服务实现代码
	GenerateService(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GenerateMiddleware 生成中间件代码
	GenerateMiddleware(ctx context.Context, spec *APISpec, options *GenerateOptions) error

	// GetName 获取生成器名称
	GetName() string

	// GetSupportedFormats 获取支持的格式
	GetSupportedFormats() []string
}

CodeGenerator 代码生成器接口

type Endpoint

type Endpoint struct {
	Name        string            `json:"name"`
	Path        string            `json:"path"`
	Method      string            `json:"method"`
	Handler     string            `json:"handler"`
	Request     *Model            `json:"request,omitempty"`
	Response    *Model            `json:"response,omitempty"`
	Middleware  []string          `json:"middleware,omitempty"`
	Tags        []string          `json:"tags,omitempty"`
	Summary     string            `json:"summary,omitempty"`
	Description string            `json:"description,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

Endpoint API端点

type Field

type Field struct {
	Name        string            `json:"name"`
	Type        string            `json:"type"`
	Required    bool              `json:"required"`
	Default     interface{}       `json:"default,omitempty"`
	Description string            `json:"description,omitempty"`
	Tags        []string          `json:"tags,omitempty"`
	Validation  *Validation       `json:"validation,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

Field 模型字段

type FileGenerator

type FileGenerator interface {
	// GenerateFile 生成单个文件
	GenerateFile(ctx context.Context, spec *APISpec, options *GenerateOptions, filePath string) error

	// GenerateDirectory 生成目录结构
	GenerateDirectory(ctx context.Context, spec *APISpec, options *GenerateOptions, dirPath string) error

	// GetTemplatePath 获取模板路径
	GetTemplatePath(templateName string) string
}

FileGenerator 文件生成器接口

type GenerateOptions

type GenerateOptions struct {
	OutputDir     string            `json:"output_dir"`
	TemplateDir   string            `json:"template_dir,omitempty"`
	Language      string            `json:"language"`  // go, typescript, python, etc.
	Framework     string            `json:"framework"` // gin, chi, echo, etc.
	PackageName   string            `json:"package_name"`
	ModuleName    string            `json:"module_name"`
	GenerateTypes []string          `json:"generate_types"` // api, client, tests, docs, service, middleware
	Features      []string          `json:"features"`       // openapi, swagger, validation, etc.
	Config        map[string]string `json:"config,omitempty"`
	Verbose       bool              `json:"verbose"`
	DryRun        bool              `json:"dry_run"`
}

GenerateOptions 生成选项

type GeneratorRegistry

type GeneratorRegistry struct {
	// contains filtered or unexported fields
}

GeneratorRegistry 生成器注册表

func NewGeneratorRegistry

func NewGeneratorRegistry() *GeneratorRegistry

NewGeneratorRegistry 创建新的生成器注册表

func (*GeneratorRegistry) GenerateAll

func (r *GeneratorRegistry) GenerateAll(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateAll 使用所有支持的生成器生成代码

func (*GeneratorRegistry) Get

func (r *GeneratorRegistry) Get(name string) (CodeGenerator, bool)

Get 获取生成器

func (*GeneratorRegistry) List

func (r *GeneratorRegistry) List() []CodeGenerator

List 列出所有生成器

func (*GeneratorRegistry) Register

func (r *GeneratorRegistry) Register(generator CodeGenerator)

Register 注册生成器

type GoAPIGenerator

type GoAPIGenerator struct {
	*BaseGenerator
}

GoAPIGenerator Go API生成器

func NewGoAPIGenerator

func NewGoAPIGenerator() *GoAPIGenerator

NewGoAPIGenerator 创建Go API生成器

func (*GoAPIGenerator) GenerateAPI

func (g *GoAPIGenerator) GenerateAPI(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateAPI 生成API代码

func (*GoAPIGenerator) GenerateClient

func (g *GoAPIGenerator) GenerateClient(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateClient 生成客户端代码

func (*GoAPIGenerator) GenerateDocs

func (g *GoAPIGenerator) GenerateDocs(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateDocs 生成文档

func (*GoAPIGenerator) GenerateMiddleware

func (g *GoAPIGenerator) GenerateMiddleware(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateMiddleware 生成中间件代码

func (*GoAPIGenerator) GenerateService

func (g *GoAPIGenerator) GenerateService(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateService 生成服务代码

func (*GoAPIGenerator) GenerateTests

func (g *GoAPIGenerator) GenerateTests(ctx context.Context, spec *APISpec, options *GenerateOptions) error

GenerateTests 生成测试代码

type GoCodeFormatter

type GoCodeFormatter struct{}

GoCodeFormatter Go代码格式化器

func NewGoCodeFormatter

func NewGoCodeFormatter() *GoCodeFormatter

NewGoCodeFormatter 创建Go代码格式化器

func (*GoCodeFormatter) Format

func (f *GoCodeFormatter) Format(code string, language string) (string, error)

Format 格式化代码

func (*GoCodeFormatter) FormatFile

func (f *GoCodeFormatter) FormatFile(filePath string, language string) error

FormatFile 格式化文件

type GoFileGenerator

type GoFileGenerator struct {
	// contains filtered or unexported fields
}

GoFileGenerator Go文件生成器

func NewGoFileGenerator

func NewGoFileGenerator(engine TemplateEngine) *GoFileGenerator

NewGoFileGenerator 创建Go文件生成器

func (*GoFileGenerator) GenerateDirectory

func (g *GoFileGenerator) GenerateDirectory(ctx context.Context, spec *APISpec, options *GenerateOptions, dirPath string) error

GenerateDirectory 生成目录结构

func (*GoFileGenerator) GenerateFile

func (g *GoFileGenerator) GenerateFile(ctx context.Context, spec *APISpec, options *GenerateOptions, filePath string) error

GenerateFile 生成单个文件

func (*GoFileGenerator) GetTemplatePath

func (g *GoFileGenerator) GetTemplatePath(templateName string) string

GetTemplatePath 获取模板路径

type GoTemplateEngine

type GoTemplateEngine struct {
	// contains filtered or unexported fields
}

GoTemplateEngine Go模板引擎

func NewGoTemplateEngine

func NewGoTemplateEngine() *GoTemplateEngine

NewGoTemplateEngine 创建Go模板引擎

func (*GoTemplateEngine) LoadTemplates

func (e *GoTemplateEngine) LoadTemplates(templateDir string) error

LoadTemplates 加载模板

func (*GoTemplateEngine) Render

func (e *GoTemplateEngine) Render(templateName string, data interface{}, output io.Writer) error

Render 渲染模板

func (*GoTemplateEngine) RenderFile

func (e *GoTemplateEngine) RenderFile(templatePath string, data interface{}, outputPath string) error

RenderFile 渲染文件模板

func (*GoTemplateEngine) RenderString

func (e *GoTemplateEngine) RenderString(templateStr string, data interface{}) (string, error)

RenderString 渲染字符串模板

type Middleware

type Middleware struct {
	Name        string            `json:"name"`
	Type        string            `json:"type"`
	Priority    int               `json:"priority"`
	Config      map[string]string `json:"config,omitempty"`
	Description string            `json:"description,omitempty"`
}

Middleware 中间件

type Model

type Model struct {
	Name        string            `json:"name"`
	Type        string            `json:"type"`
	Fields      []Field           `json:"fields"`
	Description string            `json:"description,omitempty"`
	Tags        []string          `json:"tags,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

Model 数据模型

type Service

type Service struct {
	Name        string            `json:"name"`
	Type        string            `json:"type"` // http, grpc, cron, etc.
	Endpoints   []Endpoint        `json:"endpoints,omitempty"`
	Config      map[string]string `json:"config,omitempty"`
	Description string            `json:"description,omitempty"`
}

Service 服务定义

type SpecValidator

type SpecValidator struct{}

SpecValidator 规范验证器

func NewSpecValidator

func NewSpecValidator() *SpecValidator

NewSpecValidator 创建规范验证器

func (*SpecValidator) ValidateOptions

func (v *SpecValidator) ValidateOptions(options *GenerateOptions) error

ValidateOptions 验证生成选项

func (*SpecValidator) ValidateSpec

func (v *SpecValidator) ValidateSpec(spec *APISpec) error

ValidateSpec 验证API规范

type TemplateEngine

type TemplateEngine interface {
	// Render 渲染模板
	Render(templateName string, data interface{}, output io.Writer) error

	// RenderFile 渲染文件模板
	RenderFile(templatePath string, data interface{}, outputPath string) error

	// RenderString 渲染字符串模板
	RenderString(template string, data interface{}) (string, error)

	// LoadTemplates 加载模板
	LoadTemplates(templateDir string) error
}

TemplateEngine 模板引擎接口

type Validation

type Validation struct {
	Min       *int     `json:"min,omitempty"`
	Max       *int     `json:"max,omitempty"`
	Pattern   string   `json:"pattern,omitempty"`
	MinLength *int     `json:"min_length,omitempty"`
	MaxLength *int     `json:"max_length,omitempty"`
	Enum      []string `json:"enum,omitempty"`
	Custom    string   `json:"custom,omitempty"`
}

Validation 字段验证规则

type Validator

type Validator interface {
	// ValidateSpec 验证API规范
	ValidateSpec(spec *APISpec) error

	// ValidateOptions 验证生成选项
	ValidateOptions(options *GenerateOptions) error
}

Validator 验证器接口

Jump to

Keyboard shortcuts

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