forked from GoogleCloudPlatform/protoc-gen-bq-schema
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
616 lines (530 loc) · 16.9 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// protoc plugin which converts .proto to schema for BigQuery.
// It is spawned by protoc and generates schema for BigQuery, encoded in JSON.
//
// usage:
// $ bin/protoc --bq-schema_out=path/to/outdir foo.proto
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"github.com/chuhlomin/protoc-gen-bq-schema/protos"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
)
var (
globalPkg = &ProtoPackage{
name: "",
parent: nil,
children: make(map[string]*ProtoPackage),
types: make(map[string]*descriptor.DescriptorProto),
comments: make(map[string]Comments),
path: make(map[string]string),
}
)
// Field describes the schema of a field in BigQuery.
type Field struct {
Name string `json:"name"`
Type string `json:"type"`
Mode string `json:"mode"`
Description string `json:"description,omitempty"`
Fields []*Field `json:"fields,omitempty"`
}
// ProtoPackage describes a package of Protobuf, which is an container of message types.
type ProtoPackage struct {
name string
parent *ProtoPackage
children map[string]*ProtoPackage
types map[string]*descriptor.DescriptorProto
comments map[string]Comments
path map[string]string
}
func registerType(pkgName *string, msg *descriptor.DescriptorProto, comments Comments, path string) {
pkg := globalPkg
if pkgName != nil {
for _, node := range strings.Split(*pkgName, ".") {
if pkg == globalPkg && node == "" {
// Skips leading "."
continue
}
child, ok := pkg.children[node]
if !ok {
child = &ProtoPackage{
name: pkg.name + "." + node,
parent: pkg,
children: make(map[string]*ProtoPackage),
types: make(map[string]*descriptor.DescriptorProto),
comments: make(map[string]Comments),
path: make(map[string]string),
}
pkg.children[node] = child
}
pkg = child
}
}
pkg.types[msg.GetName()] = msg
pkg.comments[msg.GetName()] = comments
pkg.path[msg.GetName()] = path
}
func (pkg *ProtoPackage) lookupType(name string) (*descriptor.DescriptorProto, bool, Comments, string) {
if strings.HasPrefix(name, ".") {
return globalPkg.relativelyLookupType(name[1:])
}
for ; pkg != nil; pkg = pkg.parent {
if desc, ok, comments, path := pkg.relativelyLookupType(name); ok {
return desc, ok, comments, path
}
}
return nil, false, Comments{}, ""
}
func relativelyLookupNestedType(desc *descriptor.DescriptorProto, name string) (*descriptor.DescriptorProto, bool, string) {
components := strings.Split(name, ".")
path := ""
componentLoop:
for _, component := range components {
for nestedIndex, nested := range desc.GetNestedType() {
if nested.GetName() == component {
desc = nested
path = fmt.Sprintf("%s.%d.%d", path, subMessagePath, nestedIndex)
continue componentLoop
}
}
glog.Infof("no such nested message %s in %s", component, desc.GetName())
return nil, false, ""
}
return desc, true, strings.Trim(path, ".")
}
func (pkg *ProtoPackage) relativelyLookupType(name string) (*descriptor.DescriptorProto, bool, Comments, string) {
components := strings.SplitN(name, ".", 2)
switch len(components) {
case 0:
glog.V(1).Info("empty message name")
return nil, false, Comments{}, ""
case 1:
found, ok := pkg.types[components[0]]
return found, ok, pkg.comments[components[0]], pkg.path[components[0]]
case 2:
glog.Infof("looking for %s in %s at %s (%v)", components[1], components[0], pkg.name, pkg)
if child, ok := pkg.children[components[0]]; ok {
found, ok, comments, path := child.relativelyLookupType(components[1])
return found, ok, comments, path
}
if msg, ok := pkg.types[components[0]]; ok {
found, ok, path := relativelyLookupNestedType(msg, components[1])
return found, ok, pkg.comments[components[0]], pkg.path[components[0]] + "." + path
}
glog.V(1).Infof("no such package nor message %s in %s", components[0], pkg.name)
return nil, false, Comments{}, ""
default:
glog.Fatal("not reached")
return nil, false, Comments{}, ""
}
}
func (pkg *ProtoPackage) relativelyLookupPackage(name string) (*ProtoPackage, bool) {
components := strings.Split(name, ".")
for _, c := range components {
var ok bool
pkg, ok = pkg.children[c]
if !ok {
return nil, false
}
}
return pkg, true
}
var (
typeFromWKT = map[string]string{
".google.protobuf.Int32Value": "INTEGER",
".google.protobuf.Int64Value": "INTEGER",
".google.protobuf.UInt32Value": "INTEGER",
".google.protobuf.UInt64Value": "INTEGER",
".google.protobuf.DoubleValue": "FLOAT",
".google.protobuf.FloatValue": "FLOAT",
".google.protobuf.BoolValue": "BOOLEAN",
".google.protobuf.StringValue": "STRING",
".google.protobuf.BytesValue": "BYTES",
".google.protobuf.Duration": "STRING",
".google.protobuf.Timestamp": "TIMESTAMP",
}
typeFromFieldType = map[descriptor.FieldDescriptorProto_Type]string{
descriptor.FieldDescriptorProto_TYPE_DOUBLE: "FLOAT",
descriptor.FieldDescriptorProto_TYPE_FLOAT: "FLOAT",
descriptor.FieldDescriptorProto_TYPE_INT64: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_UINT64: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_INT32: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_UINT32: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_FIXED64: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_FIXED32: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_SFIXED32: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_SFIXED64: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_SINT32: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_SINT64: "INTEGER",
descriptor.FieldDescriptorProto_TYPE_STRING: "STRING",
descriptor.FieldDescriptorProto_TYPE_BYTES: "BYTES",
descriptor.FieldDescriptorProto_TYPE_ENUM: "STRING",
descriptor.FieldDescriptorProto_TYPE_BOOL: "BOOLEAN",
descriptor.FieldDescriptorProto_TYPE_GROUP: "RECORD",
descriptor.FieldDescriptorProto_TYPE_MESSAGE: "RECORD",
}
modeFromFieldLabel = map[descriptor.FieldDescriptorProto_Label]string{
descriptor.FieldDescriptorProto_LABEL_OPTIONAL: "NULLABLE",
descriptor.FieldDescriptorProto_LABEL_REQUIRED: "REQUIRED",
descriptor.FieldDescriptorProto_LABEL_REPEATED: "REPEATED",
}
)
func convertField(
curPkg *ProtoPackage,
desc *descriptor.FieldDescriptorProto,
msgOpts *protos.BigQueryMessageOptions,
parentMessages map[*descriptor.DescriptorProto]bool,
comments Comments,
path string) (*Field, error) {
field := &Field{
Name: desc.GetName(),
}
if msgOpts.GetUseJsonNames() && desc.GetJsonName() != "" {
field.Name = desc.GetJsonName()
}
var ok bool
field.Mode, ok = modeFromFieldLabel[desc.GetLabel()]
if !ok {
return nil, fmt.Errorf("unrecognized field label: %s", desc.GetLabel().String())
}
field.Type, ok = typeFromFieldType[desc.GetType()]
if !ok {
return nil, fmt.Errorf("unrecognized field type: %s", desc.GetType().String())
}
if comment := comments.Get(path); comment != "" {
field.Description = comment
}
opts := desc.GetOptions()
if opts != nil && proto.HasExtension(opts, protos.E_Bigquery) {
rawOpt, err := proto.GetExtension(opts, protos.E_Bigquery)
if err != nil {
return nil, err
}
opt := *rawOpt.(*protos.BigQueryFieldOptions)
if opt.Ignore {
// skip the field below
return nil, nil
}
if opt.Require {
field.Mode = "REQUIRED"
}
if len(opt.TypeOverride) > 0 {
field.Type = opt.TypeOverride
}
if len(opt.Name) > 0 {
field.Name = opt.Name
}
if len(opt.Description) > 0 {
field.Description = opt.Description
}
}
if field.Type != "RECORD" {
return field, nil
}
if t, ok := typeFromWKT[desc.GetTypeName()]; ok {
field.Type = t
return field, nil
}
fields, err := convertFieldsForType(curPkg, desc.GetTypeName(), parentMessages)
if err != nil {
return nil, err
}
if len(fields) == 0 { // discard RECORDs that would have zero fields
return nil, nil
}
field.Fields = fields
return field, nil
}
func convertExtraField(
curPkg *ProtoPackage,
extraFieldDefinition string,
parentMessages map[*descriptor.DescriptorProto]bool,
comments Comments,
path string) (*Field, error) {
parts := strings.Split(extraFieldDefinition, ":")
if len(parts) < 2 {
return nil, fmt.Errorf("expecting at least 2 parts in extra field definition divided by colon, got %d", len(parts))
}
field := &Field{
Name: parts[0],
Type: parts[1],
Mode: "NULLABLE",
}
modeIndex := 2
if field.Type == "RECORD" {
modeIndex = 3
}
if len(parts) > modeIndex {
field.Mode = parts[modeIndex]
}
if field.Type != "RECORD" {
return field, nil
}
if len(parts) < 3 {
return nil, fmt.Errorf("extra field %s has no type defined", field.Type)
}
typeName := parts[2]
if t, ok := typeFromWKT[typeName]; ok {
field.Type = t
return field, nil
}
fields, err := convertFieldsForType(curPkg, typeName, parentMessages)
if err != nil {
return nil, err
}
if len(fields) == 0 { // discard RECORDs that would have zero fields
return nil, nil
}
field.Fields = fields
return field, nil
}
func convertFieldsForType(
curPkg *ProtoPackage,
typeName string,
parentMessages map[*descriptor.DescriptorProto]bool) ([]*Field, error) {
recordType, ok, comments, path := curPkg.lookupType(typeName)
if !ok {
return nil, fmt.Errorf("no such message type named %s", typeName)
}
fieldMsgOpts, err := getBigqueryMessageOptions(recordType)
if err != nil {
return nil, err
}
return convertMessageType(curPkg, recordType, fieldMsgOpts, parentMessages, comments, path)
}
func convertMessageType(
curPkg *ProtoPackage,
msg *descriptor.DescriptorProto,
opts *protos.BigQueryMessageOptions,
parentMessages map[*descriptor.DescriptorProto]bool,
comments Comments,
path string) (schema []*Field, err error) {
if parentMessages[msg] {
glog.Infof("Detected recursion for message %s, ignoring subfields", *msg.Name)
return
}
if glog.V(4) {
glog.Info("Converting message: ", proto.MarshalTextString(msg))
}
parentMessages[msg] = true
for fieldIndex, fieldDesc := range msg.GetField() {
fieldCommentPath := fmt.Sprintf("%s.%d.%d", path, fieldPath, fieldIndex)
field, err := convertField(curPkg, fieldDesc, opts, parentMessages, comments, fieldCommentPath)
if err != nil {
glog.Errorf("Failed to convert field %s in %s: %v", fieldDesc.GetName(), msg.GetName(), err)
return nil, err
}
// if we got no error and the field is nil, skip it
if field != nil {
schema = append(schema, field)
}
}
for _, extraField := range opts.GetExtraFields() {
field, err := convertExtraField(curPkg, extraField, parentMessages, comments, path)
if err != nil {
glog.Errorf("Failed to convert extra field %s in %s: %v", extraField, msg.GetName(), err)
return nil, err
}
schema = append(schema, field)
}
parentMessages[msg] = false
return
}
// NB: This is what the extension for tag 1021 used to look like. For some
// level of backwards compatibility, we will try to parse the extension using
// this definition if we get an error trying to parse it as the current
// definition (a message, to support multiple extension fields therein).
var e_TableName = &proto.ExtensionDesc{
ExtendedType: (*descriptor.MessageOptions)(nil),
ExtensionType: (*string)(nil),
Field: 1021,
Name: "gen_bq_schema.table_name",
Tag: "bytes,1021,opt,name=table_name,json=tableName",
Filename: "bq_table.proto",
}
func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorResponse_File, error) {
name := path.Base(file.GetName())
pkg := globalPkg
if file.Package != nil {
var ok bool
if pkg, ok = globalPkg.relativelyLookupPackage(file.GetPackage()); !ok {
return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
}
}
comments := ParseComments(file)
response := []*plugin.CodeGeneratorResponse_File{}
for msgIndex, msg := range file.GetMessageType() {
path := fmt.Sprintf("%d.%d", messagePath, msgIndex)
opts, err := getBigqueryMessageOptions(msg)
if err != nil {
return nil, err
}
if opts == nil {
continue
}
tableName := opts.GetTableName()
if len(tableName) == 0 {
continue
}
glog.V(2).Info("Generating schema for a message type ", msg.GetName())
schema, err := convertMessageType(pkg, msg, opts, make(map[*descriptor.DescriptorProto]bool), comments, path)
if err != nil {
glog.Errorf("Failed to convert %s: %v", name, err)
return nil, err
}
jsonSchema, err := json.MarshalIndent(schema, "", " ")
if err != nil {
glog.Error("Failed to encode schema", err)
return nil, err
}
filename := tableName + ".schema"
if file.Package != nil {
filename = strings.Replace(file.GetPackage(), ".", "/", -1) + "/" + filename
}
resFile := &plugin.CodeGeneratorResponse_File{
Name: proto.String(filename),
Content: proto.String(string(jsonSchema)),
}
response = append(response, resFile)
}
return response, nil
}
// getBigqueryMessageOptions returns the bigquery options for the given message.
// If an error is encountered, it is returned instead. If no error occurs, but
// the message has no gen_bq_schema.bigquery_opts option, this function returns
// nil, nil.
func getBigqueryMessageOptions(msg *descriptor.DescriptorProto) (*protos.BigQueryMessageOptions, error) {
options := msg.GetOptions()
if options == nil {
return nil, nil
}
if !proto.HasExtension(options, protos.E_BigqueryOpts) {
return nil, nil
}
optionValue, err := proto.GetExtension(options, protos.E_BigqueryOpts)
if err == nil {
return optionValue.(*protos.BigQueryMessageOptions), nil
}
// try to decode the extension using old definition before failing
optionValue, newErr := proto.GetExtension(options, e_TableName)
if newErr != nil {
return nil, err // return original error
}
// translate this old definition to the expected message type
name := *optionValue.(*string)
return &protos.BigQueryMessageOptions{
TableName: name,
}, nil
}
func convert(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error) {
generateTargets := make(map[string]bool)
for _, file := range req.GetFileToGenerate() {
generateTargets[file] = true
}
res := &plugin.CodeGeneratorResponse{}
for _, file := range req.GetProtoFile() {
for msgIndex, msg := range file.GetMessageType() {
glog.V(1).Infof("Loading a message type %s from package %s", msg.GetName(), file.GetPackage())
registerType(file.Package, msg, ParseComments(file), fmt.Sprintf("%d.%d", messagePath, msgIndex))
}
}
for _, file := range req.GetProtoFile() {
if _, ok := generateTargets[file.GetName()]; ok {
glog.V(1).Info("Converting ", file.GetName())
converted, err := convertFile(file)
if err != nil {
res.Error = proto.String(fmt.Sprintf("Failed to convert %s: %v", file.GetName(), err))
return res, err
}
res.File = append(res.File, converted...)
}
}
return res, nil
}
func convertFrom(rd io.Reader) (*plugin.CodeGeneratorResponse, error) {
glog.V(1).Info("Reading code generation request")
input, err := ioutil.ReadAll(rd)
if err != nil {
glog.Error("Failed to read request:", err)
return nil, err
}
req := &plugin.CodeGeneratorRequest{}
err = proto.Unmarshal(input, req)
if err != nil {
glog.Error("Can't unmarshal input:", err)
return nil, err
}
commandLineParameters(req.GetParameter())
glog.V(1).Info("Converting input")
return convert(req)
}
func commandLineParameters(parameter string) {
param := make(map[string]string)
for _, p := range strings.Split(parameter, ",") {
if i := strings.Index(p, "="); i < 0 {
param[p] = ""
} else {
param[p[0:i]] = p[i+1:]
}
}
for k, v := range param {
switch k {
case "enumsasints":
if v == "true" {
typeFromFieldType[descriptor.FieldDescriptorProto_TYPE_ENUM] = "INTEGER"
}
}
}
}
func main() {
flag.Parse()
ok := true
glog.Info("Processing code generator request")
res, err := convertFrom(os.Stdin)
if err != nil {
ok = false
if res == nil {
message := fmt.Sprintf("Failed to read input: %v", err)
res = &plugin.CodeGeneratorResponse{
Error: &message,
}
}
}
glog.Info("Serializing code generator response")
data, err := proto.Marshal(res)
if err != nil {
glog.Fatal("Cannot marshal response", err)
}
_, err = os.Stdout.Write(data)
if err != nil {
glog.Fatal("Failed to write response", err)
}
if ok {
glog.Info("Succeeded to process code generator request")
} else {
glog.Info("Failed to process code generator but successfully sent the error to protoc")
}
}