-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_call.go
95 lines (88 loc) · 2.17 KB
/
function_call.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
package test
import (
"fmt"
"reflect"
)
var Err = fmt.Errorf("an error")
func foo1(_ string, _ []int) {}
func foo2(_ string, _ []int) error { return Err }
func BatchFoo2(limit int, str string, ints []int) error {
for index := 0; index*limit < len(ints); index++ {
if (index+1)*limit < len(ints) {
err := foo2(str, ints[index*limit:(index+1)*limit])
if err != nil {
return err
}
} else {
err := foo2(str, ints[index*limit:])
if err != nil {
return err
}
}
}
return nil
}
func BatchFoo1(limit int, str string, ints []int) {
for index := 0; index*limit < len(ints); index++ {
if (index+1)*limit < len(ints) {
foo1(str, ints[index*limit:(index+1)*limit])
} else {
foo1(str, ints[index*limit:])
}
}
}
// BatchCallByReflect
func BatchCallByReflect(
limit int,
f interface{},
args ...interface{},
) error {
argsLen := len(args)
if argsLen == 0 {
return fmt.Errorf("args len cant be zero")
}
if limit < 1 {
return fmt.Errorf("limit < 1")
}
sliceValue := reflect.ValueOf(args[argsLen-1])
if sliceValue.Kind() != reflect.Slice {
return fmt.Errorf("last arg must be slice")
}
function := reflect.ValueOf(f)
if function.Kind() != reflect.Func {
return fmt.Errorf("invalid func kind:%s", sliceValue.Kind().String())
}
if argsLen != function.Type().NumIn() {
return fmt.Errorf("wrong args len, expected: %d, got: %d", function.Type().NumIn(), argsLen)
}
argv := make([]reflect.Value, argsLen)
for i := 0; i < argsLen-1; i++ {
argv[i] = reflect.ValueOf(args[i])
}
argvIn := append(argv[:argsLen-1], reflect.Value{})
sliceValueLen := sliceValue.Len()
call := func(argvIn []reflect.Value) error {
retValues := function.Call(argvIn)
if len(retValues) == 0 {
return nil
}
for i := len(retValues) - 1; i >= 0; i-- {
if err, ok := retValues[i].Interface().(error); ok {
return err
}
}
return nil
}
for index := 0; index*limit < sliceValueLen; index++ {
if (index+1)*limit < sliceValueLen {
argvIn[argsLen-1] = sliceValue.Slice(index*limit, (index+1)*limit)
} else {
argvIn[argsLen-1] = sliceValue.Slice(index*limit, sliceValueLen)
}
err := call(argvIn)
if err != nil {
return err
}
}
return nil
}