/
definition_store.go
56 lines (42 loc) · 1.3 KB
/
definition_store.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
package main
import (
"strings"
)
/*
For all Go types, we want to always refer to them by some kind of canonical name.
This is what we're going to use here:
foo.bar.package.type
This does NOT use the package name, only the package path and the type name.
This is defined as a method, CanonicalName(), on MemberIntermediates and DefinitionIntermediates.
*/
type DefinitionStore map[string]*DefinitionIntermediate
func (this DefinitionStore) Add(intermediates ...*DefinitionIntermediate) {
for _, intermediate := range intermediates {
_, ok := this[intermediate.CanonicalName()]
if ok {
//log.Print("duplicate detected: " + intermediate.CanonicalName())
//jlog.Log(this)
}
this[intermediate.CanonicalName()] = intermediate
}
}
func (this DefinitionStore) ExistsDefinition(referringPackage, typeName string) (*DefinitionIntermediate, bool) {
typeName = strings.TrimPrefix(typeName, "*")
pkgInfo := pkgInfos[referringPackage]
importPaths := possibleImportPaths(pkgInfo, typeName)
idx := strings.Index(typeName, ".")
if idx != -1 {
typeName = typeName[idx+1:]
}
for _, importPath := range importPaths {
for _, def := range this {
if def.Name != typeName {
continue
}
if def.PackagePath == importPath && def.Name == typeName {
return def, true
}
}
}
return nil, false
}