goku

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

protoc-gen-goku Generator

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterData

func RegisterData(data Data)

RegisterData 注册全局变量

func RegisterFunc

func RegisterFunc(name string, function interface{})

RegisterFunc 注入全局函数

func RegisterFuncMap

func RegisterFuncMap(funcMap FuncMap)

RegisterFuncMap 注入全局函数

func RegisterPlugin

func RegisterPlugin(p Plugin)

func Run

func Run(STDIN io.Reader, STDOUT io.Writer, STDERR io.Writer)

Types

type Condition

type Condition interface {
	OK(ctx *Context) bool
}

type Config

type Config struct {
	// 声明全局变量
	//  - 支持模版解析
	// 因为 map 是无序的,所以不可相互引用
	Data Data
	// 全局启用插件列表
	Plugins []string
	// 任务列表
	// TODO 改为 map[string]Job,并且params增加jobs,excludeJobs来选择要执行的job
	Jobs []Job
}

type Context

type Context struct {
	*Generator
	// contains filtered or unexported fields
}

func (*Context) Content

func (c *Context) Content() []byte

func (*Context) Data

func (c *Context) Data() Data

func (*Context) Enum

Enum ONLY [ LoopEnum ]

func (*Context) Eval

func (c *Context) Eval(text string, args ...interface{}) (string, error)

执行一段模板(脚本)

func (*Context) File

File returns current FileDescriptorProto

func (*Context) FuncMap

func (c *Context) FuncMap() FuncMap

func (*Context) GetFileName

func (c *Context) GetFileName() string

func (*Context) Loop

func (c *Context) Loop() Loop

Loop returns current loop

func (*Context) MergeData

func (c *Context) MergeData(data Data)

func (*Context) MergeFuncMap

func (c *Context) MergeFuncMap(funcMap FuncMap)

func (*Context) Message

func (c *Context) Message() *descriptors.DescriptorProto

Message ONLY [ LoopMessage ]

func (*Context) Method

Method returns current MethodDescriptorProto. ONLY [ LoopMethod ]

func (*Context) MustEval

func (c *Context) MustEval(text string, args ...interface{}) string

func (*Context) Object

func (c *Context) Object() DescriptorObject

func (*Context) ParentMessage

func (c *Context) ParentMessage() *descriptors.DescriptorProto

ParentMessage ONLY [ LoopNestedMessage LoopNestedEnum ]

func (*Context) PutValue

func (c *Context) PutValue(key interface{}, value interface{})

func (*Context) Service

Service returns current ServiceDescriptorProto. ONLY [ LoopService LoopMethod ]

func (*Context) SetContent

func (c *Context) SetContent(content []byte)

func (*Context) ShowFunc

func (c *Context) ShowFunc() string

输出当前 Context 支持的函数

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

func (*Context) WithLoop

func (c *Context) WithLoop(loop Loop, desc DescriptorObject) *Context

type Data

type Data map[string]interface{}

func (Data) Copy

func (data Data) Copy() Data

func (Data) DoMerge

func (data Data) DoMerge(other Data)

type DescriptorObject

type DescriptorObject interface {
	descriptors.DescriptorCommon
	GetName() string
}

type FuncMap

type FuncMap template.FuncMap

func (FuncMap) Copy

func (m FuncMap) Copy() FuncMap

func (FuncMap) DoMerge

func (m FuncMap) DoMerge(other FuncMap)

type Generator

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

func NewGenerator

func NewGenerator() *Generator

func (*Generator) Debug

func (g *Generator) Debug(format string, args ...interface{})

func (*Generator) Fatal

func (g *Generator) Fatal(format string, args ...interface{})

func (*Generator) FatalOnErr

func (g *Generator) FatalOnErr(err error, format string, args ...interface{})

FatalOnErr 当 err!=nil 时报错并退出进程

func (*Generator) GetDescriptorByName

func (g *Generator) GetDescriptorByName(typeName string) descriptors.ProtoType

通过protoc格式的名称 获取 proto 对象;

@param typeName .google.protobuf.FileDescriptorProto
@return
- message: *descriptorpb.DescriptorProto
- enum: *descriptorpb.EnumDescriptorProto

func (*Generator) GetFileByName

func (g *Generator) GetFileByName(protoFileName string) *descriptors.FileDescriptorProto

func (*Generator) GetObject

func (g *Generator) GetObject(typeName string) descriptors.ProtoType

func (*Generator) MustGetDescriptorByName

func (g *Generator) MustGetDescriptorByName(typeName string) descriptors.ProtoType

func (*Generator) Param

func (g *Generator) Param(key string, def string) string

func (*Generator) Recover

func (g *Generator) Recover(errPtr *error, msgs ...string)

func (*Generator) Run

func (g *Generator) Run(IN io.Reader, OUT io.Writer, ERR io.Writer)

func (*Generator) ThrowsOnErr

func (g *Generator) ThrowsOnErr(err error)

func (*Generator) Value

func (g *Generator) Value(key interface{}) interface{}

See Context.Value

func (*Generator) Warn

func (g *Generator) Warn(format string, args ...interface{})

func (*Generator) WriteOutFile

func (g *Generator) WriteOutFile(filename string, content []byte) error

输出文件 See pluginpb.CodeGeneratorResponse_File

type IfCondition

type IfCondition string

func (IfCondition) OK

func (t IfCondition) OK(ctx *Context) bool

type Job

type Job struct {
	// 任务名称
	Name string
	// 根据所处阶段
	Loop LoopCondition
	// 用户自定义的判断条件:返回 "true"/"false"
	//  - 支持模版解析
	If IfCondition
	// 模版内容
	Template string
	// 模板路径 - 支持模板解析
	//  -- Template 为空时才会读取模板文件
	TemplatePath string
	// 输出文件路径
	Out string
	// 启用插件列表
	//
	Plugins []string
	// 任务级别的变量
	// - 与全局的 Config.Data
	Data Data
}

func (Job) GetConditions

func (j Job) GetConditions() []Condition

func (Job) IsEnable

func (j Job) IsEnable(ctx *Context) bool

type Loop

type Loop string
const (
	LoopFile          Loop = "file"
	LoopService       Loop = "service"
	LoopMethod        Loop = "method"
	LoopMessage       Loop = "message"
	LoopEnum          Loop = "enum"
	LoopNestedMessage Loop = "nested_message" // 嵌套在message中的message
	LoopNestedEnum    Loop = "nested_enum"    // 嵌套在message中的enum
)

type LoopCondition

type LoopCondition []Loop

func (LoopCondition) OK

func (t LoopCondition) OK(ctx *Context) bool

type Plugin

type Plugin interface {
	Name() string
	Init(c *Generator)
	// BeforeExecute Job 执行前的回调
	// - 注入 Data
	// - 注入 FuncMap
	BeforeExecute(ctx *Context)
	// BeforeOut Job 执行完成,输出前的回调
	// - 可修改输出的内容,如格式化等
	BeforeOut(ctx *Context)
}

Plugin 模板数据插件

Jump to

Keyboard shortcuts

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