forked from xlab/goa
/
protobuf.go
447 lines (418 loc) · 13.2 KB
/
protobuf.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package codegen
import (
"fmt"
"strconv"
"strings"
"goa.design/goa/expr"
"goa.design/goa/codegen"
)
type (
// protoBufScope is the scope for protocol buffer attribute types.
protoBufScope struct {
scope *codegen.NameScope
}
)
// Name returns the protocol buffer type name.
func (p *protoBufScope) Name(att *expr.AttributeExpr, pkg string) string {
return protoBufGoFullTypeName(att, pkg, p.scope)
}
// Ref returns the protocol buffer type reference.
func (p *protoBufScope) Ref(att *expr.AttributeExpr, pkg string) string {
return protoBufGoFullTypeRef(att, pkg, p.scope)
}
// Field returns the field name as generated by protocol buffer compiler.
// NOTE: protoc does not care about common initialisms like api -> API so we
// first transform the name into snake case to end up with Api.
func (p *protoBufScope) Field(att *expr.AttributeExpr, name string, firstUpper bool) string {
return protoBufifyAtt(att, codegen.SnakeCase(name), firstUpper)
}
// Scope returns the name scope.
func (p *protoBufScope) Scope() *codegen.NameScope {
return p.scope
}
// protoBufTypeContext returns a contextual attribute for the protocol buffer type.
func protoBufTypeContext(pkg string, scope *codegen.NameScope) *codegen.AttributeContext {
ctx := codegen.NewAttributeContext(false, true, true, pkg, scope)
ctx.Scope = &protoBufScope{scope: scope}
return ctx
}
// makeProtoBufMessage recursively transforms the given attribute expression
// to generate a valid protocol buffer message definition in the proto file.
// A protocol buffer message is always a user type in goa v2.
//
// NOTE: Protocol buffer does not provide native support for nested
// arrays/maps (See grpc/docs/FAQ.md)
//
// makeProtoBufMessage ensures the resulting attribute is a user type. If the
// given attribute type is a primitive, array, or a map, it wraps the given
// attribute under an attribute with name "field" and RPC tag number 1. For,
// nested arrays/maps, the inner array/map is wrapped into a user type.
func makeProtoBufMessage(att *expr.AttributeExpr, tname string, sd *ServiceData) *expr.AttributeExpr {
att = expr.DupAtt(att)
switch dt := att.Type.(type) {
case expr.Primitive:
wrapAttr(att, tname, sd)
return att
case expr.UserType:
if dt == expr.Empty {
// Empty type must generate a message definition
att.Type = &expr.UserTypeExpr{
TypeName: tname,
AttributeExpr: &expr.AttributeExpr{Type: &expr.Object{}},
UID: sd.Name + "#" + tname,
}
return att
} else if rt, ok := dt.(*expr.ResultTypeExpr); ok && expr.IsArray(rt) {
// result type collection
wrapAttr(att, tname, sd)
}
case *expr.Array, *expr.Map:
wrapAttr(att, tname, sd)
case *expr.Object:
att.Type = &expr.UserTypeExpr{
TypeName: tname,
AttributeExpr: expr.DupAtt(att),
UID: sd.Name + "#" + tname,
}
}
// wrap nested arrays/maps
n := ""
makeProtoBufMessageR(att, &n, sd)
return att
}
// makeProtoBufMessageR is the recursive implementation of makeProtoBufMessage.
func makeProtoBufMessageR(att *expr.AttributeExpr, tname *string, sd *ServiceData, seen ...map[string]struct{}) {
wrap := func(att *expr.AttributeExpr, tname string) {
switch dt := att.Type.(type) {
case *expr.Array:
wrapAttr(att, "ArrayOf"+tname+
protoBufify(protoBufMessageDef(dt.ElemType, sd), true), sd)
case *expr.Map:
wrapAttr(att, tname+"MapOf"+
protoBufify(protoBufMessageDef(dt.KeyType, sd), true)+
protoBufify(protoBufMessageDef(dt.ElemType, sd), true), sd)
}
}
switch dt := att.Type.(type) {
case expr.UserType:
var s map[string]struct{}
if len(seen) > 0 {
s = seen[0]
} else {
s = make(map[string]struct{})
seen = append(seen, s)
}
if _, ok := s[dt.ID()]; ok {
return
}
s[dt.ID()] = struct{}{}
if rt, ok := dt.(*expr.ResultTypeExpr); ok && expr.IsArray(rt) {
wrapAttr(rt.Attribute(), rt.Name(), sd)
}
makeProtoBufMessageR(dt.Attribute(), tname, sd, seen...)
case *expr.Array:
makeProtoBufMessageR(dt.ElemType, tname, sd, seen...)
wrap(dt.ElemType, *tname)
case *expr.Map:
// need not worry about map keys because protocol buffer supports
// only primitives as map keys.
makeProtoBufMessageR(dt.ElemType, tname, sd, seen...)
wrap(dt.ElemType, *tname)
case *expr.Object:
for _, nat := range *dt {
makeProtoBufMessageR(nat.Attribute, tname, sd, seen...)
}
}
}
// wrapAttr makes the attribute type a user type by wrapping the given
// attribute into an attribute named "field".
func wrapAttr(att *expr.AttributeExpr, tname string, sd *ServiceData) {
wrap := func(att *expr.AttributeExpr) *expr.AttributeExpr {
return &expr.AttributeExpr{
Type: &expr.Object{
&expr.NamedAttributeExpr{
Name: "field",
Attribute: &expr.AttributeExpr{
Type: att.Type,
Meta: expr.MetaExpr{"rpc:tag": []string{"1"}},
},
},
},
}
}
switch dt := att.Type.(type) {
case expr.UserType:
// Don't change the original user type. Create a copy and wrap that.
ut := expr.Dup(dt).(expr.UserType)
ut.SetAttribute(wrap(ut.Attribute()))
att.Type = ut
default:
att.Type = &expr.UserTypeExpr{
TypeName: tname,
AttributeExpr: wrap(att),
UID: sd.Name + "#" + tname,
}
}
}
// unwrapAttr returns the attribute under the attribute name "field".
// If "field" does not exist, it returns the given attribute.
func unwrapAttr(att *expr.AttributeExpr) *expr.AttributeExpr {
if a := att.Find("field"); a != nil {
return a
}
return att
}
// protoBufMessageName returns the protocol buffer message name of the given
// attribute type.
func protoBufMessageName(att *expr.AttributeExpr, s *codegen.NameScope) string {
return protoBufFullMessageName(att, "", s)
}
// protoBufFullMessageName returns the protocol buffer message name of the
// given user type qualified with the given package name if applicable.
func protoBufFullMessageName(att *expr.AttributeExpr, pkg string, s *codegen.NameScope) string {
switch actual := att.Type.(type) {
case expr.UserType:
n := s.HashedUnique(actual, protoBufify(actual.Name(), true), "")
if pkg == "" {
return n
}
return pkg + "." + n
case expr.CompositeExpr:
return protoBufFullMessageName(actual.Attribute(), pkg, s)
default:
panic(fmt.Sprintf("data type is not a user type: received type %T", actual)) // bug
}
}
// protoBufGoFullTypeName returns the protocol buffer type name for the given
// attribute generated after compiling the proto file (in *.pb.go).
func protoBufGoTypeName(att *expr.AttributeExpr, s *codegen.NameScope) string {
return protoBufGoFullTypeName(att, "", s)
}
// protoBufGoFullTypeName returns the protocol buffer type name qualified with
// the given package name for the given attribute generated after compiling
// the proto file (in *.pb.go).
func protoBufGoFullTypeName(att *expr.AttributeExpr, pkg string, s *codegen.NameScope) string {
switch actual := att.Type.(type) {
case expr.UserType, expr.CompositeExpr:
return protoBufFullMessageName(att, pkg, s)
case expr.Primitive:
return protoBufNativeGoTypeName(actual)
case *expr.Array:
return "[]" + protoBufGoFullTypeRef(actual.ElemType, pkg, s)
case *expr.Map:
return fmt.Sprintf("map[%s]%s",
protoBufGoFullTypeRef(actual.KeyType, pkg, s),
protoBufGoFullTypeRef(actual.ElemType, pkg, s))
case *expr.Object:
return s.GoTypeDef(att, false, false)
default:
panic(fmt.Sprintf("unknown data type %T", actual)) // bug
}
}
// protoBufMessageDef returns the protocol buffer code that defines a message
// which matches the data structure definition (the part that comes after
// `message foo`). The message is defined using the proto3 syntax.
func protoBufMessageDef(att *expr.AttributeExpr, sd *ServiceData) string {
switch actual := att.Type.(type) {
case expr.Primitive:
return protoBufNativeMessageTypeName(att.Type)
case *expr.Array:
return "repeated " + protoBufMessageDef(actual.ElemType, sd)
case *expr.Map:
return fmt.Sprintf("map<%s, %s>", protoBufMessageDef(actual.KeyType, sd), protoBufMessageDef(actual.ElemType, sd))
case expr.UserType:
return protoBufMessageName(att, sd.Scope)
case *expr.Object:
var ss []string
ss = append(ss, " {")
for _, nat := range *actual {
var (
fn string
fnum uint64
typ string
desc string
)
{
fn = codegen.SnakeCase(protoBufify(nat.Name, false))
fnum = rpcTag(nat.Attribute)
typ = protoBufMessageDef(nat.Attribute, sd)
if nat.Attribute.Description != "" {
desc = codegen.Comment(nat.Attribute.Description) + "\n\t"
}
}
ss = append(ss, fmt.Sprintf("\t%s%s %s = %d;", desc, typ, fn, fnum))
}
ss = append(ss, "}")
return strings.Join(ss, "\n")
default:
panic(fmt.Sprintf("unknown data type %T", actual)) // bug
}
}
// protoBufGoFullTypeRef returns the Go code qualified with package name that
// refers to the Go type generated by compiling the protocol buffer
// (in *.pb.go) for the given attribute.
func protoBufGoFullTypeRef(att *expr.AttributeExpr, pkg string, s *codegen.NameScope) string {
name := protoBufGoFullTypeName(att, pkg, s)
if expr.IsObject(att.Type) {
return "*" + name
}
return name
}
// protoBufify makes a valid protocol buffer identifier out of any string.
// It does that by removing any non letter and non digit character and by
// making sure the first character is a letter or "_". protoBufify produces a
// "CamelCase" version of the string.
//
// If firstUpper is true the first character of the identifier is uppercase
// otherwise it's lowercase.
func protoBufify(str string, firstUpper bool) string {
// Optimize trivial case
if str == "" {
return ""
}
// Remove optional suffix that defines corresponding transport specific
// name.
idx := strings.Index(str, ":")
if idx > 0 {
str = str[:idx]
}
// We must disable acronyms to generate gRPC compatible names.
str = codegen.CamelCase(str, firstUpper, false)
if str == "" {
// All characters are invalid. Produce a default value.
if firstUpper {
return "Val"
}
return "val"
}
return fixReservedProtoBuf(str)
}
// protoBufifyAtt honors any struct:field:name meta set on the attribute and
// and calls protoBufify with the tag value if present or the given name
// otherwise.
func protoBufifyAtt(att *expr.AttributeExpr, name string, upper bool) string {
if tname, ok := att.Meta["struct:field:name"]; ok {
if len(tname) > 0 {
name = tname[0]
}
}
return protoBufify(name, upper)
}
// protoBufNativeMessageTypeName returns the protocol buffer built-in type
// corresponding to the given primitive type. It panics if t is not a
// primitive type.
func protoBufNativeMessageTypeName(t expr.DataType) string {
switch t.Kind() {
case expr.BooleanKind:
return "bool"
case expr.IntKind:
return "sint32"
case expr.Int32Kind:
return "sint32"
case expr.Int64Kind:
return "sint64"
case expr.UIntKind:
return "uint32"
case expr.UInt32Kind:
return "uint32"
case expr.UInt64Kind:
return "uint64"
case expr.Float32Kind:
return "float"
case expr.Float64Kind:
return "double"
case expr.StringKind:
return "string"
case expr.BytesKind:
return "bytes"
default:
panic(fmt.Sprintf("cannot compute native protocol buffer type for %T", t)) // bug
}
}
// protoBufNativeGoTypeName returns the Go type corresponding to the given
// primitive type generated by the protocol buffer compiler after compiling
// the ".proto" file (in *.pb.go).
func protoBufNativeGoTypeName(t expr.DataType) string {
switch t.Kind() {
case expr.BooleanKind:
return "bool"
case expr.IntKind:
return "int32"
case expr.Int32Kind:
return "int32"
case expr.Int64Kind:
return "int64"
case expr.UIntKind:
return "uint32"
case expr.UInt32Kind:
return "uint32"
case expr.UInt64Kind:
return "uint64"
case expr.Float32Kind:
return "float32"
case expr.Float64Kind:
return "float64"
case expr.StringKind:
return "string"
case expr.BytesKind:
return "[]byte"
default:
panic(fmt.Sprintf("cannot compute native protocol buffer type for %T", t)) // bug
}
}
// rpcTag returns the unique numbered RPC tag from the given attribute.
func rpcTag(a *expr.AttributeExpr) uint64 {
var tag uint64
if t, ok := a.FieldTag(); ok {
tn, err := strconv.ParseUint(t, 10, 64)
if err != nil {
panic(err) // bug (should catch invalid field numbers in validation)
}
tag = tn
}
return tag
}
// fixReservedProtoBuf appends an underscore on to protocol buffer reserved
// keywords.
func fixReservedProtoBuf(w string) string {
if _, ok := reservedProtoBuf[codegen.CamelCase(w, false, false)]; ok {
w += "_"
}
return w
}
var (
// reserved protocol buffer keywords and package names
reservedProtoBuf = map[string]struct{}{
// types
"bool": struct{}{},
"bytes": struct{}{},
"double": struct{}{},
"fixed32": struct{}{},
"fixed64": struct{}{},
"float": struct{}{},
"int32": struct{}{},
"int64": struct{}{},
"sfixed32": struct{}{},
"sfixed64": struct{}{},
"sint32": struct{}{},
"sint64": struct{}{},
"string": struct{}{},
"uint32": struct{}{},
"uint64": struct{}{},
// reserved keywords
"enum": struct{}{},
"import": struct{}{},
"map": struct{}{},
"message": struct{}{},
"oneof": struct{}{},
"option": struct{}{},
"package": struct{}{},
"public": struct{}{},
"repeated": struct{}{},
"reserved": struct{}{},
"returns": struct{}{},
"rpc": struct{}{},
"service": struct{}{},
"syntax": struct{}{},
}
)