-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct.go
More file actions
248 lines (184 loc) · 4.8 KB
/
construct.go
File metadata and controls
248 lines (184 loc) · 4.8 KB
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
package testo
import (
"fmt"
"reflect"
"github.com/ozontech/testo/internal/reflectutil"
"github.com/ozontech/testo/internal/stack"
"github.com/ozontech/testo/testoplugin"
)
//nolint:cyclop,funlen // this is the core of the whole framework
func construct[T CommonT](
t TestingT,
parent *T,
fill func(t *testoT),
options ...testoplugin.Option,
) T {
t.Helper()
seed := testoT{
common: t,
testingT: t,
levelOptions: options,
}
if parent != nil {
seed.parent = (*parent).unwrap()
}
if fill != nil {
fill(&seed)
}
// Passed T type may be an interface.
// In that case, we can not initialize it, since
// we don't know its underlying concrete type.
//
// However, parent is always a concrete value,
// so, if present, we extract that type from it.
realType := reflect.TypeFor[T]()
if parent != nil {
rv := reflect.ValueOf(*parent)
realType = rv.Type()
}
// special case when T is *testo.T
if realType == reflect.TypeFor[*testoT]() {
return any(&seed).(T)
}
value := reflect.New(realType)
if !reflectutil.Fill(value) {
panic(fmt.Sprintf(
"testo: infinite type recursion detected for %s inside %s",
reflectutil.FindRecursiveType(realType),
realType,
))
}
//nolint:nestif // TODO: factor out common logic
if parent == nil {
pluginTypes := make(map[reflect.Type]struct{})
collectPlugins(realType, pluginTypes)
delete(pluginTypes, realType)
plugins := make(map[reflect.Type]testoplugin.Plugin, len(pluginTypes))
for pluginType := range pluginTypes {
var child testoplugin.Plugin
if pluginType == reflect.TypeFor[*testoT]() {
child = &seed
} else {
v := reflect.New(pluginType.Elem())
reflectutil.Fill(v)
child = v.Interface().(testoplugin.Plugin)
}
plugins[pluginType] = child
}
seed.plugins = plugins
} else {
parentUnwrapped := (*parent).unwrap()
plugins := make(map[reflect.Type]testoplugin.Plugin, len(parentUnwrapped.plugins))
for pluginType := range parentUnwrapped.plugins {
var child testoplugin.Plugin
if pluginType == reflect.TypeFor[*testoT]() {
child = &seed
} else {
v := reflect.New(pluginType.Elem())
reflectutil.Fill(v)
child = v.Interface().(testoplugin.Plugin)
}
plugins[pluginType] = child
}
seed.plugins = plugins
}
specsStack := stack.New[typedPlugin]()
for pluginType, pluginValue := range seed.plugins {
specsStack.Push(typedPlugin{
Plugin: pluginValue,
Type: pluginType,
})
setPlugins(reflect.ValueOf(pluginValue), seed.plugins, &specsStack)
}
setPlugins(value, seed.plugins, &specsStack)
specs := make(map[reflect.Type]testoplugin.Spec, len(seed.plugins))
for {
p, ok := specsStack.Pop()
if !ok {
break
}
if _, ok := specs[p.Type]; ok {
continue
}
var parentPlugin testoplugin.Plugin
if parent != nil {
parentPlugin = (*parent).unwrap().plugins[p.Type]
} else {
parentPlugin = reflect.New(p.Type).Elem().Interface().(testoplugin.Plugin)
}
specs[p.Type] = p.Plugin.Plugin(parentPlugin, seed.options()...)
}
seed.spec = mergeSpecs(t, mapValues(specs)...)
return value.Elem().Interface().(T)
}
type typedPlugin struct {
testoplugin.Plugin
Type reflect.Type
}
func setPlugins(
v reflect.Value,
plugins map[reflect.Type]testoplugin.Plugin,
specs *stack.Stack[typedPlugin],
) {
if plugin, ok := plugins[v.Type().Elem()]; ok {
elem := v.Elem()
if !elem.IsValid() {
panic(fmt.Sprintf("testo: invalid elem for %s", v.Type()))
}
if !elem.CanSet() {
// TODO(metafates): add path to the field so that it is clear where error happens
panic(fmt.Sprintf("testo: can't set value for %s", v.Type()))
}
elem.Set(reflect.ValueOf(plugin))
specs.Push(typedPlugin{
Plugin: elem.Interface().(testoplugin.Plugin),
Type: elem.Type(),
})
}
v = reflectutil.Elem(v)
if v.Kind() != reflect.Struct {
return
}
// special case - we do not go deeper than that.
if v.Type() == reflect.TypeFor[T]() {
return
}
for i := range v.NumField() {
field := v.Field(i)
if !v.Type().Field(i).IsExported() {
continue
}
if field.Kind() != reflect.Pointer {
panic(
fmt.Sprintf(
"testo: all exported fields in T must be pointers, got: %s",
field.Type(),
),
)
}
setPlugins(field.Addr(), plugins, specs)
}
}
var pluginInterfaceType = reflect.TypeFor[testoplugin.Plugin]()
func collectPlugins(typ reflect.Type, plugins map[reflect.Type]struct{}) {
if typ.Implements(pluginInterfaceType) {
plugins[typ] = struct{}{}
}
typ = reflectutil.Elem(typ)
if typ.Kind() != reflect.Struct {
return
}
for i := range typ.NumField() {
field := typ.Field(i)
if field.IsExported() {
collectPlugins(field.Type, plugins)
}
}
}
func mapValues[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}