-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
118 lines (100 loc) · 3 KB
/
package.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
package ainsp
import (
"go/ast"
"go/build"
"go/token"
"path/filepath"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// PackageInfo struct and its methods
//______________________________________________________________________________
// PackageInfo holds the single paackge information.
type packageInfo struct {
ImportPath string
FilePath string
Files []string
Types map[string]*typeInfo
Fset *token.FileSet
Pkg *ast.Package
}
// Name method return package name
func (p *packageInfo) Name() string {
return filepath.Base(p.ImportPath)
}
func (p *packageInfo) processTypes(decl ast.Decl, imports map[string]string) {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || !isTypeTok(genDecl) || len(genDecl.Specs) == 0 {
return
}
spec := genDecl.Specs[0].(*ast.TypeSpec)
st, ok := spec.Type.(*ast.StructType)
if !ok {
// Not a struct type
return
}
typeName := spec.Name.Name
ty := &typeInfo{
Name: typeName,
ImportPath: filepath.ToSlash(p.ImportPath),
Methods: make([]*methodInfo, 0),
EmbeddedTypes: make([]*typeInfo, 0),
}
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.Decl, imports map[string]string) {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || !isImportTok(genDecl) {
return
}
for _, dspec := range genDecl.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
}
}