/
package_info.go
227 lines (187 loc) · 5.61 KB
/
package_info.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
package distiller
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
"os"
"path/filepath"
)
// PackageInfo holds information about a package.
type PackageInfo struct {
Package *packages.Package
Imported []*packages.Package // Packages imported from this.
Structs map[string]*StructInfo // Structures declared on this package mapped by fully qualified name.
TypedConsts map[string][]*ConstInfo // Typed constants grouped by fully qualified type name.
}
// loadedPackages caches the loaded/imported packages.
var loadedPackages = make(map[string]*PackageInfo)
// LookupStruct searches loaded packages for the specified fully qualified struct name.
// It returns nil in case of no matches.
func LookupStruct(name string) *StructInfo {
for _, pkg := range loadedPackages {
s, ok := pkg.Structs[name]
if ok {
return s
}
}
return nil
}
// LookupTypedConsts searches loaded packages for declared constants of specified fully qualified named type.
// It returns nil in case of no matches.
func LookupTypedConsts(name string) []*ConstInfo {
consts := []*ConstInfo(nil)
for _, pkg := range loadedPackages {
c, ok := pkg.TypedConsts[name]
if ok {
consts = append(consts, c...)
}
}
return consts
}
// NewPackageInfo creates a new package information object from given directory. The passed name defines
// the struct for which read also defaults values.
func NewPackageInfo(dir string, typeName string) (*PackageInfo, error) {
pkgInfo := new(PackageInfo)
pkgInfo.Structs = make(map[string]*StructInfo)
pkgInfo.TypedConsts = make(map[string][]*ConstInfo)
err := pkgInfo.readPackage(dir)
if err != nil {
return nil, err
}
if typeName != "" {
s, ok := pkgInfo.Structs[pkgInfo.Package.PkgPath+"."+typeName]
if !ok {
return nil, fmt.Errorf("cannot find struct %s in package %s", typeName, pkgInfo.Package.PkgPath)
}
if err = s.ParseDefaultsMethod(); err != nil {
return nil, err
}
}
loadedPackages[pkgInfo.Package.PkgPath] = pkgInfo
return pkgInfo, nil
}
// readPackage reads information for the package defined in the given directory and all imported packages.
func (p *PackageInfo) readPackage(dir string) error {
ok, err := isDirectory(dir)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("%v is not a directory", dir)
}
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles |
packages.NeedImports | packages.NeedDeps | packages.NeedSyntax |
packages.NeedTypes | packages.NeedTypesInfo,
}
pkgs, err := packages.Load(cfg, dir)
if err != nil {
return err
}
if len(pkgs) != 1 {
return fmt.Errorf("expected exactly 1 package for given pattern")
}
p.Package = pkgs[0]
for ident, object := range p.Package.TypesInfo.Defs {
typeName, ok := object.(*types.TypeName)
if !ok {
continue
}
for _, astFile := range p.Package.Syntax {
// Use the position to test if the type is declared in this file.
if astFile.Pos() >= typeName.Pos() || typeName.Pos() >= astFile.End() {
continue
}
typeNameString := typeName.Type().String()
nodes, _ := astutil.PathEnclosingInterval(astFile, typeName.Pos(), typeName.Pos())
isStruct := false
for _, node := range nodes {
var genDecl *ast.GenDecl
genDecl, ok = node.(*ast.GenDecl)
if !ok {
continue
}
if len(genDecl.Specs) != 1 {
return fmt.Errorf("expected one spec for struct declaration")
}
var typeSpec *ast.TypeSpec
typeSpec, ok = genDecl.Specs[0].(*ast.TypeSpec)
// Identifier is not a type declaration or not match, continue.
if !ok || typeSpec.Name != ident {
continue
}
// The identifier matches but the type is not a struct, exit loop.
if _, ok = typeSpec.Type.(*ast.StructType); !ok {
break
}
info := NewStructInfo(genDecl, p.Package)
for _, field := range info.Fields {
var namedType *types.Named
namedType, ok = field.Type.(*types.Named)
// types.Basic type.
if !ok {
continue
}
// Check if required package is loaded.
pkgPath := namedType.Obj().Pkg().Path()
_, ok = loadedPackages[pkgPath]
if pkgPath == p.Package.PkgPath || ok {
continue
}
// Load required package.
imported := p.Package.Imports[pkgPath]
if _, err = NewPackageInfo(filepath.Dir(imported.GoFiles[0]), ""); err != nil {
return err
}
}
p.Structs[typeNameString] = info
isStruct = true
break
}
// Not a struct, check if identifier is used by typed constants.
if !isStruct {
if consts := p.readIdentConsts(astFile, ident); consts != nil {
p.TypedConsts[typeNameString] = consts
}
}
}
}
return nil
}
// readIdentConsts reads typed constants what uses ident type. Returns nil if no constants use the type.
func (p *PackageInfo) readIdentConsts(astFile *ast.File, ident *ast.Ident) []*ConstInfo {
consts := []*ConstInfo(nil)
for _, decl := range astFile.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.CONST {
continue
}
for _, spec := range genDecl.Specs {
var valueSpec *ast.ValueSpec
valueSpec, ok = spec.(*ast.ValueSpec)
if !ok {
continue
}
if valueSpec.Type != nil {
var constIdent *ast.Ident
constIdent, ok = valueSpec.Type.(*ast.Ident)
if !ok || constIdent.Obj != ident.Obj {
continue
}
}
consts = append(consts, NewConstInfo(valueSpec, p.Package))
}
}
return consts
}
// isDirectory reports whether the named file is a directory.
func isDirectory(name string) (bool, error) {
info, err := os.Stat(name)
if err != nil {
return false, err
}
return info.IsDir(), nil
}