-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
360 lines (289 loc) · 9.34 KB
/
gen.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package protodefgen
import (
"fmt"
"os"
"strings"
)
type ProtoWriterOpts struct {
IndentWidth uint
}
type protoDefWriter struct {
sb strings.Builder
Proto *Proto
Opts *ProtoWriterOpts
}
func NewProtoDefWriter(p *Proto, o *ProtoWriterOpts) ProtoDefWriter {
return &protoDefWriter{
Proto: p,
Opts: o,
}
}
func (pw *protoDefWriter) ToStringBuilder() (*strings.Builder, error) {
if err := pw.addDescription(pw.Proto.Description, 0); err != nil {
return nil, fmt.Errorf("could not add description for proto file: %v", err)
}
if err := pw.writeLine("", 0); err != nil {
return nil, fmt.Errorf("could not write empty line: %v", err)
}
if err := pw.addSyntax(); err != nil {
return nil, fmt.Errorf("could not add syntax: %v", err)
}
if err := pw.addPackage(); err != nil {
return nil, fmt.Errorf("could not add package: %v", err)
}
if err := pw.addImports(); err != nil {
return nil, fmt.Errorf("could not add imports: %v", err)
}
if err := pw.addEnums(); err != nil {
return nil, fmt.Errorf("could not add enums: %v", err)
}
if err := pw.addMessages(); err != nil {
return nil, fmt.Errorf("could not add messages: %v", err)
}
if err := pw.addServices(); err != nil {
return nil, fmt.Errorf("could not add services: %v", err)
}
return &pw.sb, nil
}
func (pw *protoDefWriter) ToFile(path string) error {
sb, err := pw.ToStringBuilder()
if err != nil {
return fmt.Errorf("could not get string builder from proto: %v", err)
}
if err := os.WriteFile(path, []byte(sb.String()), os.ModePerm); err != nil {
return fmt.Errorf("could not write proto to file %s: %v", path, err)
}
return nil
}
func (pw *protoDefWriter) writeLine(input string, indents uint) error {
// TODO: optimize this
for i := 0; i < int(indents)*int(pw.Opts.IndentWidth); i++ {
if _, err := pw.sb.WriteString(" "); err != nil {
return fmt.Errorf("could not add indent %d: %v", i, err)
}
}
if _, err := pw.sb.WriteString(input); err != nil {
return fmt.Errorf("could not add input: %v", err)
}
if _, err := pw.sb.WriteString("\n"); err != nil {
return fmt.Errorf("could not add newline: %v", err)
}
return nil
}
func (pw *protoDefWriter) addDescription(description string, indents uint) error {
if isEmptyStr(description) {
return nil
}
for _, line := range strings.Split(description, "\n") {
if err := pw.writeLine(fmt.Sprintf("// %s", line), indents); err != nil {
return err
}
}
return nil
}
func (pw *protoDefWriter) addSyntax() error {
if err := pw.writeLine("syntax = \"proto3\";", 0); err != nil {
return fmt.Errorf("could not write syntax: %v", err)
}
if err := pw.writeLine("", 0); err != nil {
return fmt.Errorf("could not write empty line: %v", err)
}
return nil
}
func (pw *protoDefWriter) addPackage() error {
if isEmptyStr(pw.Proto.Package) {
return fmt.Errorf("package name cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("package %s;", pw.Proto.Package), 0); err != nil {
return fmt.Errorf("could not add package: %v", err)
}
if err := pw.writeLine("", 0); err != nil {
return fmt.Errorf("could not write empty line: %v", err)
}
return nil
}
func (pw *protoDefWriter) addImports() error {
for _, imp := range pw.Proto.Imports {
if err := pw.addImport(imp); err != nil {
return fmt.Errorf("could not add import %s: %v", imp, err)
}
}
if len(pw.Proto.Imports) > 0 {
if err := pw.writeLine("", 0); err != nil {
return fmt.Errorf("could not write empty line: %v", err)
}
}
return nil
}
func (pw *protoDefWriter) addImport(imp string) error {
if isEmptyStr(imp) {
return fmt.Errorf("import cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("import \"%s\";", imp), 0); err != nil {
return fmt.Errorf("could not add import: %v", err)
}
return nil
}
func (pw *protoDefWriter) addMessages() error {
for i := range pw.Proto.Messages {
if err := pw.addMessage(&pw.Proto.Messages[i], 0); err != nil {
return fmt.Errorf("could not add message %s at index %d: %v", pw.Proto.Messages[i].Name, i, err)
}
}
return nil
}
func (pw *protoDefWriter) addMessage(m *Message, indents uint) error {
if err := pw.addDescription(m.Description, indents); err != nil {
return fmt.Errorf("could not add description for message: %v", err)
}
if isEmptyStr(m.Name) {
return fmt.Errorf("message name cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("message %s {", m.Name), indents); err != nil {
return fmt.Errorf("could not add message start: %v", err)
}
for i := range m.Enums {
if err := pw.addEnum(&m.Enums[i], indents+1); err != nil {
return fmt.Errorf("could not add enum %s at index %d: %v", m.Enums[i].Name, i, err)
}
}
for i := range m.Messages {
if err := pw.addMessage(&m.Messages[i], indents+1); err != nil {
return fmt.Errorf("could not add message %s at index %d: %v", m.Messages[i].Name, i, err)
}
}
for i := range m.Fields {
if err := pw.addMessageField(&m.Fields[i], indents+1); err != nil {
return fmt.Errorf("could not add message field %s at index %d: %v", m.Fields[i].Name, i, err)
}
}
if err := pw.writeLine("}\n", indents); err != nil {
return fmt.Errorf("could not add message end: %v", err)
}
return nil
}
func (pw *protoDefWriter) addMessageField(f *MessageField, indents uint) error {
if err := pw.addDescription(f.Description, indents); err != nil {
return fmt.Errorf("could not add description for field: %v", err)
}
if isEmptyStr(f.Name) {
return fmt.Errorf("field name cannot be empty")
}
// TODO: validate type ?
if isEmptyStr(f.Type) {
return fmt.Errorf("field type cannot be empty")
}
if f.Id < 1 {
return fmt.Errorf("field id cannot be less than 1")
}
cardinality := ""
if f.Optional {
cardinality = "optional "
} else if f.Repeated {
cardinality = "repeated "
}
if err := pw.writeLine(fmt.Sprintf("%s%s %s = %d;", cardinality, f.Type, f.Name, f.Id), indents); err != nil {
return fmt.Errorf("could not add field line: %v", err)
}
return nil
}
func (pw *protoDefWriter) addEnums() error {
for i := range pw.Proto.Enums {
if err := pw.addEnum(&pw.Proto.Enums[i], 0); err != nil {
return fmt.Errorf("could not add enum %s at index %d: %v", pw.Proto.Enums[i].Name, i, err)
}
}
return nil
}
func (pw *protoDefWriter) addEnum(e *Enum, indents uint) error {
if err := pw.addDescription(e.Description, indents); err != nil {
return fmt.Errorf("could not add description for enum: %v", err)
}
if isEmptyStr(e.Name) {
return fmt.Errorf("enum name cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("enum %s {", e.Name), indents); err != nil {
return fmt.Errorf("could not add enum start: %v", err)
}
if len(e.Constants) < 1 {
return fmt.Errorf("must have at least one enum constant")
}
if e.Constants[0].Value != 0 {
return fmt.Errorf("first enum constant must have a value 0")
}
for i := range e.Constants {
if err := pw.addEnumConstant(&e.Constants[i], indents+1); err != nil {
return fmt.Errorf("could not add enum constant %s at index %d: %v", e.Constants[i].Name, i, err)
}
}
if err := pw.writeLine("}\n", indents); err != nil {
return fmt.Errorf("could not add enum end: %v", err)
}
return nil
}
func (pw *protoDefWriter) addEnumConstant(c *EnumConstant, indents uint) error {
if err := pw.addDescription(c.Description, indents); err != nil {
return fmt.Errorf("could not add description for costant: %v", err)
}
if isEmptyStr(c.Name) {
return fmt.Errorf("constant name cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("%s = %d;", c.Name, c.Value), indents); err != nil {
return fmt.Errorf("could not add constant line: %v", err)
}
return nil
}
func (pw *protoDefWriter) addServices() error {
for i := range pw.Proto.Services {
if err := pw.addService(&pw.Proto.Services[i], 0); err != nil {
return fmt.Errorf("could not add service %s at index %d: %v", pw.Proto.Services[i].Name, i, err)
}
}
return nil
}
func (pw *protoDefWriter) addService(s *Service, indents uint) error {
if err := pw.addDescription(s.Description, indents); err != nil {
return fmt.Errorf("could not add description for service: %v", err)
}
if isEmptyStr(s.Name) {
return fmt.Errorf("service name cannot be empty")
}
if err := pw.writeLine(fmt.Sprintf("service %s {", s.Name), indents); err != nil {
return fmt.Errorf("could not add service start: %v", err)
}
for i := range s.Methods {
if err := pw.addServiceMethod(&s.Methods[i], indents+1); err != nil {
return fmt.Errorf("could not add service method %s at index %d: %v", s.Methods[i].Name, i, err)
}
}
if err := pw.writeLine("}\n", indents); err != nil {
return fmt.Errorf("could not add service end: %v", err)
}
return nil
}
func (pw *protoDefWriter) addServiceMethod(m *ServiceMethod, indents uint) error {
if err := pw.addDescription(m.Description, indents); err != nil {
return fmt.Errorf("could not add description for method: %v", err)
}
if isEmptyStr(m.Name) {
return fmt.Errorf("method name cannot be empty")
}
if isEmptyStr(m.Request) {
return fmt.Errorf("method request cannot be empty")
}
if isEmptyStr(m.Response) {
return fmt.Errorf("method response cannot be empty")
}
streamReq := ""
if m.StreamRequest {
streamReq = "stream "
}
streamRes := ""
if m.StreamResponse {
streamRes = "stream "
}
if err := pw.writeLine(fmt.Sprintf("rpc %s(%s%s) returns (%s%s);", m.Name, streamReq, m.Request, streamRes, m.Response), indents); err != nil {
return fmt.Errorf("could not add method line: %v", err)
}
return nil
}