代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。