-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
rich_model.go
237 lines (192 loc) · 6.18 KB
/
rich_model.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
package main
import (
"flag"
"fmt"
"log"
"strings"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
v1 "github.com/shortlink-org/shortlink/pkg/protoc/protoc-gen-rich-model/options/v1"
)
const (
version = "1.3.0"
)
var (
filter = flag.String("filter", "", "Specify the filter type for generating the rich model")
filterMap = map[string]struct{}{}
)
func main() {
log.Println("protoc-rich-model version:", version, "is called")
flag.Parse()
// The main function runs the plugin.
protogen.Options{
ParamFunc: flag.CommandLine.Set,
}.Run(func(gen *protogen.Plugin) error {
log.Println("Running with protoc version:", protocVersion(gen))
// Convert the filter string into a map for quick lookup
if *filter != "" {
for _, name := range strings.Split(*filter, ";") {
filterMap[name] = struct{}{}
}
}
for _, f := range gen.Files {
if !f.Generate {
continue
}
for _, message := range f.Messages {
generateRichModel(gen, f, message) // Generate rich model for each message
}
}
return 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 generateRichModel(gen *protogen.Plugin, file *protogen.File, message *protogen.Message) {
// Filter by the specified type
if *filter != "" {
if _, ok := filterMap[message.GoIdent.GoName]; !ok {
return
}
}
// Adjust the filename to follow the '<typeName>.ddd.go' pattern
filename := fmt.Sprintf("%s_ddd.go", strings.ToLower(message.GoIdent.GoName))
g := gen.NewGeneratedFile(filename, file.GoImportPath)
printHeader(gen, g, file)
importManager := newImportManager()
// Preprocess to collect necessary imports
for _, field := range message.Fields {
if field.GoName == "" {
continue
}
_, usedImports := protobufToGoType(field)
importManager.addImports(usedImports)
}
importManager.writeImports(g)
// Add comment for the message if available
if message.Comments.Leading != "" {
g.P(strings.TrimSpace(message.Comments.Leading.String()))
}
// Generate a rich model struct
structName := strings.ToUpper(message.GoIdent.GoName[:1]) + message.GoIdent.GoName[1:] // Capitalize the first letter
g.P("type ", structName, " struct {")
for _, field := range message.Fields {
if field.GoName == "" {
continue
}
goType, usedImports := protobufToGoType(field)
if goType == "" {
continue
}
importManager.addImports(usedImports)
fieldName := strings.ToLower(field.GoName)
// Add comment for the field if available
if field.Comments.Leading != "" {
g.P(strings.TrimSpace(field.Comments.Leading.String()))
}
g.P(fieldName, " ", goType)
}
g.P("}")
// Generate getters for each field
generateGetters(g, message, structName)
}
// Generate getters for each field in the message
func generateGetters(g *protogen.GeneratedFile, message *protogen.Message, structName string) {
for _, field := range message.Fields {
if field.GoName == "" {
continue
}
goType, _ := protobufToGoType(field)
if goType == "" {
continue
}
fieldName := strings.ToLower(field.GoName)
getterName := "Get" + field.GoName // Capitalize the first letter for the public method
g.P()
g.P("// ", getterName, " returns the value of the ", fieldName, " field.")
g.P("func (m *", structName, ") ", getterName, "() ", goType, " {")
g.P("return m.", fieldName)
g.P("}")
}
}
// importManager manages the imports for the generated file
func protobufToGoType(field *protogen.Field) (string, map[string]bool) {
if field.Desc.IsList() {
innerType, innerImports := protobufToGoTypeSingle(field)
if innerType != "" {
return "[]" + innerType, innerImports
}
return "", nil
}
return protobufToGoTypeSingle(field)
}
// protobufToGoTypeSingle converts a protobuf field to a Go type
func protobufToGoTypeSingle(field *protogen.Field) (string, map[string]bool) {
imports := make(map[string]bool)
if opts := field.Desc.Options(); opts != nil {
if proto.HasExtension(opts, v1.E_GoType) {
customType, ok := proto.GetExtension(opts, v1.E_GoType).(string)
if !ok {
log.Println("invalid GoType extension for field:", field.GoName)
return "", nil
}
// Determine if imports are needed for the custom type and add them.
if strings.Contains(customType, "url.URL") {
imports["net/url"] = true
}
return customType, imports
}
}
switch field.Desc.Kind() {
case protoreflect.BoolKind:
return "bool", nil
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind:
return "int", nil
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind:
return "int64", nil
case protoreflect.StringKind:
return "string", nil
case protoreflect.BytesKind:
return "[]byte", nil
case protoreflect.EnumKind:
return "int32", nil // Enums are represented as int32 in Go
case protoreflect.FloatKind:
return "float32", nil
case protoreflect.DoubleKind:
return "float64", nil
case protoreflect.MessageKind, protoreflect.GroupKind:
if field.Message != nil && field.Message.GoIdent.GoImportPath != "" {
if field.Message.GoIdent.GoName == "Timestamp" {
imports["google.golang.org/protobuf/types/known/timestamppb"] = true
return "*timestamppb.Timestamp", imports
} else if field.Message.GoIdent.GoName == "FieldMask" {
imports["google.golang.org/protobuf/types/known/fieldmaskpb"] = true
return "fieldmaskpb.FieldMask", imports
}
}
return "*" + strings.ToUpper(field.Message.GoIdent.GoName[:1]) + field.Message.GoIdent.GoName[1:], nil // Pointer to the type, but lowercase
default:
return "", nil
}
}
// printHeader prints the header of the generated file
func printHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, file *protogen.File) {
g.P("// Code generated by protoc-gen-rich-model. DO NOT EDIT.")
g.P("// versions:")
g.P("// - protoc-gen-rich-model v" + version)
g.P("// - protoc ", protocVersion(gen))
g.P("// source: ", file.Desc.Path())
g.P()
g.P("package ", file.GoPackageName)
g.P()
}