protoc的golang自动代码生成, 源码文件【google.golang.org/protobuf/types/pluginpb/plugin.pb.go】中有如下的一段文档,很好的解释了protoc插件的工作,和使用过程。
// protoc (aka the Protocol Compiler) can be extended via plugins. A plugin is
// just a program that reads a CodeGeneratorRequest from stdin and writes a
// CodeGeneratorResponse to stdout.
//
// Plugins written using C++ can use google/protobuf/compiler/plugin.h instead
// of dealing with the raw protocol defined here.
//
// A plugin executable needs only to be placed somewhere in the path. The
// plugin should be named "protoc-gen-${NAME}", and will then be used when the
// flag "--${NAME}_out" is passed to protoc.
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: google/protobuf/compiler/plugin.proto
这段话翻译过来大致如下:
protoc可以通过插件扩展。插件的工作原理是从标准输入中读取 CodeGeneratorRequest, 处理后生成CodeGeneratorResponse ,并写入标准输出中。
插件必须按照 protoc-gen-{NAME} 格式命名,在使用protoc中将, —{NAME}_out 当作参数传给protoc, 就会起作用。
实际调用过程中,可以把生成的插件文件放在环境变量PATH中,也可直接通过—plugin=[ plugin-name=plugin-path, ….] 传给protoc
protoc --plugin=protoc-gen-NAME=path/to/mybinary --NAME_out=OUT_DIR
或
protoc --NAME_out=OUT_DIR
这样, protoc就知道要使用 protoc-gen-NAME, 并输出文件到 OUT_DIR 中去了
DEMO
main.go
package main
import (
"flag"
"fmt"
"go/format"
"google.golang.org/protobuf/compiler/protogen"
"regexp"
)
func fileGen(p *protogen.Plugin,file *protogen.File) error{
fileName := *file.Proto.Name
//fmt.Println(fileName) 标准输出也会被protoc拿到
//messageName := *file.Proto.MessageType[0].Name
//fmt.Println(messageName)
mainContent := fmt.Sprintf(`
// Code generated by protoc-gen-jwtest. DO NOT EDIT.
package jwtest
import (
"fmt"
)
func Print() {
fmt.Println("hello")
}
`)
formatContent, err := format.Source([]byte(mainContent))
if err != nil {
return err
}
f := getMainGeneratedFile(p,fileName,file)
if _, err := f.Write(formatContent); err != nil {
return err
}
return nil
}
func getMainGeneratedFile(p *protogen.Plugin, fileName string, file *protogen.File) *protogen.GeneratedFile{
path := regexp.MustCompile(`^proto/(.*).proto`).ReplaceAllString(fileName, `jwtest/$1.jwtest.go`)
generatedFile := p.NewGeneratedFile(
path,
protogen.GoImportPath(file.GeneratedFilenamePrefix),
)
return generatedFile
}
func main() {
var flags flag.FlagSet
// 插件参数
opts := &protogen.Options{
ParamFunc: flags.Set,
}
opts.Run(func(plugin *protogen.Plugin) error {
for _,fileName := range plugin.Request.FileToGenerate{
fileToGenerate := plugin.FilesByPath[fileName]
if err := fileGen(plugin,fileToGenerate); err != nil {
return err
}
}
return nil
})
}
需要注意的点:不要做额外的打印操作,否则报:Plugin output is unparseable
compiler/protogen
protogen包提供了对编写protoc插件的支持
protoc插件,即protoc缓冲区编译器,是一种程序,它从标准输入读取CodeGeneratorRequest消息,并将CodeGeneratorResponse消息写入标准输出。这个包支持编写生成Go代码的插件。
文档:https://pkg.go.dev/google.golang.org/protobuf@v1.27.1/compiler/protogen