-
Notifications
You must be signed in to change notification settings - Fork 56
/
resolver.go
64 lines (52 loc) · 1.46 KB
/
resolver.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
package gotypes
import (
"errors"
"go/ast"
"go/types"
)
func New(uses map[*ast.Ident]types.Object) *DecoratorResolver {
return &DecoratorResolver{Uses: uses}
}
type DecoratorResolver struct {
Uses map[*ast.Ident]types.Object // Types info - must include Uses
}
func (r *DecoratorResolver) ResolveIdent(file *ast.File, parent ast.Node, parentField string, id *ast.Ident) (string, error) {
if r.Uses == nil {
return "", errors.New("gotypes.DecoratorResolver needs Uses in types info")
}
if se, ok := parent.(*ast.SelectorExpr); ok && parentField == "Sel" {
// if the parent is a SelectorExpr and this Ident is in the Sel field, only resolve the path
// if X is a package identifier
xid, ok := se.X.(*ast.Ident)
if !ok {
// x is not an ident -> not a qualified identifier
return "", nil
}
obj, ok := r.Uses[xid]
if !ok {
// not found in uses -> not a qualified identifier
return "", nil
}
pn, ok := obj.(*types.PkgName)
if !ok {
// not a pkgname -> not a remote identifier
return "", nil
}
return pn.Imported().Path(), nil
}
obj, ok := r.Uses[id]
if !ok {
// not found in uses -> not a remote identifier
return "", nil
}
if v, ok := obj.(*types.Var); ok && v.IsField() {
// field ident (e.g. name of a field in a composite literal) -> doesn't need qualified ident
return "", nil
}
pkg := obj.Pkg()
if pkg == nil {
// pre-defined idents in the universe scope - e.g. "byte"
return "", nil
}
return pkg.Path(), nil
}