-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (50 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Package protoc_gen_go_enumvalue -----------------------------
// @file : main.go
// @author : JJXu
// @contact : [email protected]
// @time : 2023/10/7 22:17
// -------------------------------------------
package main
import (
gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
"google.golang.org/protobuf/compiler/protogen"
)
func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
GenerateFile(gen, f)
}
gen.SupportedFeatures = gengo.SupportedFeatures
return nil
})
}
func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
filename := file.GeneratedFilenamePrefix + ".pb.enumValue.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
//packageDoc := genPackageKnownComment(f)
g.P("package ", file.GoPackageName)
g.P()
g.P("import (")
g.P("\"github.com/golang/protobuf/proto\"")
g.P("\"github.com/dorlolo/protoc-gen-go-meta/meta\"")
g.P("\"strconv\"")
g.P(")")
for _, e := range file.Enums {
g.P("func (x ", e.GoIdent, ") Value() string {")
g.P("extOpts, err := proto.GetExtension(proto.MessageV1(x.Descriptor().Values().ByNumber(x.Number()).Options()), meta.E_EnumValue)")
g.P("if err != nil {")
g.P("return strconv.Itoa(int(x))")
g.P("}")
g.P("enumOptions, ok := extOpts.(*string)")
g.P("if ok {")
g.P("return *enumOptions")
g.P("}")
g.P("return strconv.Itoa(int(x))")
g.P("}")
g.P()
}
return g
}