-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
281 lines (247 loc) · 7.97 KB
/
schema.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
// Copyright 2018 The LUCI Authors.
//
// 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.
package main
import (
"bytes"
"fmt"
"sort"
"strings"
"unicode"
"cloud.google.com/go/bigquery"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/pmezard/go-difflib/difflib"
"go.chromium.org/luci/common/data/text/indented"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/proto/google/descutil"
)
// sourceCodeInfoMap maps descriptor proto messages to source code info,
// if available.
// See also descutil.IndexSourceCodeInfo.
type sourceCodeInfoMap map[interface{}]*descriptor.SourceCodeInfo_Location
type schemaConverter struct {
desc *descriptor.FileDescriptorSet
sourceCodeInfo map[*descriptor.FileDescriptorProto]sourceCodeInfoMap
}
// schema constructs a bigquery.Schema from a named message.
func (c *schemaConverter) schema(messageName string) (schema bigquery.Schema, description string, err error) {
file, obj, _ := descutil.Resolve(c.desc, messageName)
if obj == nil {
return nil, "", fmt.Errorf("message %q is not found", messageName)
}
msg, isMsg := obj.(*descriptor.DescriptorProto)
if !isMsg {
return nil, "", fmt.Errorf("expected %q to be a message, but it is %T", messageName, obj)
}
schema = make(bigquery.Schema, 0, len(msg.Field))
for _, field := range msg.Field {
switch s, err := c.field(file, field); {
case err != nil:
return nil, "", errors.Annotate(err, "failed to derive schema for field %q in message %q", field.GetName(), msg.GetName()).Err()
case s != nil:
schema = append(schema, s)
}
}
return schema, c.description(file, msg), nil
}
// field constructs bigquery.FieldSchema from proto field descriptor.
func (c *schemaConverter) field(file *descriptor.FileDescriptorProto, field *descriptor.FieldDescriptorProto) (*bigquery.FieldSchema, error) {
schema := &bigquery.FieldSchema{
Name: field.GetName(),
Description: c.description(file, field),
Repeated: descutil.Repeated(field),
Required: descutil.Required(field),
}
typeName := strings.TrimPrefix(field.GetTypeName(), ".")
switch field.GetType() {
case
descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT:
schema.Type = bigquery.FloatFieldType
case
descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_FIXED64,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SINT64:
schema.Type = bigquery.IntegerFieldType
case descriptor.FieldDescriptorProto_TYPE_BOOL:
schema.Type = bigquery.BooleanFieldType
case descriptor.FieldDescriptorProto_TYPE_STRING:
schema.Type = bigquery.StringFieldType
case descriptor.FieldDescriptorProto_TYPE_BYTES:
schema.Type = bigquery.BytesFieldType
case descriptor.FieldDescriptorProto_TYPE_ENUM:
schema.Type = bigquery.StringFieldType
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
switch typeName {
case "google.protobuf.Duration":
schema.Type = bigquery.FloatFieldType
case "google.protobuf.Timestamp":
schema.Type = bigquery.TimestampFieldType
case "google.protobuf.Struct":
// google.protobuf.Struct is persisted as JSONPB string.
// See also https://bit.ly/chromium-bq-struct
schema.Type = bigquery.StringFieldType
default:
switch s, _, err := c.schema(typeName); {
case err != nil:
return nil, err
case len(s) == 0:
// BigQuery does not like empty record fields.
return nil, nil
default:
schema.Type = bigquery.RecordFieldType
schema.Schema = s
}
}
default:
return nil, fmt.Errorf("not supported field type %q", field.GetType())
}
return schema, nil
}
// description returns a string description of the descriptor proto that
// ptr points to.
// If ptr is a field of an enum type, appends
// "\nValid values: <comma-separated enum member names>".
func (c *schemaConverter) description(file *descriptor.FileDescriptorProto, ptr interface{}) string {
description := c.sourceCodeInfo[file][ptr].GetLeadingComments()
// Trim leading whitespace.
lines := strings.Split(description, "\n")
trimSize := -1
for _, l := range lines {
if len(strings.TrimSpace(l)) == 0 {
// skip empty lines
continue
}
space := 0
for _, r := range l {
if unicode.IsSpace(r) {
space++
} else {
break
}
}
if trimSize == -1 || space < trimSize {
trimSize = space
}
}
if trimSize > 0 {
for i := range lines {
if len(lines[i]) >= trimSize {
lines[i] = lines[i][trimSize:]
}
}
description = strings.Join(lines, "\n")
}
description = strings.TrimSpace(description)
// Append valid enum values.
if field, ok := ptr.(*descriptor.FieldDescriptorProto); ok && field.GetType() == descriptor.FieldDescriptorProto_TYPE_ENUM {
_, obj, _ := descutil.Resolve(c.desc, strings.TrimPrefix(field.GetTypeName(), "."))
if enum, ok := obj.(*descriptor.EnumDescriptorProto); ok {
names := make([]string, len(enum.Value))
for i, v := range enum.Value {
names[i] = v.GetName()
}
if description != "" {
description += "\n"
}
description += fmt.Sprintf("Valid values: %s.", strings.Join(names, ", "))
}
}
return description
}
func printSchema(w *indented.Writer, s bigquery.Schema) {
// Field order does not matter.
// A new field is always added to the end of the field list in a live table.
// Sort fields by name to make the result deterministic.
// Schema diffing relies on it.
s = append(bigquery.Schema(nil), s...)
sort.Slice(s, func(i, j int) bool {
return s[i].Name < s[j].Name
})
for i, f := range s {
if i > 0 {
fmt.Fprintln(w)
}
if f.Description != "" {
for _, line := range strings.Split(f.Description, "\n") {
fmt.Fprintln(w, "//", line)
}
}
switch {
case f.Repeated:
fmt.Fprint(w, "repeated ")
case f.Required:
fmt.Fprint(w, "required ")
}
fmt.Fprintf(w, "%s %s", f.Type, f.Name)
if f.Type == bigquery.RecordFieldType {
fmt.Fprintln(w, " {")
w.Level++
printSchema(w, f.Schema)
w.Level--
fmt.Fprint(w, "}")
}
fmt.Fprintln(w)
}
}
func schemaString(s bigquery.Schema) string {
var buf bytes.Buffer
printSchema(&indented.Writer{Writer: &buf}, s)
return buf.String()
}
// schemaDiff returns unified diff of two schemas.
// Returns "" if there is no difference.
func schemaDiff(before, after bigquery.Schema) string {
ret, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(schemaString(before)),
B: difflib.SplitLines(schemaString(after)),
FromFile: "Current",
ToFile: "New",
Context: 3,
Eol: "\n",
})
if err != nil {
// GetUnifiedDiffString returns an error only if it fails
// to write to a bytes.Buffer, which either cannot happen or we better
// panic.
panic(err)
}
return ret
}
// addMissingFields copies fields from src to dest if they are not present in
// dest.
func addMissingFields(dest *bigquery.Schema, src bigquery.Schema) {
destFields := indexFields(*dest)
for _, sf := range src {
switch df := destFields[sf.Name]; {
case df == nil:
*dest = append(*dest, sf)
default:
addMissingFields(&df.Schema, sf.Schema)
}
}
}
func indexFields(s bigquery.Schema) map[string]*bigquery.FieldSchema {
ret := make(map[string]*bigquery.FieldSchema, len(s))
for _, f := range s {
ret[f.Name] = f
}
return ret
}