-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathanalyze.go
158 lines (141 loc) · 3.19 KB
/
analyze.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
157
158
package fastbin
import (
"fmt"
"reflect"
)
type analyzer struct {
Packages map[string]*pkgInfo
}
type pkgInfo struct {
Path string
Types map[string]*typeInfo
Imports []string
}
type typeInfo struct {
Type reflect.Type
}
func newAnalyzer() *analyzer {
return &analyzer{
Packages: map[string]*pkgInfo{},
}
}
func (this *analyzer) Analyze(types []reflect.Type) {
for _, typ := range types {
this.analyzeType(typ)
}
for _, pkg := range this.Packages {
imports := make(map[string]string)
visited := make(map[reflect.Type]int)
for _, typ := range pkg.Types {
this.analyzeImport(imports, visited, pkg.Path, typ.Type)
}
pkg.Imports = make([]string, 0, len(imports))
for _, path := range imports {
pkg.Imports = append(pkg.Imports, path)
}
}
}
func (this *analyzer) getPackage(pkgPath string) *pkgInfo {
if info, ok := this.Packages[pkgPath]; ok {
return info
}
info := &pkgInfo{
Path: pkgPath,
Types: map[string]*typeInfo{},
Imports: []string{},
}
this.Packages[pkgPath] = info
return info
}
func (this *analyzer) analyzeType(typ reflect.Type) {
switch typ.Kind() {
case reflect.Struct:
pkg := this.getPackage(typ.PkgPath())
if _, ok := pkg.Types[typ.Name()]; ok {
return
}
pkg.Types[typ.Name()] = &typeInfo{typ}
for i := 0; i < typ.NumField(); i++ {
this.analyzeType(typ.Field(i).Type)
}
case reflect.Ptr:
this.analyzeType(typ.Elem())
case reflect.Array:
this.analyzeType(typ.Elem())
case reflect.Slice:
this.analyzeType(typ.Elem())
case reflect.Map:
switch typ.Key().Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.String,
reflect.Struct,
reflect.Float32,
reflect.Float64:
if typ.Key().Kind() == reflect.Struct {
this.analyzeType(typ.Key())
}
if typ.Elem().Kind() == reflect.Struct {
panic(fmt.Sprintf("unsupported value type '%s' on map '%s'", typ.Key().Kind(), typ))
}
this.analyzeType(typ.Elem())
default:
panic(fmt.Sprintf("unsupported key type '%s' on map '%s'", typ.Key().Kind(), typ))
}
}
}
func (this *analyzer) analyzeImport(imports map[string]string, visited map[reflect.Type]int, pkg string, typ reflect.Type) {
if _, exists := visited[typ]; exists {
return
}
visited[typ] = 1
L:
for {
switch typ.Kind() {
case reflect.Array,
reflect.Ptr,
reflect.Slice,
reflect.Map:
typ = typ.Elem()
if typPkgPath := typ.PkgPath(); typPkgPath != "" && typPkgPath != pkg {
imports[typPkgPath] = typPkgPath
}
default:
break L
}
}
switch typ.Kind() {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.String:
if typPkgPath := typ.PkgPath(); typPkgPath != "" && typPkgPath != pkg {
imports[typPkgPath] = typPkgPath
}
case reflect.Struct:
if typPkgPath := typ.PkgPath(); typPkgPath == pkg {
for i := 0; i < typ.NumField(); i++ {
fieldType := typ.Field(i).Type
this.analyzeImport(imports, visited, pkg, fieldType)
}
}
}
}