-
Notifications
You must be signed in to change notification settings - Fork 2
/
reflective.go
212 lines (187 loc) · 5.88 KB
/
reflective.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
package nject
import (
"fmt"
"reflect"
"strings"
)
// TODO add ExampleReflective
// ReflectiveInvoker is an alternative provider interface that can be used
// for invoke and initialize functions. The key for those functions is that
// their implementation is provided by Collection.Bind.
type ReflectiveInvoker interface {
ReflectiveArgs
Set(func([]reflect.Value) []reflect.Value)
}
// Reflective is an alternative provider interface. Normally, providers are
// are functions or data elements to be injected. If the provider is a Reflective
// then the methods of Reflective will be called to simulate the Reflective
// being a function.
type Reflective interface {
ReflectiveArgs
Call(in []reflect.Value) []reflect.Value
}
// ReflectiveArgs is the part of a Reflective that defines the inputs
// and outputs.
type ReflectiveArgs interface {
In(i int) reflect.Type
NumIn() int
Out(i int) reflect.Type
NumOut() int
}
// ReflectiveWrapper is a special variant of Reflective where the type of
// the first input is described by Inner(). In(0) must return the
// type of func([]reflect.Type) []reflect.Type.
//
// When Call() is invoked, In(0) must be as described by Inner().
type ReflectiveWrapper interface {
Reflective
Inner() ReflectiveArgs
}
// MakeReflective is a simple utility to create a Reflective
func MakeReflective(
inputs []reflect.Type,
outputs []reflect.Type,
function func([]reflect.Value) []reflect.Value,
) Reflective {
return thinReflective{
thinReflectiveArgs: thinReflectiveArgs{
inputs: inputs,
outputs: outputs,
},
fun: function,
}
}
type thinReflectiveArgs struct {
inputs []reflect.Type
outputs []reflect.Type
}
var _ ReflectiveArgs = thinReflectiveArgs{}
func (r thinReflectiveArgs) In(i int) reflect.Type { return r.inputs[i] }
func (r thinReflectiveArgs) NumIn() int { return len(r.inputs) }
func (r thinReflectiveArgs) Out(i int) reflect.Type { return r.outputs[i] }
func (r thinReflectiveArgs) NumOut() int { return len(r.outputs) }
type thinReflective struct {
thinReflectiveArgs
fun func([]reflect.Value) []reflect.Value
}
var _ Reflective = thinReflective{}
type reflectiveBinder struct {
thinReflective
}
var (
_ ReflectiveInvoker = &reflectiveBinder{}
_ Reflective = reflectiveBinder{}
)
func (b *reflectiveBinder) Set(fun func([]reflect.Value) []reflect.Value) {
b.fun = fun
}
func (r thinReflective) Call(in []reflect.Value) []reflect.Value { return r.fun(in) }
func (r thinReflective) String() string {
in := make([]string, len(r.inputs))
for i, input := range r.inputs {
in[i] = input.String()
}
switch len(r.outputs) {
case 0:
return fmt.Sprintf("<reflectiveFunc>(%s)", strings.Join(in, ", "))
case 1:
return fmt.Sprintf("<reflectiveFunc>(%s) %s", strings.Join(in, ", "), r.outputs[0].String())
default:
out := make([]string, len(r.outputs))
for i, output := range r.outputs {
out[i] = output.String()
}
return fmt.Sprintf("<reflectiveFunc>(%s) (%s)", strings.Join(in, ", "), strings.Join(out, ", "))
}
}
// MakeReflectiveWrapper is a utility to create a ReflectiveWrapper
//
// The first argument, downIn, is the types that must be received in the down
// chain and provided to function. This does not include the
// func([]reflec.Value) []reflect.Value that is actually used for the first
// argument.
//
// The second argument, upOut, is the types that are returned on the up chain.
//
// The third argument, downOut, is the types provided in the call to the inner
// function and thus are passed down the down chain.
//
// The forth argument, upIn, is the types returned by the call to the inner
// function and thus are received from the up chain.
//
// When function is called, the first argument will be a reflect.Value, of
// course, that is the value of a function that takes []reflect.Value and
// returns []reflect.Value.
//
// EXPERIMENTAL: this is currently considered experimental and could be removed
// in a future release. If you're using this, please open a pull request to
// remove this comment.
func MakeReflectiveWrapper(
downIn []reflect.Type,
upOut []reflect.Type,
downOut []reflect.Type,
upIn []reflect.Type,
function func([]reflect.Value) []reflect.Value,
) ReflectiveWrapper {
modifiedDownIn := make([]reflect.Type, len(downIn)+1)
modifiedDownIn[0] = reflectiveFuncType
for i, t := range downIn {
modifiedDownIn[i+1] = t
}
return thinReflectiveWrapper{
thinReflective: thinReflective{
thinReflectiveArgs: thinReflectiveArgs{
inputs: modifiedDownIn,
outputs: upOut,
},
fun: function,
},
inner: thinReflectiveArgs{
inputs: downOut,
outputs: upIn,
},
fun: function,
}
}
type thinReflectiveWrapper struct {
thinReflective
inner thinReflectiveArgs
fun func([]reflect.Value) []reflect.Value
}
var _ ReflectiveWrapper = thinReflectiveWrapper{}
func (r thinReflectiveWrapper) Inner() ReflectiveArgs { return r.inner }
// wrappedReflective allows Refelective to kinda pretend to be a reflect.Type
type wrappedReflective struct {
ReflectiveArgs
}
// reflecType is a subset of reflect.Type good enough for use in characterize
type reflectType interface {
Kind() reflect.Kind
NumOut() int
NumIn() int
In(i int) reflect.Type
Elem() reflect.Type
Out(i int) reflect.Type
String() string
}
var _ reflectType = wrappedReflective{}
func (w wrappedReflective) Kind() reflect.Kind { return reflect.Func }
func (w wrappedReflective) Elem() reflect.Type { panic("call not expected") }
func (w wrappedReflective) String() string {
in := make([]string, w.NumIn())
for i := 0; i < w.NumIn(); i++ {
in[i] = w.In(i).String()
}
out := make([]string, w.NumOut())
for i := 0; i < w.NumOut(); i++ {
out[i] = w.Out(i).String()
}
switch len(out) {
case 0:
return "Reflective(" + strings.Join(in, ", ") + ")"
case 1:
return "Reflective(" + strings.Join(in, ", ") + ") " + out[0]
default:
return "Reflective(" + strings.Join(in, ", ") + ") (" + strings.Join(out, ", ") + ")"
}
}