Skip to content

Commit b996929

Browse files
committed
refactor: renamed CallFunction as UnsafeCallFunction and CheckAndValidateFunction as SafeCallFunction
1 parent dc78b9c commit b996929

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ var (
88
ErrNotAFunction = errors.New("not a function")
99
ErrFunctionParameterCountMismatch = "function parameter count mismatch, expected %d, got %d"
1010
ErrFunctionParameterTypeMismatch = "function parameter type mismatch on index %d, expected %s, got %s"
11+
ErrNilFunctionValue = errors.New("nil function value")
1112
)

function.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func CheckFunction(fn interface{}, params ...interface{}) (
1313
) {
1414
// Get the function and its parameters
1515
fnValue := reflect.ValueOf(fn)
16-
paramValues := make([]reflect.Value, len(params))
16+
paramsValues := make([]reflect.Value, len(params))
1717
for i, param := range params {
18-
paramValues[i] = reflect.ValueOf(param)
18+
paramsValues[i] = reflect.ValueOf(param)
1919
}
2020

2121
// Check if the function is valid
@@ -36,7 +36,7 @@ func CheckFunction(fn interface{}, params ...interface{}) (
3636

3737
// Check if the parameter type matches the function's parameter type
3838
var paramType, fnParamType reflect.Type
39-
for i, paramValue := range paramValues {
39+
for i, paramValue := range paramsValues {
4040
paramType = paramValue.Type()
4141
fnParamType = fnValue.Type().In(i)
4242

@@ -50,16 +50,24 @@ func CheckFunction(fn interface{}, params ...interface{}) (
5050
}
5151
}
5252

53-
return &fnValue, &paramValues, nil
53+
return &fnValue, &paramsValues, nil
5454
}
5555

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) (
5858
[]interface{},
5959
error,
6060
) {
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+
6169
// Call the function with the parameter
62-
results := fnValue.Call(paramValues)
70+
results := fnValue.Call(paramsValues)
6371

6472
// Convert the results to an interface slice
6573
interfaceResults := make([]interface{}, len(results))
@@ -70,17 +78,17 @@ func CallFunction(fnValue *reflect.Value, paramValues ...reflect.Value) (
7078
return interfaceResults, nil
7179
}
7280

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{}) (
7583
[]interface{},
7684
error,
7785
) {
7886
// Check if the function is valid
79-
fnValue, paramValues, err := CheckFunction(fn, params...)
87+
fnValue, paramsValues, err := CheckFunction(fn, params...)
8088
if err != nil {
8189
return nil, err
8290
}
8391

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...)
8694
}

0 commit comments

Comments
 (0)