-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.go
411 lines (363 loc) · 9.1 KB
/
dir.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
package xreflect
import (
"fmt"
"go/ast"
"golang.org/x/mod/modfile"
"path"
"reflect"
"strconv"
"strings"
)
type (
DirTypes struct {
options
types map[string]reflect.Type
subDirs map[string]*DirTypes
path string
specs map[string]*TypeSpec
values map[string]interface{}
methods map[string]*Methods
scopes map[string]*ast.Scope
packages map[string]string
imports map[string]GoImports
typesOccurrences map[string][]string
}
Methods struct {
receiver string
methods []*ast.FuncDecl
}
TypeSpec struct {
path string
pkg string
spec *ast.TypeSpec
*DirTypes
}
GoImport struct {
Name string
Module string
}
GoImports []*GoImport
)
func (i GoImports) Lookup(pkgName string) string {
for _, candidate := range i {
if candidate.Name == pkgName {
return candidate.Module
}
}
for _, candidate := range i {
if strings.HasSuffix(candidate.Module, "/"+pkgName) {
return candidate.Module
}
}
return ""
}
func (i GoImports) OwnertPkgPath(pkg string) string {
pkgPath := i.Lookup(pkg)
if pkgPath == "" {
return pkgPath
}
parts := strings.Split(pkgPath, "/")
if len(parts) > 1 {
return path.Join(parts[len(parts)-2:]...)
}
return ""
}
func (i *GoImport) depPath(basePath string, module *modfile.Module) string {
return path.Join(basePath, i.folder(module))
}
func (i *GoImport) folder(module *modfile.Module) string {
if mod := module; mod != nil && strings.Contains(i.Module, module.Mod.Path) {
if index := strings.Index(i.Module, mod.Mod.Path); index != -1 {
return strings.Trim(i.Module[index+len(mod.Mod.Path):], "/")
}
}
return ""
}
func (i GoImports) lookup(packageAlias string) *GoImport {
for _, cadndidate := range i {
if cadndidate.Name == packageAlias {
return cadndidate
}
}
suffix := "/" + packageAlias
for _, cadndidate := range i {
if strings.HasSuffix(cadndidate.Module, suffix) {
return cadndidate
}
}
return nil
}
func newGoImports(file *ast.File) GoImports {
var imports []*GoImport
for _, spec := range file.Imports {
value, _ := strconv.Unquote(spec.Path.Value)
imp := &GoImport{Module: value}
if spec.Name != nil {
imp.Name = spec.Name.Name
}
imports = append(imports, imp)
}
return imports
}
func (t *DirTypes) addPackage(aPath string, pkg string) {
t.packages[aPath] = pkg
if strings.HasSuffix(aPath, ".go") {
parent, _ := path.Split(aPath)
if strings.HasSuffix(parent, "/") {
parent = parent[:len(parent)-1]
}
t.packages[parent] = pkg
}
}
func (t *DirTypes) PackagePath(aPath string) string {
return t.packages[aPath]
}
func NewDirTypes(path string) *DirTypes {
ret := &DirTypes{
path: path,
types: map[string]reflect.Type{},
subDirs: map[string]*DirTypes{},
specs: map[string]*TypeSpec{},
values: map[string]interface{}{},
methods: map[string]*Methods{},
imports: map[string]GoImports{},
scopes: map[string]*ast.Scope{},
packages: map[string]string{},
typesOccurrences: map[string][]string{},
}
return ret
}
func (t *TypeSpec) lookup(packagePath, packageIdentifier, typeName string) (reflect.Type, error) {
rType, err := t.lookupType(packagePath, packageIdentifier, typeName)
if err != nil {
return nil, err
}
if t.options.onLookup != nil {
t.options.onLookup(packagePath, packageIdentifier, typeName, rType)
}
return rType, nil
}
func (t *TypeSpec) lookupType(packagePath string, packageIdentifier string, typeName string) (reflect.Type, error) {
lookup := t.options.lookup
if lookup == nil && t.options.Registry != nil {
lookup = t.options.Registry.Lookup
}
if lookup != nil {
rType, err := lookup(typeName, WithPackagePath(packagePath), WithPackage(packageIdentifier), WithGoImports(t.GoImports))
if err == nil {
return rType, nil
}
}
rType, err := t.Type(typeName)
if rType != nil {
return rType, nil
}
if packageIdentifier != "" && packageIdentifier != t.pkg && t.module != nil {
imps := t.DirTypes.imports[t.path]
impModule := imps.lookup(packageIdentifier)
folder := impModule.folder(t.module)
subDir, ok := t.subDirs[folder]
subDirPath := impModule.depPath(t.moduleLocation, t.module)
if !ok {
if subDir, err = ParseTypes(subDirPath, withOptions(&t.options)); err == nil {
t.subDirs[folder] = subDir
}
}
if subDir != nil {
rType, err = subDir.Type(typeName)
if rType != nil {
return rType, nil
}
}
}
if imports, ok := t.imports[t.path]; ok {
if imp := imports.lookup(packageIdentifier); imp != nil {
location, folder := sourceLocation(t, imp)
if location != "" {
subDir, ok := t.subDirs[folder]
if !ok {
subDir, err = ParseTypes(location, withOptions(&t.options))
if err != nil {
return nil, err
}
t.subDirs[folder] = subDir
}
dirSpec := &TypeSpec{path: t.path, DirTypes: subDir}
return dirSpec.lookup(packagePath, packageIdentifier, typeName)
}
}
}
return nil, err
}
func (t *DirTypes) registerTypeSpec(path string, pkg string, spec *ast.TypeSpec) {
t.specs[spec.Name.Name] = &TypeSpec{
path: path,
pkg: pkg,
spec: spec,
}
t.typesOccurrences[spec.Name.Name] = append(t.typesOccurrences[spec.Name.Name], path)
}
func (t *DirTypes) Type(name string) (reflect.Type, error) {
if rType, ok := t.types[name]; ok {
return rType, nil
}
goImports := t.GoImports
spec, ok := t.specs[name]
if !ok {
return nil, fmt.Errorf("not found type %v", name)
}
spec.DirTypes = t
if len(goImports) > 0 {
spec.GoImports = goImports
}
pkgPath := ""
matched, err := spec.matchType(spec.pkg, &pkgPath, spec.spec, spec.spec.Type, t.GoImports)
if err != nil {
return nil, err
}
t.types[name] = matched
return matched, nil
}
func (t *DirTypes) Value(symbol string) (interface{}, error) {
if value, ok := t.values[symbol]; ok {
return value, nil
}
for _, scope := range t.scopes {
aValue, ok := t.valueInScope(symbol, scope)
if ok {
t.values[symbol] = aValue
return aValue, nil
}
}
return nil, t.notFoundValueError(symbol)
}
func (t *DirTypes) notFoundValueError(value string) error {
return fmt.Errorf("not found value %v", value)
}
// TypesNames returns types names
func (t *DirTypes) TypesNames() []string {
var result []string
if len(t.specs) == 0 {
return result
}
for key := range t.specs {
result = append(result, key)
}
return result
}
func (t *DirTypes) DirTypes(pkg string) *DirTypes {
return t.subDirs["/"+pkg]
}
func (t *DirTypes) TypesInPackage(pkg string) []string {
var result []string
spec, ok := t.subDirs["/"+pkg]
if !ok {
return result
}
for k := range spec.specs {
result = append(result, k)
}
return result
}
func (t *DirTypes) TypeNamesInPath(aPath string) []string {
var result []string
val, ok := t.scopes[aPath]
if !ok {
return result
}
for k := range val.Objects {
result = append(result, k)
}
return result
}
func (t *DirTypes) MatchTypeNamesInPath(aPath string, comments string) string {
typeNames := t.TypeNamesInPath(aPath)
lcComments := strings.ToLower(comments)
for _, typeName := range typeNames {
aSpec, ok := t.specs[typeName]
if !ok {
continue
}
if comments := aSpec.spec.Comment; comments != nil {
for _, candidate := range comments.List {
if strings.Contains(candidate.Text, lcComments) {
return typeName
}
}
}
}
return ""
}
func (t *DirTypes) valueInScope(name string, scope *ast.Scope) (interface{}, bool) {
if anObject := scope.Lookup(name); anObject != nil {
spec, ok := anObject.Decl.(*ast.ValueSpec)
if !ok {
return nil, false
}
for _, value := range spec.Values {
return value, true
}
}
return nil, false
}
func (t *DirTypes) addScope(path string, scope *ast.Scope) {
if scope == nil {
return
}
t.scopes[path] = scope
}
func (t *DirTypes) Methods(receiver string) []*ast.FuncDecl {
if methods, ok := t.methods[receiver]; ok {
return methods.methods
}
return nil
}
func (t *DirTypes) registerMethod(receiver string, spec *ast.FuncDecl) {
index, ok := t.methods[receiver]
if !ok {
index = &Methods{
receiver: receiver,
}
t.methods[receiver] = index
}
index.methods = append(index.methods, spec)
}
func (t *DirTypes) addImports(path string, file *ast.File) error {
t.imports[path] = newGoImports(file)
return nil
}
func (t *DirTypes) Imports(path string) []string {
if strings.HasPrefix(path, "*") {
path = path[1:]
var imports []string
for fileName, fileImports := range t.imports {
if strings.HasSuffix(fileName, path) {
for _, imp := range fileImports {
imports = append(imports, imp.Module)
}
}
}
return imports
}
var result []string
if values, ok := t.imports[path]; ok {
for _, imp := range values {
result = append(result, imp.Module)
}
}
return result
}
func (t *DirTypes) ValueInFile(file, value string) (interface{}, error) {
scope, ok := t.scopes[file]
if !ok {
return nil, fmt.Errorf("not found file %v", file)
}
aValue, ok := t.valueInScope(value, scope)
if ok {
return aValue, nil
}
return nil, t.notFoundValueError(value)
}
func (t *DirTypes) TypesOccurrences(typeName string) []string {
return t.typesOccurrences[typeName]
}