-
Notifications
You must be signed in to change notification settings - Fork 38
/
finder.go
361 lines (331 loc) · 10.7 KB
/
finder.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
// Copyright (c) 2020 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"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"strings"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
)
// findEntities finds all entities in the given file paths. An error is
// returned if there are naming collisions, otherwise, return a slice of
// warnings (or nil).
func findEntities(paths, excludes []string) ([]*Table, []error, error) {
var entities []*Table
var warnings []error
for _, path := range paths {
fileSet := token.NewFileSet()
packages, err := parser.ParseDir(fileSet, path, func(fileInfo os.FileInfo) bool {
if len(excludes) == 0 {
return true
}
for _, exclude := range excludes {
if matched, _ := filepath.Match(exclude, fileInfo.Name()); matched {
return false
}
}
return true
}, 0)
if err != nil {
return nil, nil, err
}
erv := new(entityRecordingVisitor)
for _, pkg := range packages { // go through all the packages
for _, file := range pkg.Files { // go through all the files
packagePrefix, hasDosa := findDosaPackage(file)
//if erv.PackageName != "" { // skip packages that don't import 'dosa'
if hasDosa {
erv.packagePrefix = packagePrefix
for _, decl := range file.Decls { // go through all the declarations
ast.Walk(erv, decl)
}
}
}
}
entities = append(entities, erv.entities...)
warnings = append(warnings, erv.warnings...)
}
return entities, warnings, nil
}
// FindEntityByName returns the entity with given name in the path.
func FindEntityByName(path string, structName string) (*Table, error) {
// find all entites in the given path
entities, _, err := findEntities([]string{path}, []string{})
if err != nil {
return nil, err
}
// filter entites with the given struct name
entitiesFilteredByName := []*Table{}
for _, table := range entities {
if table.StructName == structName {
entitiesFilteredByName = append(entitiesFilteredByName, table)
}
}
// only one entity with given name should be found
if len(entitiesFilteredByName) == 0 {
return nil, errors.Errorf("no entity named %s found in the path %s", structName, path)
}
if len(entitiesFilteredByName) > 1 {
return nil, errors.Errorf("more than one entities named %s found in the path %s", structName, path)
}
return entitiesFilteredByName[0], nil
}
// dosaPackageName is the name of the dosa package, fully qualified and quoted
const dosaPackageName = `"github.com/uber-go/dosa"`
func findDosaPackage(file *ast.File) (string, bool) {
// look for the case where we import dosa
for _, impspec := range file.Imports {
if impspec.Path.Value == dosaPackageName {
// impspec.Name is nil when not renamed,
// so we use the default "dosa"
if impspec.Name == nil {
return "dosa", true
}
// renamed case
return impspec.Name.Name, true
}
}
if file.Name.Name == "dosa" {
// special case: our package is 'dosa' so no prefix is required
return "", true
}
// this file doesn't have any references to dosa, so skip it
return "", false
}
// entityRecordingVisitor is a visitor that records entities it finds
// It also keeps track of all failed entities that pass the basic "looks like a DOSA object" test
// (see isDosaEntity to understand that test)
type entityRecordingVisitor struct {
entities []*Table
warnings []error
packagePrefix string
}
// Visit records all the entities seen into the entityRecordingVisitor structure
func (f *entityRecordingVisitor) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.File, *ast.Package, *ast.BlockStmt, *ast.DeclStmt, *ast.FuncDecl, *ast.GenDecl:
return f
case *ast.TypeSpec:
if structType, ok := n.Type.(*ast.StructType); ok {
// look for a Entity with a dosa annotation
if isDosaEntity(structType) {
table, err := tableFromStructType(n.Name.Name, structType, f.packagePrefix)
if err == nil {
f.entities = append(f.entities, table)
} else {
f.warnings = append(f.warnings, err)
}
}
}
}
return nil
}
// isDosaEntity is a sanity check so that only objects that are probably supposed to be dosa
// annotated objects will generate warnings. The rules for that are:
// - must have some fields
// - the first field should be of type Entity
// TODO: Really any field could be type Entity, but we currently do not have this case
func isDosaEntity(structType *ast.StructType) bool {
// structures with no fields cannot be dosa entities
if len(structType.Fields.List) < 1 {
return false
}
// the first field should be a DOSA Entity type
candidateEntityField := structType.Fields.List[0]
if identifier, ok := candidateEntityField.Type.(*ast.Ident); ok {
if identifier.Name != entityName {
return false
}
}
// and should have a DOSA tag
if candidateEntityField.Tag == nil || candidateEntityField.Tag.Kind != token.STRING {
return false
}
entityTag := reflect.StructTag(strings.Trim(candidateEntityField.Tag.Value, "`"))
if entityTag.Get(dosaTagKey) == "" {
return false
}
return true
}
func parseASTType(expr ast.Expr) (string, error) {
var kind string
var err error
switch typeName := expr.(type) {
case *ast.Ident:
kind = typeName.Name
// not an Entity type, perhaps another primitive type
case *ast.ArrayType:
// only dosa allowed array type is []byte
if typeName, ok := typeName.Elt.(*ast.Ident); ok {
if typeName.Name == "byte" {
kind = "[]byte"
}
}
case *ast.SelectorExpr:
// only dosa allowed selector is time.Time
if innerName, ok := typeName.X.(*ast.Ident); ok {
kind = innerName.Name + "." + typeName.Sel.Name
}
case *ast.StarExpr:
// pointer types
// need to recursively parse the type
kind, err = parseASTType(typeName.X)
kind = "*" + kind
default:
err = fmt.Errorf("Unexpected field type: %v", typeName)
}
return kind, err
}
// tableFromStructType takes an ast StructType and converts it into a Table object
func tableFromStructType(structName string, structType *ast.StructType, packagePrefix string) (*Table, error) {
normalizedName, err := NormalizeName(structName)
if err != nil {
// TODO: This isn't correct, someone could override the name later
return nil, errors.Wrapf(err, "struct name is invalid")
}
t := &Table{
StructName: structName,
EntityDefinition: EntityDefinition{
Name: normalizedName,
Columns: []*ColumnDefinition{},
Indexes: map[string]*IndexDefinition{},
},
ColToField: map[string]string{},
FieldToCol: map[string]string{},
}
for _, field := range structType.Fields.List {
var dosaTag string
if field.Tag != nil {
entityTag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
dosaTag = strings.TrimSpace(entityTag.Get(dosaTagKey))
}
if dosaTag == "-" { // skip explicitly ignored fields
continue
}
kind, err := parseASTType(field.Type)
if err != nil {
return nil, err
}
if kind == packagePrefix+"."+entityName || (packagePrefix == "" && kind == entityName) {
var err error
if t.EntityDefinition.Name, t.TTL, t.ETL, t.Key, err = parseEntityTag(structName, dosaTag); err != nil {
return nil, err
}
} else {
for _, fieldName := range field.Names {
name := fieldName.Name
if kind == packagePrefix+"."+indexName || (packagePrefix == "" && kind == indexName) {
indexName, indexKey, indexColumns, err := parseIndexTag(name, dosaTag)
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 {
firstRune, _ := utf8.DecodeRuneInString(name)
if unicode.IsLower(firstRune) {
// skip unexported fields
continue
}
typ, isPointer := stringToDosaType(kind, packagePrefix)
if typ == Invalid {
return nil, fmt.Errorf("Column %q has invalid type %q", name, kind)
}
cd, err := parseField(typ, isPointer, name, dosaTag)
if err != nil {
return nil, errors.Wrapf(err, "column %q", name)
}
t.Columns = append(t.Columns, cd)
t.ColToField[cd.Name] = name
t.FieldToCol[name] = cd.Name
}
}
if len(field.Names) == 0 {
if kind == packagePrefix+"."+indexName || (packagePrefix == "" && kind == indexName) {
indexName, indexKey, indexColumns, err := parseIndexTag("", dosaTag)
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}
}
}
}
}
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
}
func stringToDosaType(inType, pkg string) (Type, bool) {
// Append a dot if the package suffix doesn't already have one.
if pkg != "" && !strings.HasSuffix(pkg, ".") {
pkg += "."
}
switch inType {
case "string":
return String, false
case "[]byte":
return Blob, false
case "bool":
return Bool, false
case "int32":
return Int32, false
case "int64":
return Int64, false
case "float64":
return Double, false
case "time.Time":
return Timestamp, false
case "UUID", pkg + "UUID":
return TUUID, false
case "*string":
return String, true
case "*bool":
return Bool, true
case "*int32":
return Int32, true
case "*int64":
return Int64, true
case "*float64":
return Double, true
case "*time.Time":
return Timestamp, true
case "*UUID", "*" + pkg + "UUID":
return TUUID, true
default:
return Invalid, false
}
}