-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathfuncmap.go
More file actions
45 lines (39 loc) · 984 Bytes
/
Copy pathfuncmap.go
File metadata and controls
45 lines (39 loc) · 984 Bytes
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
package libs
import (
"errors"
"reflect"
)
var (
ErrParamsNotAdapted = errors.New("The number of params is not adapted.")
)
type Funcs map[string]reflect.Value
func NewFuncs(size int) Funcs {
return make(Funcs, size)
}
func (f Funcs) Bind(name string, fn interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(name + " is not callable.")
}
}()
v := reflect.ValueOf(fn)
v.Type().NumIn()
f[name] = v
return
}
func (f Funcs) Call(name string, params ... interface{}) (result []reflect.Value, err error) {
if _, ok := f[name]; !ok {
err = errors.New(name + " does not exist.")
return
}
if len(params) != f[name].Type().NumIn() {
err = ErrParamsNotAdapted
return
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
result = f[name].Call(in)
return
}