-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallable.go
277 lines (251 loc) · 8.49 KB
/
callable.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
271
272
273
274
275
276
277
/*
Copyright 2021 Joseph Cumines
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bigbuff
import (
"fmt"
"reflect"
)
type (
// Callable models a function, and is used by this package to provide a higher-level mechanism (than the reflect
// package) for calling arbitrary functions, in a generic way, see also the NewCallable factory function
Callable interface {
// Type must return a valid type of kind func, corresponding to the type of the Callable
Type() reflect.Type
// Call accepts an args function and passes results into a results function, to be utilised in a manner
// generally equivalent to `results(callable(args()))`, treating nil (interface values) as omitting any
// args, or not handling any return value. Note that Call will return an error if args or results are not
// compatible with the underlying types, indicated by Callable.Type. See also bigbuff.Call and CallOption.
Call(args, results interface{}) error
}
// CallOption models a configuration option for the Call function provided by this package, see also the
// Call-prefixed functions provided by this package
CallOption func(config *callConfig) error
// callConfig models configuration for the Call function
callConfig struct {
this reflect.Type
args interface{}
results interface{}
}
callable struct {
callableValue
}
callableValue interface {
Type() reflect.Type
Call(in []reflect.Value) []reflect.Value
}
)
// NewCallable initialises a new Callable from fn, which must be a non-nil function, but is otherwise unconstrained,
// note a panic will occur if fn is not a function, or is a function but isn't non-nil
func NewCallable(fn interface{}) Callable {
v := reflect.ValueOf(fn)
if kind := v.Kind(); kind != reflect.Func {
panic(fmt.Errorf(`bigbuff.NewCallable not func: %v`, kind))
}
if v.IsNil() {
panic(fmt.Errorf(`bigbuff.NewCallable nil func`))
}
return &callable{v}
}
// Call will call a Callable with any provided options, available as functions provided by this package that are
// prefixed with "Call", see also MustCall
func Call(caller Callable, options ...CallOption) error {
config := &callConfig{this: caller.Type()}
if kind := config.this.Kind(); kind != reflect.Func {
return fmt.Errorf(`bigbuff.Call type error: not func: %v`, kind)
}
for _, option := range options {
if err := option(config); err != nil {
return err
}
}
return caller.Call(config.args, config.results)
}
// MustCall is equivalent to Call but will panic on error
func MustCall(caller Callable, options ...CallOption) {
if err := Call(caller, options...); err != nil {
panic(err)
}
}
// CallArgs returns a CallOption that will pass the provided args to a Callable that is called via the Call function
func CallArgs(args ...interface{}) CallOption {
return func(config *callConfig) error {
in, err := resolveArgs(config.this, typesArgs(args))
if err != nil {
return fmt.Errorf(`bigbuff.CallArgs %s`, err)
}
config.args = reflect.MakeFunc(
reflect.FuncOf(nil, in, false),
func([]reflect.Value) (results []reflect.Value) {
results = make([]reflect.Value, len(in))
for i, in := range in {
results[i] = reflect.New(in).Elem()
results[i].Set(reflect.ValueOf(args[i]))
}
return
},
).Interface()
return nil
}
}
// CallResults returns a CallOption that will assign the call's results to the values pointed at by results
func CallResults(results ...interface{}) CallOption {
return func(config *callConfig) error {
out := typesInOut(config.this.NumOut(), config.this.Out)
if len(results) != len(out) {
return fmt.Errorf(`bigbuff.CallResults results error: invalid length: mandatory=%d len=%d`, len(out), len(results))
}
for i, out := range out {
v := reflect.ValueOf(results[i])
t := v.Type()
if kind := t.Kind(); kind != reflect.Ptr {
return fmt.Errorf(`bigbuff.CallResults results[%d] error: %v kind %v not ptr`, i, t, kind)
}
if v.IsNil() {
return fmt.Errorf(`bigbuff.CallResults results[%d] error: %v value is nil`, i, t)
}
t = t.Elem()
if !out.AssignableTo(t) {
return fmt.Errorf(`bigbuff.CallResults results[%d] error: %v not assignable to %v`, i, out, t)
}
}
config.results = reflect.MakeFunc(
reflect.FuncOf(out, nil, false),
func(args []reflect.Value) []reflect.Value {
for i, arg := range args {
reflect.ValueOf(results[i]).Elem().Set(arg)
}
return nil
},
).Interface()
return nil
}
}
// CallResultsSlice returns a CallOption that will append the call's results to a slice pointed at by target
func CallResultsSlice(target interface{}) CallOption {
return func(config *callConfig) error {
value := reflect.ValueOf(target)
if value.Kind() != reflect.Ptr {
return fmt.Errorf(`bigbuff.CallResultsSlice target error: not ptr: %T`, target)
}
if value.IsNil() {
return fmt.Errorf(`bigbuff.CallResultsSlice target error: nil ptr: %T`, target)
}
if value.Elem().Kind() != reflect.Slice {
return fmt.Errorf(`bigbuff.CallResultsSlice target error: not slice: %T`, target)
}
out := typesInOut(config.this.NumOut(), config.this.Out)
{
elem := value.Elem().Type().Elem()
for i, v := range out {
if !v.AssignableTo(elem) {
return fmt.Errorf(`bigbuff.CallResultsSlice results[%d] error: %v not assignable to %v`, i, v, elem)
}
out[i] = elem
}
}
config.results = reflect.MakeFunc(
reflect.FuncOf(out, nil, false),
func(args []reflect.Value) []reflect.Value {
if len(args) != 0 {
value.Elem().Set(reflect.Append(value.Elem(), args...))
}
return nil
},
).Interface()
return nil
}
}
// CallArgsRaw returns a CallOption that will pass args to Callable.Call without modification or validation
func CallArgsRaw(args interface{}) CallOption {
return func(config *callConfig) error {
config.args = args
return nil
}
}
// CallResultsRaw returns a CallOption that will pass results to Callable.Call without modification or validation
func CallResultsRaw(results interface{}) CallOption {
return func(config *callConfig) error {
config.results = results
return nil
}
}
// Call implements Callable.Call using the reflect package
func (x *callable) Call(args, results interface{}) error {
var argsV reflect.Value
if args != nil {
argsV = reflect.ValueOf(args)
if kind := argsV.Kind(); kind != reflect.Func {
return fmt.Errorf(`bigbuff.callable args error: not func: %v`, kind)
}
if argsV.IsNil() {
return fmt.Errorf(`bigbuff.callable args error: nil func`)
}
if n := argsV.Type().NumIn(); n != 0 && (n != 1 || !argsV.Type().IsVariadic()) {
return fmt.Errorf(`bigbuff.callable args error: mandatory input`)
}
}
var resultsV reflect.Value
if results != nil {
resultsV = reflect.ValueOf(results)
if kind := resultsV.Kind(); kind != reflect.Func {
return fmt.Errorf(`bigbuff.callable results error: not func: %v`, kind)
}
if resultsV.IsNil() {
return fmt.Errorf(`bigbuff.callable results error: nil func`)
}
}
var in []reflect.Value
if argsV != (reflect.Value{}) {
in = argsV.Call(nil)
}
out := x.callableValue.Call(in)
if resultsV != (reflect.Value{}) {
resultsV.Call(out)
}
return nil
}
func typesInOut(n int, fn func(i int) reflect.Type) []reflect.Type {
r := make([]reflect.Type, n)
for i := range r {
r[i] = fn(i)
}
return r
}
func typesArgs(args []interface{}) []reflect.Type {
r := make([]reflect.Type, len(args))
for i, arg := range args {
r[i] = reflect.TypeOf(arg)
}
return r
}
func resolveArgs(this reflect.Type, args []reflect.Type) ([]reflect.Type, error) {
var (
in = typesInOut(this.NumIn(), this.In)
variadic reflect.Type
)
if this.IsVariadic() {
variadic, in[len(in)-1], in = in[len(in)-1].Elem(), nil, in[:len(in)-1]
for len(args) > len(in) {
in = append(in, variadic)
}
}
if len(args) != len(in) {
return nil, fmt.Errorf(`args error: invalid length: mandatory=%d variadic=%v len=%d`, len(in), variadic != nil, len(args))
}
for i, in := range in {
if !args[i].AssignableTo(in) {
return nil, fmt.Errorf(`args[%d] error: %v not assignable to %v`, i, args[i], in)
}
}
return in, nil
}