You can not select more than 25 topics
Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- package tasks
-
- import (
- "errors"
- "reflect"
- )
-
- var (
- // ErrTaskMustBeFunc ...
- ErrTaskMustBeFunc = errors.New("Task must be a func type")
- // ErrTaskReturnsNoValue ...
- ErrTaskReturnsNoValue = errors.New("Task must return at least a single value")
- // ErrLastReturnValueMustBeError ..
- ErrLastReturnValueMustBeError = errors.New("Last return value of a task must be error")
- )
-
- // ValidateTask validates task function using reflection and makes sure
- // it has a proper signature. Functions used as tasks must return at least a
- // single value and the last return type must be error
- func ValidateTask(task interface{}) error {
- v := reflect.ValueOf(task)
- t := v.Type()
-
- // Task must be a function
- if t.Kind() != reflect.Func {
- return ErrTaskMustBeFunc
- }
-
- // Task must return at least a single value
- if t.NumOut() < 1 {
- return ErrTaskReturnsNoValue
- }
-
- // Last return value must be error
- lastReturnType := t.Out(t.NumOut() - 1)
- errorInterface := reflect.TypeOf((*error)(nil)).Elem()
- if !lastReturnType.Implements(errorInterface) {
- return ErrLastReturnValueMustBeError
- }
-
- return nil
- }
|