forked from uber-go/dosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_parser.go
583 lines (510 loc) · 16.3 KB
/
entity_parser.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dosa
import (
"fmt"
"reflect"
"regexp"
"strings"
"time"
"unicode"
"github.com/pkg/errors"
)
const (
entityName = "Entity"
indexName = "Index"
dosaTagKey = "dosa"
asc = "asc"
desc = "desc"
)
var (
primaryKeyPattern0 = regexp.MustCompile(`primaryKey\s*=\s*([^=]*)((\s+.*=)|$)`)
primaryKeyPattern1 = regexp.MustCompile(`\(\s*\((.*)\)(.*)\)`)
primaryKeyPattern2 = regexp.MustCompile(`\(\s*([^,\s]+),?(.*)\)`)
primaryKeyPattern3 = regexp.MustCompile(`^\s*([^(),\s]+)\s*$`)
indexKeyPattern0 = regexp.MustCompile(`key\s*=\s*([^=]*)((\s+.*=)|$)`)
namePattern0 = regexp.MustCompile(`name\s*=\s*(\S*)`)
columnsPattern = regexp.MustCompile(`columns\s*=\s*\(([^\(\)]+)\)`)
etlPattern0 = regexp.MustCompile(`etl\s*=\s*(\S*)`)
ttlPattern0 = regexp.MustCompile(`ttl\s*=\s*(\S*)`)
indexType = reflect.TypeOf((*Index)(nil)).Elem()
)
// parseClusteringKeys func parses the clustering key of DOSA object
func parseClusteringKeys(ckStr string) ([]*ClusteringKey, error) {
ckStr = strings.TrimSpace(ckStr)
cks := strings.Split(ckStr, ",")
var clusteringKeys []*ClusteringKey
for _, ck := range cks {
fields := strings.Fields(ck)
if len(fields) == 0 {
continue
}
if len(fields) > 2 {
return nil, fmt.Errorf("Clustering key definition %q should look like \"name[ asc/desc]\"",
ck)
}
descending := false
if len(fields) == 2 {
switch strings.ToLower(fields[1]) {
case desc:
descending = true
case asc:
descending = false
default:
return nil, fmt.Errorf("invalid clustering key order %q in %q", fields[1], ck)
}
}
clusteringKeys = append(clusteringKeys, &ClusteringKey{Name: strings.TrimSpace(fields[0]), Descending: descending})
}
return clusteringKeys, nil
}
// parsePartitionKey func parses the partition key of DOSA object
func parsePartitionKey(pkStr string) []string {
pkStr = strings.TrimSpace(pkStr)
var pks []string
partitionKeys := strings.Split(pkStr, ",")
for _, pk := range partitionKeys {
npk := strings.TrimSpace(pk)
if len(pk) > 0 {
pks = append(pks, npk)
}
}
return pks
}
// parsePrimaryKey func parses the primary key of DOSA object
func parsePrimaryKey(tableName, pkStr string) (*PrimaryKey, error) {
// parens must be matched
if !parensBalanced(pkStr) {
return nil, fmt.Errorf("unmatched parentheses: %q", pkStr)
}
// filter out "trailing comma and space"
pkStr = strings.TrimRight(pkStr, ", ")
pkStr = strings.TrimSpace(pkStr)
var partitionKeyStr string
var clusteringKeyStr string
matched := false
// case 1: primaryKey=((PK1,PK2), PK3, PK4)
matchs := primaryKeyPattern1.FindStringSubmatch(pkStr)
if len(matchs) == 3 {
matched = true
partitionKeyStr = matchs[1]
clusteringKeyStr = matchs[2]
}
// case 2: primaryKey=(PK1,PK2)
if !matched {
matchs = primaryKeyPattern2.FindStringSubmatch(pkStr)
if len(matchs) == 3 {
matched = true
partitionKeyStr = matchs[1]
clusteringKeyStr = matchs[2]
}
}
// case 3: primaryKey=PK1 (only one primary key)
if !matched {
matchs = primaryKeyPattern3.FindStringSubmatch(pkStr)
if len(matchs) == 2 {
matched = true
partitionKeyStr = matchs[1]
clusteringKeyStr = ""
}
}
if !matched {
return nil, fmt.Errorf("invalid primary key: %s", pkStr)
}
partitionKeys := parsePartitionKey(partitionKeyStr)
clusteringKeys, err := parseClusteringKeys(clusteringKeyStr)
if err != nil {
return nil, errors.Wrapf(err, "invalid primary key: %s", pkStr)
}
// TODO optimize this , this is too slow
// search for duplicates
everything := partitionKeys
for _, ck := range clusteringKeys {
everything = append(everything, ck.Name)
}
seen := map[string]bool{}
for v := range everything {
if _, ok := seen[everything[v]]; ok {
return nil, fmt.Errorf("Object %q has duplicate field %q in key struct tag", tableName, everything[v])
}
seen[everything[v]] = true
}
return &PrimaryKey{
PartitionKeys: partitionKeys,
ClusteringKeys: clusteringKeys,
}, nil
}
// TableFromInstance creates a dosa.Table from a dosa.DomainObject instance.
// Note: this method is not cheap as it does a lot of reflection to build the
// Table instances. It is recommended to only be called once and cache results.
func TableFromInstance(object DomainObject) (*Table, error) {
elem := reflect.TypeOf(object).Elem()
t := &Table{
StructName: elem.Name(),
ColToField: map[string]string{},
FieldToCol: map[string]string{},
EntityDefinition: EntityDefinition{
Columns: []*ColumnDefinition{},
Indexes: map[string]*IndexDefinition{},
},
}
for i := 0; i < elem.NumField(); i++ {
structField := elem.Field(i)
if len(structField.PkgPath) > 0 { // skip unexported fields
continue
}
tag := strings.TrimSpace(structField.Tag.Get(dosaTagKey))
if tag == "-" { // skip explicitly ignored fields
continue
}
name := structField.Name
if name == entityName {
var err error
if t.EntityDefinition.Name, t.TTL, t.ETL, t.Key, err = parseEntityTag(t.StructName, tag); err != nil {
return nil, err
}
} else {
// parse index fields
if structField.Type == indexType {
indexName, indexKey, indexColumns, err := parseIndexTag(structField.Name, tag)
if err != nil {
return nil, err
}
if _, exist := t.Indexes[indexName]; exist {
return nil, errors.Errorf("index name is duplicated: %s", indexName)
}
t.Indexes[indexName] = &IndexDefinition{Key: indexKey, Columns: indexColumns}
} else {
cd, err := parseFieldTag(structField, tag)
if err != nil {
return nil, errors.Wrapf(err, "column %q had invalid type", name)
}
t.Columns = append(t.Columns, cd)
t.ColToField[cd.Name] = name
t.FieldToCol[name] = cd.Name
}
}
}
if t.Key == nil {
return nil, errors.Errorf("cannot find dosa.Entity in object %s", t.StructName)
}
translateKeyName(t)
if err := t.EnsureValid(); err != nil {
return nil, errors.Wrap(err, "failed to parse dosa object")
}
return t, nil
}
// translateKeyName translate the primary keys to the internal column name based on the mapping
// between fields and columns.
func translateKeyName(t *Table) {
pk := t.EntityDefinition.Key
for i := range pk.PartitionKeys {
name := pk.PartitionKeys[i]
if v, ok := t.FieldToCol[name]; ok {
pk.PartitionKeys[i] = v
}
}
for i := range pk.ClusteringKeys {
name := pk.ClusteringKeys[i].Name
if v, ok := t.FieldToCol[name]; ok {
pk.ClusteringKeys[i].Name = v
}
}
for _, index := range t.Indexes {
pk := index.Key
for i := range pk.PartitionKeys {
name := pk.PartitionKeys[i]
if v, ok := t.FieldToCol[name]; ok {
pk.PartitionKeys[i] = v
}
}
for i := range pk.ClusteringKeys {
name := pk.ClusteringKeys[i].Name
if v, ok := t.FieldToCol[name]; ok {
pk.ClusteringKeys[i].Name = v
}
}
}
}
// parseIndexTag functions parses DOSA index tag
func parseIndexTag(indexName, dosaAnnotation string) (string, *PrimaryKey, []string, error) {
// index name struct must be exported in the entity,
// otherwise it will be ignored when upserting the schema.
if len(indexName) != 0 && unicode.IsLower([]rune(indexName)[0]) {
expected := []rune(indexName)
expected[0] = unicode.ToUpper(expected[0])
return "", nil, nil, fmt.Errorf("index name (%s) must be exported, "+
"try (%s) instead", indexName, string(expected))
}
tag := dosaAnnotation
// find the primaryKey
matchs := indexKeyPattern0.FindStringSubmatch(tag)
if len(matchs) != 4 {
return "", nil, nil, fmt.Errorf("dosa.Index %s with an invalid dosa index tag %q", indexName, tag)
}
pkString := matchs[1]
key, err := parsePrimaryKey(indexName, pkString)
if err != nil {
return "", nil, nil, errors.Wrapf(err, "struct %s has an invalid index key %q", indexName, pkString)
}
toRemove := strings.TrimSuffix(matchs[0], matchs[2])
toRemove = strings.TrimSuffix(matchs[0], matchs[3])
tag = strings.Replace(tag, toRemove, "", 1)
//find the name
fullNameTag, name, err := parseNameTag(tag, indexName)
if err != nil {
return "", nil, nil, errors.Wrapf(err, "invalid name tag: %s", tag)
}
tag = strings.Replace(tag, fullNameTag, "", 1)
// find the columns
fullColumnsTag, indexColumns, err := parseColumnsTag(tag)
if err != nil {
return "", nil, nil, errors.Wrapf(err, "invalid columns tag: %s", tag)
}
tag = strings.Replace(tag, fullColumnsTag, "", 1)
tag = strings.TrimSpace(tag)
if tag != "" {
return "", nil, nil, fmt.Errorf("index field %s with an invalid dosa index tag: %s", indexName, tag)
}
return name, key, indexColumns, nil
}
// parseNameTag functions parses DOSA "name" tag
func parseNameTag(tag, defaultName string) (string, string, error) {
fullNameTag := ""
name := defaultName
matches := namePattern0.FindStringSubmatch(tag)
if len(matches) == 2 {
fullNameTag = matches[0]
name = matches[1]
}
// filter out "trailing comma"
name = strings.TrimRight(name, " ,")
var err error
name, err = NormalizeName(name)
if err != nil {
return "", "", err
}
return fullNameTag, name, nil
}
// parseColumnsTag parses the "columns" tag of a dosa.Index in the entity. It returns
// the matched section of the tag string and a list of the selected fields.
func parseColumnsTag(tag string) (string, []string, error) {
fullColumnsTag := ""
var indexColumns []string
var fields []string
matches := columnsPattern.FindStringSubmatch(tag)
if len(matches) == 2 {
fullColumnsTag = matches[0]
fields = strings.Split(matches[1], ",")
}
for _, field := range fields {
field = strings.TrimSpace(field)
if field != "" {
field, err := NormalizeName(strings.TrimSpace(field))
if err != nil {
return "", nil, err
}
indexColumns = append(indexColumns, field)
}
}
return fullColumnsTag, indexColumns, nil
}
// parseETLTag functions parses DOSA "etl" tag
func parseETLTag(tag string) (string, ETLState, error) {
fullETLTag := ""
etlTag := ""
matches := etlPattern0.FindStringSubmatch(tag)
if len(matches) == 2 {
fullETLTag = matches[0]
etlTag = matches[1]
}
if len(matches) == 0 {
return "", EtlOff, nil
}
// filter out "trailing comma"
etlTag = strings.TrimRight(etlTag, " ,")
var err error
etlTag, err = NormalizeName(etlTag)
if err != nil {
return "", EtlOff, err
}
etlState, err := ToETLState(etlTag)
if err != nil {
return "", EtlOff, err
}
return fullETLTag, etlState, nil
}
// parseTTLTag functions parses DOSA "ttl" tag
func parseTTLTag(tag string) (string, time.Duration, error) {
fullTTLTag := ""
ttlTag := ""
matches := ttlPattern0.FindStringSubmatch(tag)
if len(matches) == 0 {
return "", NoTTL(), nil
}
if len(matches) == 2 {
fullTTLTag = matches[0]
ttlTag = matches[1]
}
// filter out "trailing comma"
ttlTag = strings.TrimRight(ttlTag, " ,")
ttl, err := time.ParseDuration(ttlTag)
if err != nil {
return "", NoTTL(), err
}
if err = ValidateTTL(ttl); err != nil {
return "", NoTTL(), err
}
return fullTTLTag, ttl, nil
}
// parseEntityTag function parses DOSA tag on the "Entity" field
func parseEntityTag(structName, dosaAnnotation string) (string, time.Duration, ETLState, *PrimaryKey, error) {
tag := dosaAnnotation
// find the primaryKey
matchs := primaryKeyPattern0.FindStringSubmatch(tag)
if len(matchs) != primaryKeyPattern0.NumSubexp()+1 {
return "", NoTTL(), EtlOff, nil, fmt.Errorf("dosa.Entity on object %s with an invalid dosa struct tag %q", structName, tag)
}
pkString := matchs[1]
key, err := parsePrimaryKey(structName, pkString)
if err != nil {
return "", NoTTL(), EtlOff, nil, errors.Wrapf(err, "struct %s has an invalid primary key %q", structName, pkString)
}
toRemove := strings.TrimSuffix(matchs[0], matchs[2])
toRemove = strings.TrimSuffix(matchs[0], matchs[3])
tag = strings.Replace(tag, toRemove, "", 1)
// find the name
fullNameTag, name, err := parseNameTag(tag, structName)
if err != nil {
return "", NoTTL(), EtlOff, nil, errors.Wrapf(err, "invalid name tag: %s", tag)
}
tag = strings.Replace(tag, fullNameTag, "", 1)
// find the ETL flag
fullETLTag, etlState, err := parseETLTag(tag)
if err != nil {
return "", NoTTL(), EtlOff, nil, errors.Wrapf(err, "invalid etl tag: %s", tag)
}
tag = strings.Replace(tag, fullETLTag, "", 1)
// find the ttl flag
fullTTLTag, ttl, err := parseTTLTag(tag)
if err != nil {
return "", NoTTL(), EtlOff, nil, errors.Wrapf(err, "invalid ttl tag: %s", tag)
}
tag = strings.Replace(tag, fullTTLTag, "", 1)
tag = strings.TrimSpace(tag)
if tag != "" {
return "", NoTTL(), EtlOff, nil, fmt.Errorf("struct %s with an invalid dosa struct tag: %s", structName, tag)
}
return name, ttl, etlState, key, nil
}
// parseFieldTag function parses DOSA tag on the fields in the DOSA struct except the "Entity" field
func parseFieldTag(structField reflect.StructField, dosaAnnotation string) (*ColumnDefinition, error) {
typ, isPointer, err := typify(structField.Type)
if err != nil {
return nil, err
}
return parseField(typ, isPointer, structField.Name, dosaAnnotation)
}
func parseField(typ Type, isPointer bool, name string, tag string) (*ColumnDefinition, error) {
// parse name tag
fullNameTag, name, err := parseNameTag(tag, name)
if err != nil {
return nil, fmt.Errorf("invalid name tag: %s", tag)
}
tag = strings.Replace(tag, fullNameTag, "", 1)
if strings.TrimSpace(tag) != "" {
return nil, fmt.Errorf("field %s with an invalid dosa field tag: %s", name, tag)
}
return &ColumnDefinition{Name: name, IsPointer: isPointer, Type: typ}, nil
}
func parensBalanced(s string) bool {
// This is effectively pushing left parens on the stack, and popping them when
// a right paren is seen. Since the stack only ever contains the same character,
// we don't actually need the stack -- only its size.
var ssize uint
for i := 0; i < len(s); i++ {
if s[i] == '(' {
ssize++
} else if s[i] == ')' {
if ssize == 0 {
// Extra right paren
return false
}
ssize--
}
}
// Stack must be empty
return ssize == 0
}
var (
uuidType = reflect.TypeOf(UUID(""))
blobType = reflect.TypeOf([]byte{})
timestampType = reflect.TypeOf(time.Time{})
int32Type = reflect.TypeOf(int32(0))
int64Type = reflect.TypeOf(int64(0))
doubleType = reflect.TypeOf(float64(0.0))
stringType = reflect.TypeOf("")
boolType = reflect.TypeOf(true)
nullBoolType = reflect.TypeOf((*bool)(nil))
nullInt32Type = reflect.TypeOf((*int32)(nil))
nullInt64Type = reflect.TypeOf((*int64)(nil))
nullDoubleType = reflect.TypeOf((*float64)(nil))
nullStringType = reflect.TypeOf((*string)(nil))
nullUUIDType = reflect.TypeOf((*UUID)(nil))
nullTimeType = reflect.TypeOf((*time.Time)(nil))
)
func typify(f reflect.Type) (Type, bool, error) {
switch f {
case uuidType:
return TUUID, false, nil
case blobType:
return Blob, false, nil
case timestampType:
return Timestamp, false, nil
case int32Type:
return Int32, false, nil
case int64Type:
return Int64, false, nil
case doubleType:
return Double, false, nil
case stringType:
return String, false, nil
case boolType:
return Bool, false, nil
case nullUUIDType:
return TUUID, true, nil
case nullTimeType:
return Timestamp, true, nil
case nullInt32Type:
return Int32, true, nil
case nullInt64Type:
return Int64, true, nil
case nullDoubleType:
return Double, true, nil
case nullStringType:
return String, true, nil
case nullBoolType:
return Bool, true, nil
}
return Invalid, true, fmt.Errorf("Invalid type %v", f)
}
func (d Table) String() string {
return d.Name + " " + d.Key.String()
}