-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.go
122 lines (105 loc) · 2.29 KB
/
finder.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
package main
import (
"errors"
"fmt"
"go/ast"
"strings"
)
var (
UnknownTargetError = errors.New("target Structure unknown")
)
type structFinder struct {
name string
filename string
s *ast.TypeSpec
m []ast.Node
i []*ast.ImportSpec
}
func newStructFinder(name string, filename string) *structFinder {
return &structFinder{name: strings.TrimSpace(name), filename: filename}
}
func (s *structFinder) Find(n ast.Node) bool {
switch lookedFor := n.(type) {
case *ast.ImportSpec:
s.i = append(s.i, lookedFor)
case *ast.TypeSpec:
if lookedFor.Name == nil {
return true
}
if lookedFor.Name.Name == s.name {
switch lookedFor.Type.(type) {
case *ast.StructType:
s.s = lookedFor
case *ast.InterfaceType:
s.s = lookedFor
case *ast.SliceExpr:
s.s = lookedFor
case *ast.ArrayType:
s.s = lookedFor
case *ast.MapType:
s.s = lookedFor
}
}
case *ast.FuncDecl:
if lookedFor.Recv == nil {
return false
}
s.recursiveResolveRcv(lookedFor, lookedFor.Recv.List[0].Type)
default:
return true
}
return false
}
func (s *structFinder) recursiveResolveRcv(n ast.Node, t ast.Expr) {
switch exp := t.(type) {
case *ast.StarExpr:
s.recursiveResolveRcv(n, exp.X)
case *ast.Ident:
if exp.Name == s.name {
s.m = append(s.m, n)
}
case *ast.IndexListExpr:
s.recursiveResolveRcv(n, exp.X)
default:
critical(fmt.Sprintf("WARNING: abnormal func rcv: %typ", n.(*ast.FuncDecl).Recv.List[0].Type))
}
}
func (s *structFinder) Structure() *ast.TypeSpec {
return s.s
}
func (s *structFinder) Methods() []ast.Node {
return s.m
}
func (s *structFinder) Imports() []*ast.ImportSpec {
return s.i
}
type pathFinder struct {
pkg, target string
Success bool
ast.Node
}
func newPathFinder(currentPkg string) *pathFinder {
return &pathFinder{pkg: currentPkg}
}
func (s *pathFinder) Find(n ast.Node) bool {
switch lookedFor := n.(type) {
case *ast.SelectorExpr:
s.pkg = lookedFor.X.(*ast.Ident).Name
s.target = lookedFor.Sel.Name
s.Node = lookedFor
s.Success = true
return false
case *ast.Ident:
s.target = lookedFor.Name
s.Node = lookedFor
s.Success = true
return false
}
return true
}
func (s *pathFinder) Structure() ast.Node {
return s.Node
}
func (s *pathFinder) Path() (pkg, target string) {
return s.pkg, s.target
}