Skip to content

Commit b344db7

Browse files
authored
Merge pull request #5 from derekhjray/main
add custom enum and fix issues
2 parents 42bf4e8 + c16f398 commit b344db7

File tree

6 files changed

+387
-30
lines changed

6 files changed

+387
-30
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ protoc-gen-ego: $(SOURCES)
99
install: $(SOURCES)
1010
go install -ldflags '-s -w -X main.version=$(VERSION) -X main.rc=$(RC)'
1111

12+
.PHONY: desc
13+
desc: ./testdata/message.proto
14+
protoc --proto_path=./testdata --plugin=protoc-gen-ego=./gen_desc --ego_out=paths=source_relative,enum=camelcase:./testdata message.proto
15+
1216
.PHONY: test
1317
test: protoc-gen-ego $(TEST_PROTO_FILES)
1418
protoc --proto_path=. --plugin=protoc-gen-ego=./protoc-gen-ego --ego_out=paths=source_relative,enum=camelcase:. $(TEST_PROTO_FILES)

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ const (
5252
)
5353
```
5454

55+
### Customize Enum Name
56+
- protobuf source code
57+
using `@go.enum=strip` to strip the enum type name
58+
```protobuf
59+
// @go.enum=strip
60+
enum OS {
61+
HARMONY = 0;
62+
// @go.name=IOS
63+
IOS = 1;
64+
ANDROID = 2;
65+
}
66+
```
67+
68+
- generated go source code
69+
```go
70+
type OS int32
71+
const (
72+
Harmony OS = 0
73+
IOS OS = 1
74+
Android OS = 2
75+
)
76+
```
77+
5578
### Generate Field Tags
5679

5780
- protobuf source code

runner.go

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"fmt"
67
"go/ast"
@@ -114,28 +115,168 @@ func refactorProtoSources(files []*protogen.File) (descs []*FileDescriptor, err
114115
return
115116
}
116117

118+
const (
119+
stripNone = iota
120+
stripNested
121+
stripAll
122+
)
123+
117124
func refactorEnumConstants(enums []*protogen.Enum, prefixes ...string) error {
118125
if !camelcaseEnumConstants {
119126
return nil
120127
}
121128

122-
var prefix string
129+
var (
130+
prefix string
131+
enumType string
132+
)
133+
123134
if len(prefixes) > 0 {
124135
prefix = prefixes[0]
125136
}
126137

127138
for _, enum := range enums {
128-
enum.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName)
139+
enumType = strings.TrimPrefix(enum.GoIdent.GoName, prefix)
140+
strip := renameEnumType(enum)
129141
for _, value := range enum.Values {
130-
value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix)
131-
value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, enum.GoIdent.GoName)
132-
value.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName + value.GoIdent.GoName)
142+
renameEnumValue(value, prefix, enumType, strip)
133143
}
134144
}
135145

136146
return nil
137147
}
138148

149+
func renameEnumType(enum *protogen.Enum) (strip int) {
150+
const (
151+
stripComment = "@go.enum=strip"
152+
goNameComment = "@go.name="
153+
)
154+
155+
var name string
156+
157+
leading := enum.Comments.Leading.String()
158+
var buf bytes.Buffer
159+
scanner := bufio.NewScanner(strings.NewReader(leading))
160+
for scanner.Scan() {
161+
line := scanner.Text()
162+
if line == "" {
163+
continue
164+
}
165+
166+
if index := strings.Index(line, stripComment); index >= 0 {
167+
line = line[index+len(stripComment):]
168+
if strings.HasPrefix(line, "=nested") {
169+
strip = stripNested
170+
} else {
171+
strip = stripAll
172+
}
173+
174+
continue
175+
}
176+
177+
if index := strings.Index(line, goNameComment); index >= 0 {
178+
name = strings.TrimSpace(line[index+len(goNameComment):])
179+
continue
180+
}
181+
182+
buf.WriteString(line)
183+
buf.WriteByte('\n')
184+
}
185+
186+
enum.Comments.Leading = protogen.Comments(buf.String())
187+
188+
trailing := enum.Comments.Trailing.String()
189+
if index := strings.Index(trailing, stripComment); index >= 0 {
190+
trailing = trailing[index+len(stripComment):]
191+
if strings.HasPrefix(trailing, "=nested") {
192+
strip = stripNested
193+
} else {
194+
strip = stripAll
195+
}
196+
enum.Comments.Trailing = ""
197+
} else if index := strings.Index(trailing, goNameComment); index >= 0 {
198+
enum.Comments.Trailing = ""
199+
name = strings.TrimSpace(trailing[index+len(goNameComment):])
200+
}
201+
202+
if name != "" {
203+
enum.GoIdent.GoName = name
204+
} else {
205+
enum.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName)
206+
}
207+
208+
return
209+
}
210+
211+
func renameEnumValue(value *protogen.EnumValue, prefix, enum string, strip int) {
212+
var (
213+
name string
214+
buf bytes.Buffer
215+
)
216+
217+
const goNameComment = "@go.name="
218+
219+
scanner := bufio.NewScanner(strings.NewReader(value.Comments.Leading.String()))
220+
for scanner.Scan() {
221+
line := scanner.Text()
222+
if line == "" {
223+
continue
224+
}
225+
226+
if index := strings.Index(line, goNameComment); index >= 0 {
227+
name = strings.TrimSpace(line[index+len(goNameComment):])
228+
continue
229+
}
230+
231+
buf.WriteString(line)
232+
buf.WriteByte('\n')
233+
}
234+
235+
value.Comments.Leading = protogen.Comments(buf.String())
236+
if name == "" {
237+
trailing := value.Comments.Trailing.String()
238+
if index := strings.Index(trailing, goNameComment); index >= 0 {
239+
name = strings.TrimSpace(trailing[index+len(goNameComment):])
240+
value.Comments.Trailing = ""
241+
}
242+
}
243+
244+
switch strip {
245+
case stripAll:
246+
if name != "" {
247+
value.GoIdent.GoName = name
248+
return
249+
}
250+
251+
if prefix != "" {
252+
value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix+"_")
253+
} else {
254+
value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, enum+"_")
255+
256+
}
257+
case stripNested:
258+
if name != "" {
259+
value.GoIdent.GoName = strcase.UpperCamelCase(enum) + name
260+
return
261+
} else {
262+
value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix+"_")
263+
value.GoIdent.GoName = enum + "_" + value.GoIdent.GoName
264+
}
265+
default:
266+
if name != "" {
267+
if prefix != "" {
268+
value.GoIdent.GoName = strcase.UpperCamelCase(prefix) + name
269+
} else {
270+
value.GoIdent.GoName = strcase.UpperCamelCase(enum) + name
271+
}
272+
273+
return
274+
}
275+
}
276+
277+
value.GoIdent.GoName = strcase.UpperCamelCase(value.GoIdent.GoName)
278+
}
279+
139280
func regenerateGoSources(descs []*FileDescriptor, sources []*pluginpb.CodeGeneratorResponse_File) (err error) {
140281
if len(descs) == 0 || len(sources) == 0 {
141282
return

testdata/message.desc

975 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)