Created
April 2, 2020 10:07
-
-
Save thinhdanggroup/264a144d4a96631aec9ed2752b450ed5 to your computer and use it in GitHub Desktop.
Validate Handler and Input Params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func validateFunc(handler interface{}, nArgs int) (interface{}, error) { | |
method := reflect.ValueOf(handler) | |
f := reflect.Indirect(method) | |
// check type func | |
if f.Kind() != reflect.Func { | |
return f, fmt.Errorf("%T must be a Function ", f) | |
} | |
methodType := method.Type() | |
numIn := methodType.NumIn() | |
// check number of args | |
if nArgs < numIn { | |
return nil, errors.New("Call with too few input arguments") | |
} else if nArgs > numIn { | |
return nil, errors.New("Call with too many input arguments") | |
} | |
return f, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment