forked from utilitywarehouse/protoc-gen-go-enums
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (111 loc) · 2.99 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"strings"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
var includeNested bool
func main() {
protogen.Options{
ParamFunc: func(name, value string) error {
switch name {
case "include_nested":
switch value {
case "true", "yes", "y", "1":
includeNested = true
}
}
return nil
},
}.Run(generate)
}
func generate(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
newFileEnumGenerator(f, gen).Generate()
}
return nil
}
type fileEnumGenerator struct {
f *protogen.File
gen *protogen.Plugin
allConsts map[string]struct{}
gf *protogen.GeneratedFile
}
func newFileEnumGenerator(f *protogen.File, gen *protogen.Plugin) *fileEnumGenerator {
gen.SupportedFeatures |= uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
return &fileEnumGenerator{f: f, gen: gen, allConsts: make(map[string]struct{})}
}
func (eg *fileEnumGenerator) Generate() {
eg.gf = eg.gen.NewGeneratedFile(eg.f.GeneratedFilenamePrefix+".pb.enums.go", eg.f.GoImportPath)
eg.writeHeader()
var seen int
for _, enum := range eg.f.Enums {
seen += eg.processEnum(enum)
}
if includeNested {
for _, m := range eg.f.Messages {
for _, enum := range m.Enums {
seen += eg.processEnum(enum)
}
}
}
if seen == 0 {
// No enums processed
eg.gf.Skip()
}
}
func (eg *fileEnumGenerator) writeHeader() {
eg.gf.P("// Code generated by protoc-gen-go-enums. DO NOT EDIT.")
eg.gf.P("// source: ", eg.f.Desc.Path())
eg.gf.P()
eg.gf.P("package ", eg.f.GoPackageName)
eg.gf.P()
}
func (eg *fileEnumGenerator) processEnum(enum *protogen.Enum) (seen int) {
if hasClash, clashing := eg.hasConstClash(enum); hasClash {
println(fmt.Sprintf("skipped generating constants for enum %v as it would duplicate constant %s", enum.Desc.FullName(), clashing))
return seen
}
eg.gf.P("const (")
for _, v := range enum.Values {
c := constNameFor(v)
eg.allConsts[c] = struct{}{}
if v.Desc.Options().(*descriptorpb.EnumValueOptions).GetDeprecated() {
eg.gf.P("// Deprecated: Do not use.")
}
eg.gf.P(c, " = ", eg.golangValue(v))
seen++
}
eg.gf.P(")")
eg.gf.P()
return seen
}
func (eg *fileEnumGenerator) hasConstClash(enum *protogen.Enum) (bool, string) {
for _, v := range enum.Values {
constName := constNameFor(v)
if _, exists := eg.allConsts[constName]; exists {
return true, constName
}
}
return false, ""
}
func constNameFor(v *protogen.EnumValue) string {
return string(v.Desc.Name())
}
func (eg *fileEnumGenerator) golangValue(e *protogen.EnumValue) string {
pkg := string(eg.f.Desc.Package())
typeName := strings.TrimPrefix(string(e.Parent.Desc.FullName()), ".")
typeName = strings.TrimPrefix(typeName, pkg+".")
parts := strings.Split(typeName, ".")
if len(parts) > 1 {
typeName = strings.Join(parts[0:len(parts)-1], "_")
} else {
typeName = parts[0]
}
return typeName + "_" + string(e.Desc.Name())
}