-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.go
66 lines (56 loc) · 1.68 KB
/
calc.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
package lr0
import (
"reflect"
"github.com/pkg/errors"
)
type calcFunc func([]any) (any, error)
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
func newCalcFunc(fn any, expectArgsCount int) calcFunc {
if fn == nil && expectArgsCount == 1 {
return calcDefaultBubble
}
funcV := reflect.ValueOf(fn)
if funcV.Kind() != reflect.Func {
panic(errors.Wrapf(ErrDefine, "fn contains not a func: %s", funcV.Kind()))
}
if funcV.IsNil() {
panic(errors.Wrap(ErrDefine, "fn func is nil"))
}
funcT := funcV.Type()
if funcT.NumIn() != expectArgsCount {
panic(errors.Wrapf(ErrDefine, "fn arguments count is %d when wanted %d", funcT.NumIn(), expectArgsCount))
}
if funcT.IsVariadic() {
panic(errors.Wrap(ErrDefine, "fn func is variadic"))
}
switch funcT.NumOut() {
case 1:
return func(v []any) (any, error) {
res := funcV.Call(prepareCalcArgs(v))
return res[0].Interface(), nil
}
case 2:
if t1 := funcT.Out(1); t1.Kind() != reflect.Interface || !t1.Implements(typeOfError) || !typeOfError.AssignableTo(t1) {
panic(errors.Wrapf(ErrDefine, "fn func 2nd result must be `error`, given %v", t1))
}
return func(v []any) (any, error) {
res := funcV.Call(prepareCalcArgs(v))
v0 := res[0].Interface()
v1 := res[1].Interface()
if v1 == nil {
return v0, nil
}
return v0, v1.(error)
}
default:
panic(errors.Wrapf(ErrDefine, "fn results count is %d when wanted 1 (value any) or 2 (value any, err error)", funcT.NumOut()))
}
}
func prepareCalcArgs(vs []any) []reflect.Value {
res := make([]reflect.Value, 0, len(vs))
for _, v := range vs {
res = append(res, reflect.ValueOf(v))
}
return res
}
func calcDefaultBubble(v []any) (any, error) { return v[0], nil }