-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (55 loc) · 1.41 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
56
57
58
59
60
61
62
63
package main
import (
"google.golang.org/protobuf/compiler/protogen"
"strings"
)
func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
generateFile(gen, f)
}
return nil
})
}
// generateFile generates a _enum.pb.go file containing enum.
func generateFile(gen *protogen.Plugin, file *protogen.File) {
if len(file.Enums) <= 0 {
return
}
filename := file.GeneratedFilenamePrefix + "_enum.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by protoc-gen-go-enum. DO NOT EDIT...")
g.P()
g.P("package ", file.GoPackageName)
g.P()
for _, enum := range file.Enums {
// const
comment := enum.Comments.Leading.String()
comment = strings.Trim(comment, " ")
comment = strings.Trim(comment, "\r\n")
g.P(comment)
g.P("const (")
for i, e := range enum.Values {
comment := e.Comments.Leading.String()
comment = strings.Trim(comment, " ")
comment = strings.Trim(comment, "\r\n")
g.P(e.GoIdent, " int64 ", "=", i, comment)
}
g.P(")")
// map
g.P("var (")
g.P(enum.GoIdent, "_TXT=map[int64]string {")
for _, e := range enum.Values {
comment := e.Comments.Leading.String()
comment = strings.TrimPrefix(comment, "//")
comment = strings.Trim(comment, " ")
comment = strings.Trim(comment, "\r\n")
g.P(e.GoIdent, ": \"", comment, "\",")
}
g.P("}")
g.P(")")
}
}