-
Notifications
You must be signed in to change notification settings - Fork 14
/
reg_import.go
72 lines (58 loc) · 1.48 KB
/
reg_import.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
package goql
import (
"sync"
"github.com/fzerorubigd/goql/astdata"
)
type importProvider struct {
cache map[string][]interface{}
lock *sync.Mutex
}
func (v *importProvider) Provide(in interface{}) []interface{} {
v.lock.Lock()
defer v.lock.Unlock()
p := in.(*astdata.Package)
if d, ok := v.cache[p.Path()]; ok {
return d
}
va := p.Imports()
res := make([]interface{}, len(va))
for i := range va {
res[i] = va[i]
}
v.cache[p.Path()] = res
return res
}
type canonicalCol struct{}
func (canonicalCol) Value(in interface{}) String {
im := in.(*astdata.Import)
if im.Canonical() == "" {
return String{Null: true}
}
return String{String: im.Canonical()}
}
type pathCol struct{}
func (pathCol) Value(in interface{}) String {
im := in.(*astdata.Import)
return String{String: im.Path()}
}
type packageCol struct{}
func (packageCol) Value(in interface{}) String {
im := in.(*astdata.Import)
return String{String: im.TargetPackage()}
}
func registerImport() {
RegisterTable("imports", &importProvider{
cache: make(map[string][]interface{}),
lock: &sync.Mutex{},
})
RegisterField("imports", "pkg_name", genericPackageName{})
RegisterField("imports", "pkg_path", genericPackagePath{})
RegisterField("imports", "file", genericFileName{})
RegisterField("imports", "docs", genericDoc{})
RegisterField("imports", "canonical", canonicalCol{})
RegisterField("imports", "path", pathCol{})
RegisterField("imports", "package", packageCol{})
}
func init() {
registerImport()
}