-
Notifications
You must be signed in to change notification settings - Fork 4
/
funcs.go
33 lines (31 loc) · 870 Bytes
/
funcs.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
package goutil
import (
"fmt"
"reflect"
)
// Call the func by name stored in m, with params as args
func Call(m map[string]interface{},
name string, params ...interface{}) ([]reflect.Value, error) {
var nf interface{}
if nf = m[name]; nf == nil {
return nil, fmt.Errorf("func %s not found", name)
}
f := reflect.ValueOf(nf)
if f.Kind() != reflect.Func {
return nil, fmt.Errorf("%s is not a function", name)
}
t := f.Type()
if !t.IsVariadic() && len(params) != t.NumIn() {
return nil, fmt.Errorf("(len(params)=%d) != (t.NumIn()=%d)",
len(params), t.NumIn())
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
if !t.IsVariadic() && in[k].Type() != t.In(k) {
return nil, fmt.Errorf("(in[%d].Type()=%s) != (t.In(%d)=%s)",
k, in[k].Type(), k, t.In(k))
}
}
return f.Call(in), nil
}