-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
73 lines (59 loc) · 1.46 KB
/
function.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
package jsonapi
import (
"fmt"
"reflect"
)
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// reflectFunc creates a *reflectedFn
func reflectFunc(fn interface{}) *reflectedFn {
t := reflect.TypeOf(fn)
if t.Kind() != reflect.Func {
panic("function expected")
}
v := reflect.ValueOf(fn)
in := make([]reflect.Type, 0, t.NumIn())
for i := 0; i < t.NumIn(); i++ {
in = append(in, t.In(i))
}
rFn := reflectedFn{
fn: v,
in: in,
}
switch t.NumOut() {
case 0:
rFn.outFn = func(in []reflect.Value) (interface{}, error) {
return nil, nil
}
case 1:
if t.Out(0).Implements(errorType) {
rFn.outFn = func(out []reflect.Value) (interface{}, error) {
err, _ := out[0].Interface().(error)
return nil, err
}
} else {
rFn.outFn = func(out []reflect.Value) (interface{}, error) {
return out[0].Interface(), nil
}
}
case 2:
if !t.Out(1).Implements(errorType) {
panic(fmt.Sprintf("function %s second return value must be an error", t.Name()))
}
rFn.outFn = func(out []reflect.Value) (interface{}, error) {
err, _ := out[1].Interface().(error)
return out[0].Interface(), err
}
default:
panic(fmt.Sprintf("function %s cannot return more than two values", t.Name()))
}
return &rFn
}
type reflectedFn struct {
fn reflect.Value
in []reflect.Type
outFn func(out []reflect.Value) (interface{}, error)
}
func (h *reflectedFn) call(in []reflect.Value) (interface{}, error) {
out := h.fn.Call(in)
return h.outFn(out)
}