@@ -13,9 +13,9 @@ func CheckFunction(fn interface{}, params ...interface{}) (
13
13
) {
14
14
// Get the function and its parameters
15
15
fnValue := reflect .ValueOf (fn )
16
- paramValues := make ([]reflect.Value , len (params ))
16
+ paramsValues := make ([]reflect.Value , len (params ))
17
17
for i , param := range params {
18
- paramValues [i ] = reflect .ValueOf (param )
18
+ paramsValues [i ] = reflect .ValueOf (param )
19
19
}
20
20
21
21
// Check if the function is valid
@@ -36,7 +36,7 @@ func CheckFunction(fn interface{}, params ...interface{}) (
36
36
37
37
// Check if the parameter type matches the function's parameter type
38
38
var paramType , fnParamType reflect.Type
39
- for i , paramValue := range paramValues {
39
+ for i , paramValue := range paramsValues {
40
40
paramType = paramValue .Type ()
41
41
fnParamType = fnValue .Type ().In (i )
42
42
@@ -50,16 +50,24 @@ func CheckFunction(fn interface{}, params ...interface{}) (
50
50
}
51
51
}
52
52
53
- return & fnValue , & paramValues , nil
53
+ return & fnValue , & paramsValues , nil
54
54
}
55
55
56
- // CallFunction dynamically calls a function with some typed parameters
57
- func CallFunction (fnValue * reflect.Value , paramValues ... reflect.Value ) (
56
+ // UnsafeCallFunction calls a function with some typed parameters without checking if the function is valid
57
+ func UnsafeCallFunction (fnValue * reflect.Value , paramsValues ... reflect.Value ) (
58
58
[]interface {},
59
59
error ,
60
60
) {
61
+ // Check if the function or the parameters values are nil
62
+ if fnValue == nil {
63
+ return nil , ErrNilFunctionValue
64
+ }
65
+ if paramsValues == nil {
66
+ paramsValues = make ([]reflect.Value , 0 )
67
+ }
68
+
61
69
// Call the function with the parameter
62
- results := fnValue .Call (paramValues )
70
+ results := fnValue .Call (paramsValues )
63
71
64
72
// Convert the results to an interface slice
65
73
interfaceResults := make ([]interface {}, len (results ))
@@ -70,17 +78,17 @@ func CallFunction(fnValue *reflect.Value, paramValues ...reflect.Value) (
70
78
return interfaceResults , nil
71
79
}
72
80
73
- // CheckAndCallFunction checks if a function is valid and calls it with some typed parameters
74
- func CheckAndCallFunction (fn interface {}, params ... interface {}) (
81
+ // SafeCallFunction calls a function with some typed parameters after checking if the function is valid
82
+ func SafeCallFunction (fn interface {}, params ... interface {}) (
75
83
[]interface {},
76
84
error ,
77
85
) {
78
86
// Check if the function is valid
79
- fnValue , paramValues , err := CheckFunction (fn , params ... )
87
+ fnValue , paramsValues , err := CheckFunction (fn , params ... )
80
88
if err != nil {
81
89
return nil , err
82
90
}
83
91
84
- // Call the function with the parameter
85
- return CallFunction (fnValue , * paramValues ... )
92
+ // Call the function with the parameter (now, we are sure that the function is valid)
93
+ return UnsafeCallFunction (fnValue , * paramsValues ... )
86
94
}
0 commit comments