/
file.go
156 lines (128 loc) · 3.43 KB
/
file.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
package source
import (
"fmt"
"github.com/LeMikaelF/proxy-generator/generator/internal/method"
"go/ast"
"go/token"
"path"
"strings"
)
type inspectorAdapter struct {
delegate func(node ast.Node, f func(ast.Node) bool)
file *ast.File
}
func (i *inspectorAdapter) Inspect(pre func(ast.Node) bool) {
i.delegate(i.file, pre)
}
type File struct {
file *ast.File
inspect func(pre func(ast.Node) bool)
}
func NewFile(file *ast.File) *File {
inspect := &inspectorAdapter{ast.Inspect, file}
return &File{file: file, inspect: inspect.Inspect}
}
func (s *File) FindStructDeclaration(typeName string) *ast.GenDecl {
var structDecl *ast.GenDecl
s.inspect(func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
return true
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if ok && typeSpec.Name.Name == typeName {
structDecl = genDecl
return false
}
}
return true
})
return structDecl
}
func (s *File) toAstFile() *ast.File {
return s.file
}
func (s *File) FindMethods(structName string, passThroughMethods map[string]bool) ([]method.Method, []string) {
var methods []method.Method
imports := make(map[string]struct{})
importMap := s.collectImports()
s.inspect(func(n ast.Node) bool {
funcDecl, ok := n.(*ast.FuncDecl)
if !ok || funcDecl.Recv == nil || len(funcDecl.Recv.List) == 0 {
return true
}
recvType, hasStar := getReceiver(funcDecl)
ident, ok := recvType.(*ast.Ident)
if !ok || ident.Name != structName {
return true
}
m := method.New(passThroughMethods, funcDecl, ident, hasStar)
methods = append(methods, m)
populateImports(imports, m, importMap)
return true
})
return methods, mapToSlice(imports)
}
type importInfo struct {
Path string
Alias string
}
func (s *File) collectImports() map[string]importInfo {
importMap := make(map[string]importInfo)
s.inspect(func(n ast.Node) bool {
importSpec, ok := n.(*ast.ImportSpec)
if !ok {
return true
}
importPath := strings.Trim(importSpec.Path.Value, "\"")
alias := ""
if importSpec.Name != nil {
alias = importSpec.Name.Name
} else {
_, alias = path.Split(importPath)
}
importMap[alias] = importInfo{Path: importPath, Alias: alias}
return true
})
return importMap
}
func getReceiver(funcDecl *ast.FuncDecl) (ast.Expr, bool) {
recvType := funcDecl.Recv.List[0].Type
starExpr, isStar := recvType.(*ast.StarExpr)
if isStar {
recvType = starExpr.X
}
return recvType, isStar
}
func populateImports(imports map[string]struct{}, m method.Method, importMap map[string]importInfo) {
for _, field := range append(m.ParamTypes, m.ResultExprs...) {
switch t := field.Type.(type) {
case *ast.Ident:
if info, ok := importMap[t.Name]; ok {
imports[fmt.Sprintf("%s %q", info.Alias, info.Path)] = struct{}{}
}
case *ast.SelectorExpr:
if x, ok := t.X.(*ast.Ident); ok {
if info, ok := importMap[x.Name]; ok {
imports[fmt.Sprintf("%s %q", info.Alias, info.Path)] = struct{}{}
}
}
case *ast.StarExpr:
if se, ok := t.X.(*ast.SelectorExpr); ok {
if x, ok := se.X.(*ast.Ident); ok {
if info, ok := importMap[x.Name]; ok {
imports[fmt.Sprintf("%s %q", info.Alias, info.Path)] = struct{}{}
}
}
}
}
}
}
func mapToSlice(imports map[string]struct{}) []string {
importsList := make([]string, 0, len(imports))
for k := range imports {
importsList = append(importsList, k)
}
return importsList
}