-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_parsing.go
398 lines (330 loc) · 13 KB
/
option_parsing.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
package main
import (
"fmt"
"github.com/pseudomuto/protokit"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"math"
)
// CustomOptions stores all custom options defined by proto files being compiled
type CustomOptions struct {
FileOptions map[int32]*CustomOptionDef `json:"file_options"`
MessageOptions map[int32]*CustomOptionDef `json:"message_options"`
FieldOptions map[int32]*CustomOptionDef `json:"field_options"`
EnumOptions map[int32]*CustomOptionDef `json:"enum_options"`
EnumValueOptions map[int32]*CustomOptionDef `json:"enum_value_options"`
ServiceOptions map[int32]*CustomOptionDef `json:"service_options"`
MethodOptions map[int32]*CustomOptionDef `json:"method_options"`
}
// CustomOptionDef is a custom option defined by a proto file
type CustomOptionDef struct {
Index int32
Name string
FullName string
Type descriptorpb.FieldDescriptorProto_Type
TypeName string
}
func NewCustomOptions() *CustomOptions {
return &CustomOptions{
FileOptions: make(map[int32]*CustomOptionDef),
MessageOptions: make(map[int32]*CustomOptionDef),
FieldOptions: make(map[int32]*CustomOptionDef),
EnumOptions: make(map[int32]*CustomOptionDef),
EnumValueOptions: make(map[int32]*CustomOptionDef),
ServiceOptions: make(map[int32]*CustomOptionDef),
MethodOptions: make(map[int32]*CustomOptionDef),
}
}
// parseAllCustomOptionDefinitions finds all custom options defined by any of `files` and returns
// a struct containing them
func parseAllCustomOptionDefinitions(files []*protokit.FileDescriptor) *CustomOptions {
ret := NewCustomOptions()
//Loop through all extensions defined by all files
for _, file := range files {
for _, ext := range file.GetExtensions() {
// To add support for message, file, etc. options, add a case statement here
switch ext.GetExtendee() {
case ".google.protobuf.FileOptions":
ret.FileOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.MessageOptions":
ret.MessageOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.FieldOptions":
ret.FieldOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.EnumOptions":
ret.EnumOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.EnumValueOptions":
ret.EnumValueOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.ServiceOptions":
ret.ServiceOptions[ext.GetNumber()] = parseCustomOption(ext)
case ".google.protobuf.MethodOptions":
ret.MethodOptions[ext.GetNumber()] = parseCustomOption(ext)
}
}
}
return ret
}
// parseAllCustomOptionValues parses all the values of the custom options set on files/messages/fields/services/etc.
// and updates `context` with the parsed values
func parseAllCustomOptionValues(context *Context) {
for _, file := range context.Files {
file.Options = parseFileOptions(file.Descriptor, context)
}
for _, message := range context.Messages {
message.Options = parseMessageOptions(message.Descriptor, context)
}
for _, field := range context.Fields {
field.Options = parseFieldOptions(field.Descriptor, context)
}
for _, enum := range context.Enums {
enum.Options = parseEnumOptions(enum.Descriptor, context)
}
for _, enumVal := range context.EnumValues {
enumVal.Options = parseEnumValueOptions(enumVal.Descriptor, context)
}
for _, service := range context.Services {
service.Options = parseServiceOptions(service.Descriptor, context)
}
for _, method := range context.Methods {
method.Options = parseMethodOptions(method.Descriptor, context)
}
}
// parseCustomOption parses a custom option
func parseCustomOption(ext *protokit.ExtensionDescriptor) *CustomOptionDef {
ret := &CustomOptionDef{Index: ext.GetNumber(), Name: ext.GetName(), Type: ext.GetType(), TypeName: ext.GetTypeName()}
// Lil' hack to get the FQN of the option
ret.FullName = ext.GetPackage() + "." + ext.GetName()
// `ext.GetTypeName()` will return an empty string if `Type` is a Message
if len(ret.TypeName) == 0 {
ret.TypeName = ret.Type.String()
}
return ret
}
func parseRawOptions(entityName string, raw protoreflect.RawFields, optionsDB *map[int32]*CustomOptionDef) map[string]interface{} {
ret := make(map[string]interface{})
size := len(raw)
consumed := 0
if size <= 0 {
return nil
}
// Handle each option
for consumed < size {
// Read tag
optionIndex, wireType, length := protowire.ConsumeTag(raw[consumed:])
if length < 0 {
fmt.Printf("FAILED PARSING OPTIONS FOR ENTITY %s: %v", entityName, raw)
return nil
}
consumed += length
// Find option
optionDef, found := (*optionsDB)[int32(optionIndex)]
if !found {
continue
}
var opt interface{}
uintVal := uint64(0)
bytesVal := make([]byte, 0)
valLen := 0
// First, we need to decode value based on its WIRE type
switch wireType {
case protowire.VarintType:
uintVal, valLen = protowire.ConsumeVarint(raw[consumed:])
case protowire.Fixed32Type:
val, i32ValLen := protowire.ConsumeFixed32(raw[consumed:])
uintVal = uint64(val)
valLen = i32ValLen
case protowire.Fixed64Type:
uintVal, valLen = protowire.ConsumeFixed64(raw[consumed:])
case protowire.BytesType:
bytesVal, valLen = protowire.ConsumeBytes(raw[consumed:])
case protowire.StartGroupType:
bytesVal, valLen = protowire.ConsumeGroup(optionIndex, raw[consumed:])
case protowire.EndGroupType:
// Should not get here
default:
}
consumed += valLen
//Next, we need to treat the decoded value differently based on the type it's declared as in the proto file.
//This is because, for example, `sint32` and `int32` are both encoded as varints, but `sint32` is zigzag-encoded.
switch optionDef.Type {
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
opt = math.Float64frombits(uintVal)
case descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
// TODO: proper float parsing
opt = math.Float32frombits(uint32(uintVal))
case descriptorpb.FieldDescriptorProto_TYPE_INT64,
descriptorpb.FieldDescriptorProto_TYPE_INT32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED64,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED32:
// TODO: proper int parsing
opt = int64(uintVal)
case descriptorpb.FieldDescriptorProto_TYPE_UINT64,
descriptorpb.FieldDescriptorProto_TYPE_UINT32,
descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
descriptorpb.FieldDescriptorProto_TYPE_FIXED32:
opt = uintVal
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
// No ternary expression in Go? Gross!
if uintVal == 0 {
opt = false
} else {
opt = true
}
case descriptorpb.FieldDescriptorProto_TYPE_STRING:
opt = string(bytesVal)
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
opt = "TODO: PARSE GROUPS"
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
// TODO: PARSE MESSAGES
opt = optionDef.TypeName
case descriptorpb.FieldDescriptorProto_TYPE_BYTES:
opt = bytesVal
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
opt = struct {
EnumType string `json:"enum_type"`
EnumVal uint64 `json:"enum_value"`
}{
StripStartingPeriod(optionDef.TypeName),
uintVal,
}
case descriptorpb.FieldDescriptorProto_TYPE_SINT32,
descriptorpb.FieldDescriptorProto_TYPE_SINT64:
opt = protowire.DecodeZigZag(uintVal)
}
ret[optionDef.FullName] = opt
}
return ret
}
/**
*
* Here be the consequence of Golang's type system
*
* May you look upon this and despair
*
*/
// parseFileOptions parses options on a file,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseFileOptions(file *protokit.FileDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := file.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(file.GetName(), raw, &context.CustomOptions.FileOptions) {
ret[k] = v
}
return ret
}
// parseMessageOptions parses options on a message,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseMessageOptions(message *protokit.Descriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := message.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(message.GetFullName(), raw, &context.CustomOptions.MessageOptions) {
ret[k] = v
}
return ret
}
// parseFieldOptions parses options on a field,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseFieldOptions(field *protokit.FieldDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := field.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(field.GetFullName(), raw, &context.CustomOptions.FieldOptions) {
ret[k] = v
}
return ret
}
// parseEnumOptions parses options on an enum,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseEnumOptions(enum *protokit.EnumDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := enum.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(enum.GetFullName(), raw, &context.CustomOptions.EnumOptions) {
ret[k] = v
}
return ret
}
// parseEnumValueOptions parses options on an enum value,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseEnumValueOptions(enumVal *protokit.EnumValueDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := enumVal.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(enumVal.GetFullName(), raw, &context.CustomOptions.EnumValueOptions) {
ret[k] = v
}
return ret
}
// parseServiceOptions parses options on a service,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseServiceOptions(service *protokit.ServiceDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := service.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(service.GetFullName(), raw, &context.CustomOptions.ServiceOptions) {
ret[k] = v
}
return ret
}
// parseMethodOptions parses options on a method,
// mapping them to CustomOptions which were discovered during `parseAllCustomOptionDefinitions`
func parseMethodOptions(method *protokit.MethodDescriptor, context *Context) map[string]interface{} {
ret := make(map[string]interface{})
options := method.GetOptions()
// Handle some common pre-defined (non-custom) options
if options.GetDeprecated() {
ret["deprecated"] = true
}
// End handling pre-defined options
//`options` is a protobuf message -- custom options will be in its unknown fields
raw := options.ProtoReflect().GetUnknown()
// Parse the options from the raw bytes of the message and store them in ret
for k, v := range parseRawOptions(method.GetFullName(), raw, &context.CustomOptions.MethodOptions) {
ret[k] = v
}
return ret
}