github.com/songzhibin97/gkit@v1.2.13/distributed/task/validate.go (about) 1 package task 2 3 import ( 4 "errors" 5 "reflect" 6 ) 7 8 var ( 9 ErrTaskMustFunc = errors.New("task type must func") 10 ErrTaskReturnNoValue = errors.New("task return is no value") 11 ErrTaskReturnNoErr = errors.New("task return last values is must be error") 12 ) 13 14 func ValidateTask(task interface{}) error { 15 v := reflect.ValueOf(task) 16 t := v.Type() 17 if t.Kind() != reflect.Func { 18 return ErrTaskMustFunc 19 } 20 21 if t.NumOut() < 1 { 22 return ErrTaskReturnNoValue 23 } 24 lastReturnType := t.Out(t.NumOut() - 1) 25 errorInterface := reflect.TypeOf((*error)(nil)).Elem() 26 if !lastReturnType.Implements(errorInterface) { 27 return ErrTaskReturnNoErr 28 } 29 return nil 30 }