-
Notifications
You must be signed in to change notification settings - Fork 3
/
ast.go
604 lines (510 loc) · 15.5 KB
/
ast.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
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/tools/aah source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/scanner"
"go/token"
"os"
"path/filepath"
"strings"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
)
var (
buildImportCache = map[string]string{}
// Reference: https://golang.org/pkg/builtin/
builtInDataTypes = map[string]bool{
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int16": true,
"int32": true,
"int64": true,
"int8": true,
"rune": true,
"string": true,
"uint": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uint8": true,
"uintptr": true,
}
)
type (
// Program holds all details loaded from the Go source code for given Path.
program struct {
Path string
Packages []*packageInfo
RegisteredActions map[string]map[string]uint8
}
// PackageInfo holds the single paackge information.
packageInfo struct {
Fset *token.FileSet
Pkg *ast.Package
Types map[string]*typeInfo
ImportPath string
FilePath string
Files []string
}
// TypeInfo holds the information about Controller Name, Methods,
// Embedded types etc.
typeInfo struct {
Name string
ImportPath string
Methods []*methodInfo
EmbeddedTypes []*typeInfo
}
// MethodInfo holds the information of single method and it's Parameters.
methodInfo struct {
Name string
StructName string
Parameters []*parameterInfo
}
// ParameterInfo holds the information of single Parameter in the method.
parameterInfo struct {
Name string
ImportPath string
Type *typeExpr
}
// TypeExpr holds the information of single parameter data type.
typeExpr struct {
Expr string
IsBuiltIn bool
PackageName string
ImportPath string
PackageIndex uint8
Valid bool
}
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//___________________________________
// LoadProgram method loads the Go source code for the given directory.
func loadProgram(path string, excludes ess.Excludes, registeredActions map[string]map[string]uint8) (*program, []error) {
if err := validateInput(path); err != nil {
return nil, append([]error{}, err)
}
prg := &program{
Path: path,
Packages: []*packageInfo{},
RegisteredActions: registeredActions,
}
var (
pkgs map[string]*ast.Package
errs []error
)
err := ess.Walk(path, func(srcPath string, info os.FileInfo, err error) error {
if err != nil {
errs = append(errs, err)
}
// Excludes
if excludes.Match(filepath.Base(srcPath)) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if !info.IsDir() {
return nil
}
if info.IsDir() && ess.IsDirEmpty(srcPath) {
// skip directory if it's empty
return filepath.SkipDir
}
pfset := token.NewFileSet()
pkgs, err = parser.ParseDir(pfset, srcPath, func(f os.FileInfo) bool {
return !f.IsDir() && !excludes.Match(f.Name())
}, 0)
if err != nil {
if errList, ok := err.(scanner.ErrorList); ok {
// TODO parsing error list
fmt.Println(errList)
}
errs = append(errs, fmt.Errorf("error parsing dir[%s]: %s", srcPath, err))
return nil
}
pkg, err := validateAndGetPkg(pkgs, srcPath)
if err != nil {
errs = append(errs, err)
return nil
}
if pkg != nil {
pkg.Fset = pfset
pkg.FilePath = srcPath
pkg.ImportPath = stripGoPath(srcPath)
prg.Packages = append(prg.Packages, pkg)
}
return nil
})
if err != nil {
errs = append(errs, err)
}
return prg, errs
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Program methods
//___________________________________
// Process method processes all packages in the program for `Type`,
// `Embedded Type`, `Method`, etc.
func (prg *program) Process() {
for _, pkgInfo := range prg.Packages {
pkgInfo.Types = map[string]*typeInfo{}
// Each source file
for name, file := range pkgInfo.Pkg.Files {
pkgInfo.Files = append(pkgInfo.Files, filepath.Base(name))
var fileImports map[string]string
// collecting imports
for _, decl := range file.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if isImportTok(genDecl) {
fileImports = pkgInfo.processImports(genDecl)
}
}
}
// collecting types
for _, decl := range file.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if isTypeTok(genDecl) {
pkgInfo.processTypes(genDecl, fileImports)
}
}
}
// collecting methods
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
findMethods(pkgInfo, prg.RegisteredActions, funcDecl, fileImports)
}
}
}
}
}
// FindTypeByEmbeddedType method returns all the typeInfo that has directly or
// indirectly embedded by given type name. Type name must be fully qualified
// type name. E.g.: aahframework.org/aah.Controller
func (prg *program) FindTypeByEmbeddedType(qualifiedTypeName string) []*typeInfo {
var (
queue = []string{qualifiedTypeName}
processed []string
result []*typeInfo
)
for len(queue) > 0 {
typeName := queue[0]
queue = queue[1:]
processed = append(processed, typeName)
// search within all packages in the program
for _, p := range prg.Packages {
// search within all struct type in the package
for _, t := range p.Types {
// If this one has been processed or is already in queue, then move on.
if ess.IsSliceContainsString(processed, t.FullyQualifiedName()) ||
ess.IsSliceContainsString(queue, t.FullyQualifiedName()) {
continue
}
// search through the embedded types to see if the current type is among them.
for _, et := range t.EmbeddedTypes {
// If so, add this type's FullyQualifiedName into queue,
// and it's typeInfo into result.
if typeName == et.FullyQualifiedName() {
queue = append(queue, t.FullyQualifiedName())
result = append(result, t)
break
}
}
}
}
}
return result
}
// CreateImportPaths method returns unique package alias with import path.
func (prg *program) CreateImportPaths(types []*typeInfo) map[string]string {
importPaths := map[string]string{}
for _, t := range types {
importPath := filepath.ToSlash(t.ImportPath)
if _, found := importPaths[importPath]; !found {
cnt := 0
pkgAlias := t.PackageName()
for isPkgAliasExists(importPaths, pkgAlias) {
pkgAlias = fmt.Sprintf("%s%d", t.PackageName(), cnt)
cnt++
}
importPaths[importPath] = pkgAlias
}
}
return importPaths
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// PackageInfo methods
//___________________________________
// Name method return package name
func (p *packageInfo) Name() string {
return filepath.Base(p.ImportPath)
}
func (p *packageInfo) processTypes(decl *ast.GenDecl, imports map[string]string) {
spec := decl.Specs[0].(*ast.TypeSpec)
typeName := spec.Name.Name
ty := &typeInfo{
Name: typeName,
ImportPath: filepath.ToSlash(p.ImportPath),
Methods: []*methodInfo{},
EmbeddedTypes: []*typeInfo{},
}
// struct type
st, ok := spec.Type.(*ast.StructType)
if ok {
// finding embedded type(s) and it's package
for _, field := range st.Fields.List {
// If field.Names is set, it's not an embedded type.
if field.Names != nil && len(field.Names) > 0 {
continue
}
fPkgName, fTypeName := parseStructFieldExpr(field.Type)
if ess.IsStrEmpty(fTypeName) {
continue
}
// Find the import path for embedded type. If it was referenced without
// a package name, use the current package import path otherwise
// get the import path by package name.
var eTypeImportPath string
if ess.IsStrEmpty(fPkgName) {
eTypeImportPath = ty.ImportPath
} else {
var found bool
if eTypeImportPath, found = imports[fPkgName]; !found {
log.Errorf("AST: Unable to find import path for %s.%s", fPkgName, fTypeName)
continue
}
}
ty.EmbeddedTypes = append(ty.EmbeddedTypes, &typeInfo{Name: fTypeName, ImportPath: eTypeImportPath})
}
}
p.Types[typeName] = ty
}
func (p *packageInfo) processImports(decl *ast.GenDecl) map[string]string {
imports := map[string]string{}
for _, dspec := range decl.Specs {
spec := dspec.(*ast.ImportSpec)
var pkgAlias string
if spec.Name != nil {
if spec.Name.Name == "_" {
continue
}
pkgAlias = spec.Name.Name
}
importPath := spec.Path.Value[1 : len(spec.Path.Value)-1]
if ess.IsStrEmpty(pkgAlias) {
if alias, found := buildImportCache[importPath]; found {
pkgAlias = alias
} else { // build cache
pkg, err := build.Import(importPath, p.FilePath, 0)
if err != nil {
log.Errorf("AST: Unable to find import path: %s", importPath)
continue
}
pkgAlias = pkg.Name
buildImportCache[importPath] = pkg.Name
}
}
imports[pkgAlias] = importPath
}
return imports
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// TypeInfo methods
//___________________________________
// FullyQualifiedName method returns the fully qualified type name.
func (t *typeInfo) FullyQualifiedName() string {
return fmt.Sprintf("%s.%s", t.ImportPath, t.Name)
}
// PackageName method returns types package name from import path.
func (t *typeInfo) PackageName() string {
return filepath.Base(t.ImportPath)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// TypeExpr methods
//___________________________________
// Name method returns type name for expression.
func (te *typeExpr) Name() string {
if te.IsBuiltIn || ess.IsStrEmpty(te.PackageName) {
return te.Expr
}
return fmt.Sprintf("%s%s.%s", te.Expr[:te.PackageIndex], te.PackageName, te.Expr[te.PackageIndex:])
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
func validateInput(path string) error {
if ess.IsStrEmpty(path) {
return errors.New("path is required input")
}
if !ess.IsFileExists(path) {
return fmt.Errorf("path is does not exists: %s", path)
}
return nil
}
func validateAndGetPkg(pkgs map[string]*ast.Package, path string) (*packageInfo, error) {
pkgCnt := len(pkgs)
// no source code found in the directory
if pkgCnt == 0 {
return nil, nil
}
// not permitted by Go lang spec
if pkgCnt > 1 {
var names []string
for k := range pkgs {
names = append(names, k)
}
return nil, fmt.Errorf("more than one package name [%s] found in single"+
" directory: %s", strings.Join(names, ", "), path)
}
pkg := &packageInfo{}
for _, v := range pkgs {
pkg.Pkg = v
}
return pkg, nil
}
func isImportTok(decl *ast.GenDecl) bool {
return token.IMPORT == decl.Tok
}
func isTypeTok(decl *ast.GenDecl) bool {
return token.TYPE == decl.Tok
}
func stripGoPath(pkgFilePath string) string {
idx := strings.Index(pkgFilePath, "src")
return filepath.Clean(pkgFilePath[idx+4:])
}
func isPkgAliasExists(importPaths map[string]string, pkgAlias string) bool {
_, found := importPaths[pkgAlias]
return found
}
func findMethods(pkg *packageInfo, routeMethods map[string]map[string]uint8, fn *ast.FuncDecl, imports map[string]string) {
// do not process if -
// 1. does not have receiver (only methods)
// 2. method is not exported/public
// 3. method returns result
if fn.Recv == nil || !fn.Name.IsExported() ||
fn.Type.Results != nil {
return
}
actionName := fn.Name.Name
if isInterceptorActioName(actionName) {
return
}
controllerName := getName(fn.Recv.List[0].Type)
method := &methodInfo{Name: actionName, StructName: controllerName, Parameters: []*parameterInfo{}}
// processed so set to level 2, used to display unimplemented action details
// TODO for controller check too
for k, v := range routeMethods {
if strings.HasSuffix(k, controllerName) {
if _, found := v[actionName]; found {
v[actionName] = 2
}
}
}
// processing method parameters
for _, field := range fn.Type.Params.List {
for _, fieldName := range field.Names {
te, err := parseParamFieldExpr(pkg.Name(), field.Type)
if err != nil {
log.Errorf("AST: Unable to parse parameter '%s' on action '%s.%s', ignoring it", fieldName.Name, controllerName, actionName)
return
}
var importPath string
if !ess.IsStrEmpty(te.PackageName) {
var found bool
if importPath, found = imports[te.PackageName]; !found {
importPath = pkg.ImportPath
}
}
method.Parameters = append(method.Parameters, ¶meterInfo{
Name: fieldName.Name,
ImportPath: importPath,
Type: te,
})
}
}
if ty := pkg.Types[controllerName]; ty == nil {
log.Errorf("AST: Type[%s] not found in package: %s", controllerName, pkg.ImportPath)
} else {
ty.Methods = append(ty.Methods, method)
}
return
}
func isInterceptorActioName(actionName string) bool {
return (strings.HasPrefix(actionName, "Before") || strings.HasPrefix(actionName, "After") ||
strings.HasPrefix(actionName, "Panic") || strings.HasPrefix(actionName, "Finally"))
}
func getName(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.SelectorExpr:
return getName(t.X)
case *ast.StarExpr:
return getName(t.X)
default:
return ""
}
}
func isBuiltInDataType(typeName string) bool {
_, found := builtInDataTypes[typeName]
return found
}
// parseStructFieldExpr method to find a direct "embedded|sub-type".
// Struct ast.Field as follows:
// Ident { "type-name" } e.g. UserController
// SelectorExpr { "package-name", "type-name" } e.g. aah.Controller
// StarExpr { "*", "package-name", "type-name"} e.g. *aah.Controller
func parseStructFieldExpr(fieldType ast.Expr) (string, string) {
for {
if starExpr, ok := fieldType.(*ast.StarExpr); ok {
fieldType = starExpr.X
continue
}
break
}
// type it's in the same package, it's an ast.Ident.
if ident, ok := fieldType.(*ast.Ident); ok {
return "", ident.Name
}
// type it's in the different package, it's an ast.SelectorExpr.
if selectorExpr, ok := fieldType.(*ast.SelectorExpr); ok {
if pkgIdent, ok := selectorExpr.X.(*ast.Ident); ok {
return pkgIdent.Name, selectorExpr.Sel.Name
}
}
return "", ""
}
func parseParamFieldExpr(pkgName string, expr ast.Expr) (*typeExpr, error) {
switch t := expr.(type) {
case *ast.Ident:
if isBuiltInDataType(t.Name) {
return &typeExpr{Expr: t.Name, IsBuiltIn: true}, nil
}
return &typeExpr{Expr: t.Name, PackageName: pkgName}, nil
case *ast.SelectorExpr:
e, err := parseParamFieldExpr(pkgName, t.X)
return &typeExpr{Expr: t.Sel.Name, PackageName: e.Expr}, err
case *ast.StarExpr:
e, err := parseParamFieldExpr(pkgName, t.X)
return &typeExpr{Expr: "*" + e.Expr, PackageName: e.PackageName, PackageIndex: e.PackageIndex + uint8(1)}, err
case *ast.ArrayType:
e, err := parseParamFieldExpr(pkgName, t.Elt)
return &typeExpr{Expr: "[]" + e.Expr, PackageName: e.PackageName, PackageIndex: e.PackageIndex + uint8(2)}, err
case *ast.Ellipsis:
e, err := parseParamFieldExpr(pkgName, t.Elt)
return &typeExpr{Expr: "[]" + e.Expr, PackageName: e.PackageName, PackageIndex: e.PackageIndex + uint8(2)}, err
}
return nil, errors.New("not a valid fieldname/parameter name")
}