-
Notifications
You must be signed in to change notification settings - Fork 2
/
reflective_test.go
244 lines (225 loc) · 5.94 KB
/
reflective_test.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
package nject
import (
"fmt"
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type argsWrapper struct {
t reflect.Type
}
var _ ReflectiveArgs = argsWrapper{}
func (f argsWrapper) NumIn() int { return f.t.NumIn() }
func (f argsWrapper) In(i int) reflect.Type { return f.t.In(i) }
func (f argsWrapper) NumOut() int { return f.t.NumOut() }
func (f argsWrapper) Out(i int) reflect.Type { return f.t.Out(i) }
type wrapShell struct {
argsWrapper
inner argsWrapper
v reflect.Value
}
var _ ReflectiveWrapper = wrapShell{}
func (w wrapShell) Inner() ReflectiveArgs { return w.inner }
func (w wrapShell) Call(in []reflect.Value) []reflect.Value {
inner := in[0].Interface().(func([]reflect.Value) []reflect.Value)
in[0] = reflect.MakeFunc(w.v.Type().In(0), inner)
return w.v.Call(in)
}
type funcWrapper struct {
v reflect.Value
}
var _ Reflective = funcWrapper{}
func (f funcWrapper) NumIn() int { return f.v.Type().NumIn() }
func (f funcWrapper) In(i int) reflect.Type { return f.v.Type().In(i) }
func (f funcWrapper) NumOut() int { return f.v.Type().NumOut() }
func (f funcWrapper) Out(i int) reflect.Type { return f.v.Type().Out(i) }
func (f funcWrapper) Call(in []reflect.Value) []reflect.Value { return f.v.Call(in) }
type invokeWrapper struct {
argsWrapper
v reflect.Value
}
var _ ReflectiveInvoker = invokeWrapper{}
func (f invokeWrapper) Set(imp func([]reflect.Value) []reflect.Value) {
f.v.Elem().Set(reflect.MakeFunc(f.v.Type().Elem(), imp))
}
func TestManualReflective(t *testing.T) {
t.Parallel()
var called bool
var fCalled bool
f := func(inner func(), _ s1) {
fCalled = true
assert.False(t, called, "before inner")
inner()
assert.True(t, called, "after inner")
}
w := funcWrapper{v: reflect.ValueOf(f)}
_ = Run("TestReflective",
s1("s1"),
w,
func() {
called = true
},
)
assert.True(t, fCalled, "f called")
}
func TestReflective(t *testing.T) {
t.Parallel()
var buf string
type binder func(*Collection, interface{}, interface{})
print := func(v ...interface{}) {
buf += fmt.Sprint(v...)
}
cases := []struct {
name string
collection *Collection
call func(*Collection, binder)
call2 func(*Collection, binder)
call3 func(*Collection, binder)
}{
{
name: "simple",
collection: Sequence("terminal error",
int64(8),
func(i func() error) {
print("error is", i())
},
func(s string) (bool, TerminalError) {
return strconv.ParseBool(s)
},
func(b bool) int {
print("got", b)
if b {
return 1
}
return 0
}),
call: func(c *Collection, doBind binder) {
var x func(string) int
doBind(c, &x, nil)
assert.Equal(t, 1, x("true"))
},
},
{
name: "wrapper",
collection: Sequence("wrapper",
func(inner func(string, int) bool, i int) string {
s := strconv.Itoa(i)
b := inner(s, i)
s2 := strconv.FormatBool(b)
return s2
},
func(s string, i int) bool {
s2 := strconv.Itoa(i)
return s == s2
}),
call: func(c *Collection, doBind binder) {
var x func(int) string
doBind(c, &x, nil)
assert.Equal(t, "true", x(32))
},
},
{
name: "with-init",
collection: Sequence("with-init",
func(s string, i int) string {
is := strconv.Itoa(i)
return s + " " + is
}),
call: func(c *Collection, doBind binder) {
var x func(string) string
var y func(int)
doBind(c, &x, &y)
y(10)
y(11)
assert.Equal(t, "ten 10", x("ten"))
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
names := []string{"regular-bind", "reflective-bind"}
for i, doBind := range []binder{
func(c *Collection, invokeF interface{}, initF interface{}) {
err := c.Bind(invokeF, initF)
require.NoError(t, err, "bind")
},
func(c *Collection, invokeF interface{}, initF interface{}) {
v := reflect.ValueOf(invokeF)
t.Log("reflective bind to", v.Type().Elem())
invokeV := invokeWrapper{
argsWrapper: argsWrapper{
t: v.Type().Elem(),
},
v: v,
}
if initF != nil {
iv := reflect.ValueOf(initF)
initV := invokeWrapper{
argsWrapper: argsWrapper{
t: iv.Type().Elem(),
},
v: iv,
}
err := c.Bind(invokeV, initV)
require.NoError(t, err, "reflective bind")
} else {
err := c.Bind(invokeV, nil)
require.NoError(t, err, "reflective bind")
}
},
} {
t.Run(names[i], func(t *testing.T) {
for callNum, call := range []func(*Collection, binder){tc.call, tc.call2, tc.call3} {
if call == nil {
continue
}
t.Run(fmt.Sprintf("call%d", callNum+1), func(t *testing.T) {
call(tc.collection, doBind)
})
rc := rewriteReflective(tc.collection, false)
t.Run(fmt.Sprintf("call-reflective%d", callNum+1), func(t *testing.T) {
call(rc, doBind)
})
rwc := rewriteReflective(tc.collection, true)
t.Run(fmt.Sprintf("call-reflective-wrapper%d", callNum+1), func(t *testing.T) {
call(rwc, doBind)
})
}
})
}
})
}
}
func rewriteReflective(c *Collection, inner bool) *Collection {
n := Sequence("redone")
for _, fm := range c.contents {
//nolint:govet // fm shadows, yah, duh
fm := fm.copy()
switch fm.fn.(type) {
case ReflectiveWrapper, Reflective:
//
default:
if reflect.TypeOf(fm.fn).Kind() == reflect.Func {
fm.fn = makeReflectiveShell(fm.fn, inner)
}
}
n.contents = append(n.contents, fm)
}
return n
}
func makeReflectiveShell(fn interface{}, inner bool) Reflective {
v := reflect.ValueOf(fn)
if !inner {
return funcWrapper{v: v}
}
if v.Type().NumIn() == 0 || v.Type().In(0).Kind() != reflect.Func {
return funcWrapper{v: v}
}
return wrapShell{
argsWrapper: argsWrapper{t: v.Type()},
inner: argsWrapper{t: v.Type().In(0)},
v: v,
}
}