1 Star 0 Fork 0

民禧农机/tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
generator.go 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
surongzhen 提交于 2021-07-06 15:53 . 增加生成enum中文map的命令行
package generator
import (
"fmt"
"strconv"
"strings"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"gitee.com/surongzhen1213/tools/protobuf/pkg/generator"
"gitee.com/surongzhen1213/tools/protobuf/pkg/naming"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"google.golang.org/protobuf/compiler/protogen"
)
type enum struct {
generator.Base
Files []*protogen.File
filesHandled int
}
type enumItem struct {
enumDesc protoreflect.EnumDescriptor
loc protoreflect.SourceLocations
//values protoreflect.EnumValueDescriptors
}
// EnumGenerator enum generator.
func EnumGenerator() *enum {
t := &enum{}
return t
}
// Generate ...
func (t *enum) Generate(in *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
//fmt.Println("-->in:",in,"<---")
t.Setup(in)
// Showtime! Generate the response.
resp := new(plugin.CodeGeneratorResponse)
for _, f := range t.GenFiles {
respFile := t.generateForFile(f)
if respFile != nil {
resp.File = append(resp.File, respFile)
}
}
return resp
}
func (t *enum) generateForFile(file *descriptor.FileDescriptorProto) *plugin.CodeGeneratorResponse_File {
var enums []*enumItem
desc, err := protodesc.NewFile(file, new(protoregistry.Files))
if err != nil {
fmt.Println(fmt.Errorf("invalid FileDescriptorProto %q: %v", file.GetName(), err))
return nil
}
for i, eds := 0, desc.Enums(); i < eds.Len(); i++ {
//for j := 0; j < eds.Get(i).Values().Len(); j++ {
// comment := desc.SourceLocations().ByDescriptor(eds.Get(i).Values().Get(j))
// fmt.Println("comment:",comment.TrailingComments)
// comment.
//}
//enums = append(enums, &enumDescComment{
// name: string(eds.Get(i).Name()),
// desc: protodesc.ToEnumDescriptorProto(eds.Get(i)),
// loc: desc.SourceLocations().ByDescriptor(eds.Get(i)),
// values: eds.Get(i).Values(),
//})
enums = append(enums, &enumItem{
enumDesc: eds.Get(i),
loc: desc.SourceLocations(),
})
}
if len(enums) == 0 {
return nil
}
resp := new(plugin.CodeGeneratorResponse_File)
t.generateFileHeader(file, t.GenPkgName)
t.generateImports(file)
for _, enum := range enums {
t.generateEnum(file, enum)
}
resp.Name = proto.String(naming.GenFileName(file, ".enum.go"))
resp.Content = proto.String(t.FormattedOutput())
t.Output.Reset()
t.filesHandled++
return resp
}
func (t *enum) generateFileHeader(file *descriptor.FileDescriptorProto, pkgName string) {
t.P("// Code generated by protoc-gen-enum ", generator.Version, ", DO NOT EDIT.")
t.P("// source: ", file.GetName())
t.P()
if t.filesHandled == 0 {
comment, err := t.Reg.FileComments(file)
if err == nil && comment.Leading != "" {
// doc for the first file
t.P("/*")
t.P("Package ", t.GenPkgName, " is a generated enum package.")
t.P("This code was generated with protoc-gen-enum ", generator.Version, ".")
t.P()
for _, line := range strings.Split(comment.Leading, "\n") {
line = strings.TrimPrefix(line, " ")
// ensure we don't escape from the block comment
line = strings.Replace(line, "*/", "* /", -1)
t.P(line)
}
t.P()
t.P("It is generated from these files:")
for _, f := range t.GenFiles {
t.P("\t", f.GetName())
}
t.P("*/")
}
}
t.P(`package `, pkgName)
t.P()
}
func (t *enum) generateImports(file *descriptor.FileDescriptorProto) {
//
}
func (t *enum) generateEnum(file *descriptor.FileDescriptorProto, enum *enumItem) {
t.P("// ", string(enum.enumDesc.Name()), " enum")
t.P("var ", string(enum.enumDesc.Name()), "_relation = map[int32]string{")
for i := 0; i < enum.enumDesc.Values().Len(); i++ {
comment := enum.loc.ByDescriptor(enum.enumDesc.Values().Get(i))
t.P(enum.enumDesc.Values().Get(i).Number(), ": ", strconv.Quote(strings.TrimSpace(strings.TrimSuffix(comment.TrailingComments, "\n"))), ",")
}
t.P("}")
t.P()
}
// P forwards to g.gen.P, which prints output.
func (t *enum) P(args ...interface{}) {
for _, x := range args {
fmt.Fprint(t.Output, x)
}
fmt.Fprintln(t.Output)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/surongzhen1213/tools.git
git@gitee.com:surongzhen1213/tools.git
surongzhen1213
tools
tools
v1.0.7

搜索帮助