/
reflect.go
270 lines (213 loc) · 6.79 KB
/
reflect.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package cte
import (
"fmt"
"reflect"
"sort"
"strings"
)
type methodLocation struct {
rootPlanName string
componentStack
}
type method struct {
owningType string
name string
arguments string // comma-separated argument types
outputs string // comma-separated argument types
}
func (m method) hasSameSignature(other method) bool {
return m.name == other.name &&
m.arguments == other.arguments &&
m.outputs == other.outputs
}
func (m method) String() string {
methodSignature := func() string {
if strings.Contains(m.outputs, ",") {
return fmt.Sprintf("%s(%s) (%s)", m.name, m.arguments, m.outputs)
}
if m.outputs == "" {
return fmt.Sprintf("%s(%s)", m.name, m.arguments)
}
return fmt.Sprintf("%s(%s) %s", m.name, m.arguments, m.outputs)
}()
if m.owningType != "" {
return m.owningType + "." + methodSignature
}
return methodSignature
}
var extractMethodDetails = func(rm reflect.Method, ignoreFirstReceiverArgument bool) method {
var arguments []string
for i := 0; i < rm.Type.NumIn(); i++ {
if i == 0 && ignoreFirstReceiverArgument {
continue
}
arguments = append(arguments, rm.Type.In(i).String())
}
var outputs []string
for i := 0; i < rm.Type.NumOut(); i++ {
outputs = append(outputs, rm.Type.Out(i).String())
}
return method{
name: rm.Name,
arguments: strings.Join(arguments, ","),
outputs: strings.Join(outputs, ","),
}
}
//go:generate mockery --name iStructDisassembler --case=underscore --inpackage
type iStructDisassembler interface {
isAvailableMoreThanOnce(m method) bool
findAvailableMethods(name string) (methodSet, bool)
findMethodLocations(ms methodSet, rootPlanName string) []string
addAvailableMethod(rootPlanName string, cs componentStack, m method)
extractAvailableMethods(t reflect.Type) []method
performMethodExtraction(t reflect.Type, rootPlanName string, cs componentStack) []method
extractChildMethods(t reflect.Type, rootPlanName string, cs componentStack) []method
extractOwnMethods(t reflect.Type, rootPlanName string, cs componentStack, hoistedMethods []method) []method
}
type structDisassembler struct {
itself iStructDisassembler
availableMethods map[string]methodSet
methodsAvailableMoreThanOnce methodSet
methodLocations map[method][]methodLocation
}
func newStructDisassembler() structDisassembler {
result := structDisassembler{
availableMethods: make(map[string]methodSet),
methodsAvailableMoreThanOnce: make(methodSet),
methodLocations: make(map[method][]methodLocation),
}
result.itself = result
return result
}
func (sd structDisassembler) self() iStructDisassembler {
if sd.itself == nil {
return sd
}
return sd.itself
}
func (sd structDisassembler) isAvailableMoreThanOnce(m method) bool {
return sd.methodsAvailableMoreThanOnce.has(m)
}
func (sd structDisassembler) findAvailableMethods(name string) (methodSet, bool) {
result, ok := sd.availableMethods[name]
return result, ok && result.count() > 0
}
func (sd structDisassembler) findMethodLocations(ms methodSet, rootPlanName string) []string {
var methodLocations []string
for _, m := range ms.items() {
for _, ml := range sd.methodLocations[m] {
if ml.rootPlanName == rootPlanName {
methodLocations = append(methodLocations, ml.componentStack.String())
}
}
}
return methodLocations
}
func (sd structDisassembler) addAvailableMethod(rootPlanName string, cs componentStack, m method) {
ms, ok := sd.availableMethods[m.name]
if !ok {
ms = make(methodSet)
sd.availableMethods[m.name] = ms
}
// Even if a method is registered twice, they will be declared at different locations.
// Hence, the provided component stack will never be the same and thus, we must record
// the location before checking for duplicate.
sd.methodLocations[m] = append(sd.methodLocations[m], methodLocation{
rootPlanName: rootPlanName,
componentStack: cs.clone(),
})
if ms.has(m) {
sd.methodsAvailableMoreThanOnce.add(m)
return
}
ms.add(m)
}
func (sd structDisassembler) extractAvailableMethods(t reflect.Type) []method {
var cs componentStack
return sd.performMethodExtraction(t, extractFullNameFromType(t), cs)
}
func (sd structDisassembler) performMethodExtraction(t reflect.Type, rootPlanName string, cs componentStack) []method {
cs = cs.push(extractFullNameFromType(t))
defer func() {
cs = cs.pop()
}()
hoistedMethods := sd.self().extractChildMethods(t, rootPlanName, cs)
ownMethods := sd.self().extractOwnMethods(t, rootPlanName, cs, hoistedMethods)
var allMethods []method
allMethods = append(allMethods, hoistedMethods...)
allMethods = append(allMethods, ownMethods...)
return allMethods
}
func (sd structDisassembler) extractChildMethods(t reflect.Type, rootPlanName string, cs componentStack) []method {
var hoistedMethods []method
actualType := t
if actualType.Kind() == reflect.Pointer {
actualType = t.Elem()
}
if actualType.Kind() == reflect.Struct {
for i := 0; i < actualType.NumField(); i++ {
rf := actualType.Field(i)
// Extract methods from embedded fields
if rf.Anonymous {
childMethods := sd.self().performMethodExtraction(rf.Type, rootPlanName, cs)
hoistedMethods = append(hoistedMethods, childMethods...)
}
}
}
return hoistedMethods
}
func (sd structDisassembler) extractOwnMethods(t reflect.Type, rootPlanName string, cs componentStack, hoistedMethods []method) []method {
var ownMethods []method
for i := 0; i < t.NumMethod(); i++ {
rm := t.Method(i)
m := extractMethodDetails(rm, true)
m.owningType = t.PkgPath() + "/" + t.Name()
isHoistedMethod := func() bool {
for j := 0; j < len(hoistedMethods); j++ {
hm := hoistedMethods[j]
if hm.hasSameSignature(m) {
return true
}
}
return false
}()
// If a parent struct has a method carrying the same signature as one
// that is available in an embedded field, it means this is a hoisted
// method or the parent is overriding the same method with its own
// implementation. In either case, we can assume this is a hoisted
// method as it doesn't make a difference to how we should validate
// code templates.
if isHoistedMethod {
continue
}
ownMethods = append(ownMethods, m)
sd.self().addAvailableMethod(rootPlanName, cs, m)
}
return ownMethods
}
type methodSet map[method]struct{}
func (ms methodSet) add(m method) {
ms[m] = struct{}{}
}
func (ms methodSet) has(m method) bool {
_, ok := ms[m]
return ok
}
func (ms methodSet) count() int {
return len(ms)
}
func (ms methodSet) items() []method {
methods := make([]method, 0, len(ms))
for key, _ := range ms {
methods = append(methods, key)
}
return methods
}
func (ms methodSet) String() string {
methods := make([]string, 0, len(ms))
for key, _ := range ms {
methods = append(methods, key.String())
}
sort.Strings(methods)
return strings.Join(methods, ", ")
}