forked from simonbos/protoc-gen-go-rsn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
451 lines (404 loc) · 13.7 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
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
448
449
450
451
package main
import (
"errors"
"fmt"
"runtime/debug"
"sort"
"strings"
"github.com/iancoleman/strcase"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)
const (
extension = ".rsn.go"
fmtPackage = protogen.GoImportPath("fmt")
regexpPackage = protogen.GoImportPath("regexp")
errorsPackage = protogen.GoImportPath("errors")
typeStructField = "Type"
parentStructField = "Parent"
)
type Resource struct {
ServiceName string
Type string
Patterns []Pattern
}
func (r Resource) parentTypeTypeAlias() string {
return r.Type + "ParentType"
}
func (r Resource) structName() string {
return r.Type + "Rsn"
}
func (r Resource) parentStructName() string {
return r.Type + "ParentRsn"
}
func (r Resource) parentStructFields() []string {
result := make(map[string]struct{})
for _, pattern := range r.Patterns {
for _, field := range pattern.parentStructFields("") {
result[field] = struct{}{}
}
}
fields := make([]string, 0, len(result))
for k := range result {
fields = append(fields, k)
}
sort.Slice(fields, func(i, j int) bool {
return fields[i] < fields[j]
})
return fields
}
// lastStructFields should return a length of 1 for well-defined resource
// patterns (which have the same final collection identifier).
func (r Resource) lastStructFields() []string {
result := make(map[string]struct{})
for _, pattern := range r.Patterns {
result[pattern.lastStructField("")] = struct{}{}
}
fields := make([]string, 0, len(result))
for k := range result {
fields = append(fields, k)
}
sort.Slice(fields, func(i, j int) bool {
return fields[i] < fields[j]
})
return fields
}
type Pattern struct {
collectionIds []string
resourceVars []string
}
func (p Pattern) parentStructFields(prefix string) []string {
result := make([]string, len(p.resourceVars)-1)
for i := 0; i < len(p.resourceVars)-1; i++ {
result[i] = prefix + strcase.ToCamel(p.resourceVars[i]) + "Id"
}
return result
}
func (p Pattern) lastStructField(prefix string) string {
return prefix + strcase.ToCamel(p.resourceVars[len(p.resourceVars)-1]) + "Id"
}
func (p Pattern) parentTypeConst() string {
if len(p.resourceVars) == 1 {
// top-level
return strcase.ToCamel(p.resourceVars[0]) + "RootParentType"
}
result := ""
for _, resourceVar := range p.resourceVars {
result += strcase.ToCamel(resourceVar)
}
return result + "ParentType"
}
func (p Pattern) parentTypeString() string {
if len(p.resourceVars) == 1 {
// top-level
return ""
}
result := strcase.ToLowerCamel(p.resourceVars[0])
for i := 1; i < len(p.resourceVars)-1; i++ {
result += strcase.ToCamel(p.resourceVars[i])
}
return result
}
func (p Pattern) regexName() string {
result := ""
for i, resourceVar := range p.resourceVars {
if i == 0 {
result += strcase.ToLowerCamel(resourceVar)
} else {
result += strcase.ToCamel(resourceVar)
}
}
return result + "RsnPattern"
}
func (p Pattern) parentRegexName(t string) string {
result := strcase.ToLowerCamel(p.resourceVars[0])
for i := 1; i < len(p.resourceVars)-1; i++ {
result += strcase.ToCamel(p.resourceVars[i])
}
return result + t + "ParentPattern"
}
func (p Pattern) regexString() string {
result := "^"
for i, collectionId := range p.collectionIds {
if i != 0 {
result += "/"
}
result += collectionId + "/([^/]+)"
}
return result + "$"
}
func (p Pattern) parentRegexString() string {
result := "^"
for i := 0; i < len(p.collectionIds)-1; i++ {
if i != 0 {
result += "/"
}
result += p.collectionIds[i] + "/([^/]+)"
}
return result + "$"
}
func (p Pattern) formatString() string {
result := ""
for i, collectionId := range p.collectionIds {
if i != 0 {
result += "/"
}
result += collectionId + "/%s"
}
return result
}
func (p Pattern) parentFormatString() string {
result := ""
for i := 0; i < len(p.collectionIds)-1; i++ {
if i != 0 {
result += "/"
}
result += p.collectionIds[i] + "/%s"
}
return result
}
func NewPattern(patternString string) (Pattern, error) {
splittedPattern := strings.Split(patternString, "/")
if len(splittedPattern)%2 != 0 {
return Pattern{}, errors.New("the pattern does not have an equal number of collection ids and resource variables")
}
var p Pattern
for i := 0; i < len(splittedPattern); i += 2 {
p.collectionIds = append(p.collectionIds, splittedPattern[i])
p.resourceVars = append(p.resourceVars, strings.Trim(splittedPattern[i+1], "{}"))
}
return p, nil
}
func main() {
options := protogen.Options{}
options.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
version := "(unknown)"
bi, ok := debug.ReadBuildInfo()
if ok {
version = bi.Main.Version
}
generateFile(gen, f, version)
}
return nil
})
}
func parseResourcesFromDefinitions(file *protogen.File) []Resource {
resources := make([]Resource, 0)
options := file.Desc.Options()
if options != nil {
resourceDescriptors := proto.GetExtension(options, annotations.E_ResourceDefinition).([]*annotations.ResourceDescriptor)
for _, resourceDescriptor := range resourceDescriptors {
resource, err := parseResourceFromDescriptor(resourceDescriptor)
if err != nil {
// TODO: warning
continue
}
resources = append(resources, resource)
}
}
return resources
}
func parseResourcesFromMessages(file *protogen.File) []Resource {
resources := make([]Resource, 0)
for _, message := range file.Messages {
options := message.Desc.Options()
if options == nil {
continue
}
if proto.HasExtension(options, annotations.E_Resource) {
resourceDescriptor := proto.GetExtension(options, annotations.E_Resource).(*annotations.ResourceDescriptor)
resource, err := parseResourceFromDescriptor(resourceDescriptor)
if err != nil {
// TODO: warning
continue
}
resources = append(resources, resource)
}
}
return resources
}
func parseResourceFromDescriptor(resourceDescriptor *annotations.ResourceDescriptor) (Resource, error) {
if len(resourceDescriptor.Type) == 0 {
return Resource{}, fmt.Errorf("resource descriptor should have a type")
}
if len(resourceDescriptor.Pattern) == 0 {
return Resource{}, fmt.Errorf("resource descriptor should have at least 1 pattern")
}
resourceTypeSplitted := strings.Split(resourceDescriptor.Type, "/")
if len(resourceTypeSplitted) != 2 {
return Resource{}, fmt.Errorf("invalid resource type %s", resourceDescriptor.Type)
}
resource := Resource{
ServiceName: resourceTypeSplitted[0],
Type: resourceTypeSplitted[1],
}
for _, patternString := range resourceDescriptor.Pattern {
pattern, err := NewPattern(patternString)
if err != nil {
return Resource{}, fmt.Errorf("invalid pattern %s: %v", patternString, err)
}
resource.Patterns = append(resource.Patterns, pattern)
}
return resource, nil
}
func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}
func generateFile(plugin *protogen.Plugin, file *protogen.File, version string) {
definitionResources := parseResourcesFromDefinitions(file)
messageResources := parseResourcesFromMessages(file)
if len(definitionResources) == 0 && len(messageResources) == 0 {
return
}
g := plugin.NewGeneratedFile(file.GeneratedFilenamePrefix+extension, file.GoImportPath)
// Package statement
g.P("// Code generated by protoc-gen-go-rsn. DO NOT EDIT.")
g.P("// versions:")
g.P("// - protoc-gen-go-rsn ", version)
g.P("// - protoc ", protocVersion(plugin))
g.P()
g.P("package ", file.GoPackageName)
g.P()
// Generate code for each definition resource
for _, resource := range definitionResources {
generateResource(g, resource)
}
// Generate code for each message resource
for _, resource := range messageResources {
generateResource(g, resource)
}
}
func generateResource(g *protogen.GeneratedFile, resource Resource) {
g.P("// ", resource.parentTypeTypeAlias(), " indicates the possible parent types for resource '", resource.Type, "'.")
g.P("type ", resource.parentTypeTypeAlias(), " = string")
g.P()
g.P("const (")
for _, pattern := range resource.Patterns {
g.P("// ", pattern.parentTypeConst(), " is a possible parent type for resource '", resource.Type, "', used for '", pattern.parentTypeString(), "'.")
g.P(pattern.parentTypeConst(), " ", resource.parentTypeTypeAlias(), " = \"", pattern.parentTypeString(), "\"")
}
g.P(")")
g.P()
g.P("// ", resource.parentStructName(), " is the representation of a '", resource.Type, "' parent resource name.")
g.P("// This contains all possible identifiers for all parent types. The Type field implicitly declares the")
g.P("// identifiers used for that context.")
g.P("type ", resource.parentStructName(), " struct{")
for _, structField := range resource.parentStructFields() {
g.P(structField, " string")
}
g.P(typeStructField, " ", resource.parentTypeTypeAlias())
g.P("}")
g.P()
g.P("// ResourceName formats r to a resource name.")
g.P("func (r ", resource.parentStructName(), ") ResourceName() string {")
g.P("switch r.", typeStructField, " {")
for _, pattern := range resource.Patterns {
g.P("case ", pattern.parentTypeConst(), ":")
g.P("return ", g.QualifiedGoIdent(fmtPackage.Ident("Sprintf")), "(\"", pattern.parentFormatString(), "\", ", strings.Join(pattern.parentStructFields("r."), ", "), ")")
}
g.P("}")
g.P("return \"\"")
g.P("}")
g.P()
g.P("// IsZero reports whether r represents the zero resource name.")
g.P("func (r ", resource.parentStructName(), ") IsZero() bool {")
parentStructFields := resource.parentStructFields()
parentZeroChecks := make([]string, len(parentStructFields)+1)
for i, structField := range parentStructFields {
parentZeroChecks[i] = fmt.Sprintf("len(r.%s) == 0", structField)
}
parentZeroChecks[len(parentStructFields)] = fmt.Sprintf("len(r.%s) == 0", typeStructField)
g.P("return ", strings.Join(parentZeroChecks, " && "))
g.P("}")
g.P()
g.P("var (")
for _, pattern := range resource.Patterns {
g.P(pattern.parentRegexName(resource.Type), " = ", g.QualifiedGoIdent(regexpPackage.Ident("MustCompile")), "(`", pattern.parentRegexString(), "`)")
}
g.P(")")
g.P()
g.P("// Parse", resource.Type, "ParentResourceName parses the given parent resource name to a parent resource name.")
g.P("// Errors for unknown patterns.")
g.P("func Parse", resource.Type, "ParentResourceName(resourceName string) (", resource.parentStructName(), ", error) {")
for _, pattern := range resource.Patterns {
g.P("if match := ", pattern.parentRegexName(resource.Type), ".FindStringSubmatch(resourceName); match != nil {")
g.P("return ", resource.parentStructName(), "{")
for i, structField := range pattern.parentStructFields("") {
g.P(structField, ": match[", i+1, "],")
}
g.P(typeStructField, ": ", pattern.parentTypeConst(), ",")
g.P("}, nil")
g.P("}")
}
g.P("return ", resource.parentStructName(), "{}, ", g.QualifiedGoIdent(errorsPackage.Ident("New")), "(\"invalid parent resource name for resource type ", "'", resource.Type, "' in service '", resource.ServiceName, "'\")")
g.P("}")
g.P()
g.P("// ", resource.structName(), " is the representation of a '", resource.Type, "' resource name.")
g.P("type ", resource.structName(), " struct{")
g.P(parentStructField, " ", resource.parentStructName())
for _, structField := range resource.lastStructFields() {
g.P(structField, " string")
}
g.P("}")
g.P()
g.P("// ResourceName formats r to a resource name.")
g.P("func (r ", resource.structName(), ") ResourceName() string {")
g.P("switch r.", parentStructField, ".", typeStructField, " {")
for _, pattern := range resource.Patterns {
g.P("case ", pattern.parentTypeConst(), ":")
g.P("return ", g.QualifiedGoIdent(fmtPackage.Ident("Sprintf")), "(\"", pattern.formatString(), "\", ", strings.Join(append(pattern.parentStructFields("r."+parentStructField+"."), pattern.lastStructField("r.")), ", "), ")")
}
g.P("}")
g.P("return \"\"")
g.P("}")
g.P()
g.P("// IsZero reports whether r represents the zero resource name.")
g.P("func (r ", resource.structName(), ") IsZero() bool {")
lastStructFields := resource.lastStructFields()
resourceZeroChecks := make([]string, len(lastStructFields)+1)
resourceZeroChecks[0] = fmt.Sprintf("r.%s.IsZero()", parentStructField)
for i, structField := range lastStructFields {
resourceZeroChecks[i+1] = fmt.Sprintf("len(r.%s) == 0", structField)
}
g.P("return ", strings.Join(resourceZeroChecks, " && "))
g.P("}")
g.P()
g.P("var (")
for _, pattern := range resource.Patterns {
g.P(pattern.regexName(), " = ", g.QualifiedGoIdent(regexpPackage.Ident("MustCompile")), "(`", pattern.regexString(), "`)")
}
g.P(")")
g.P()
g.P("// Parse", resource.Type, "ResourceName parses the given resource name to a resource name.")
g.P("// Errors for unknown patterns.")
g.P("func Parse", resource.Type, "ResourceName(resourceName string) (", resource.structName(), ", error) {")
for _, pattern := range resource.Patterns {
g.P("if match := ", pattern.regexName(), ".FindStringSubmatch(resourceName); match != nil {")
g.P("return ", resource.structName(), "{")
g.P(parentStructField, ": ", resource.parentStructName(), "{")
for i, structField := range pattern.parentStructFields("") {
g.P(structField, ": match[", i+1, "],")
}
g.P(typeStructField, ": ", pattern.parentTypeConst(), ",")
g.P("},")
g.P(pattern.lastStructField(""), ": match[", len(pattern.parentStructFields(""))+1, "],")
g.P("}, nil")
g.P("}")
}
g.P("return ", resource.structName(), "{}, ", g.QualifiedGoIdent(errorsPackage.Ident("New")), "(\"invalid resource name for resource type ", "'", resource.Type, "' in service '", resource.ServiceName, "'\")")
g.P("}")
g.P()
}