mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-08 14:03:24 +09:00
chore: enable forcetypeassert linter, fix issues (#38804)
Enable [`forcetypeassert`](https://github.com/gostaticanalysis/forcetypeassert) linter to prevent unchecked type assertions. ~650 issues fixed, most fixes were clean, some use `setting.PanicInDevOrTesting`. The only behaviour changes are where code would previously send a 500 error or panic, a 4xx error is now emitted. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -58,11 +58,9 @@ func (t *Task) IsEnabled() bool {
|
||||
// GetConfig will return a copy of the task's config
|
||||
func (t *Task) GetConfig() Config {
|
||||
if reflect.TypeOf(t.config).Kind() == reflect.Pointer {
|
||||
// Pointer:
|
||||
return reflect.New(reflect.ValueOf(t.config).Elem().Type()).Interface().(Config)
|
||||
return reflect.New(reflect.ValueOf(t.config).Elem().Type()).Interface().(Config) //nolint:forcetypeassert // pointer
|
||||
}
|
||||
// Not pointer:
|
||||
return reflect.New(reflect.TypeOf(t.config)).Elem().Interface().(Config)
|
||||
return reflect.New(reflect.TypeOf(t.config)).Elem().Interface().(Config) //nolint:forcetypeassert // not pointer
|
||||
}
|
||||
|
||||
// Run will run the task incrementing the cron counter with no user defined
|
||||
@@ -124,9 +122,9 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) {
|
||||
if err := t.fun(ctx, doer, config); err != nil {
|
||||
var message string
|
||||
var status string
|
||||
if db.IsErrCancelled(err) {
|
||||
if errCancelled, ok := err.(db.ErrCancelled); ok {
|
||||
status = "cancelled"
|
||||
message = err.(db.ErrCancelled).Message
|
||||
message = errCancelled.Message
|
||||
} else {
|
||||
status = "error"
|
||||
message = err.Error()
|
||||
@@ -168,7 +166,7 @@ func GetTask(name string) *Task {
|
||||
}
|
||||
|
||||
// RegisterTask allows a task to be registered with the cron service
|
||||
func RegisterTask(name string, config Config, fun func(context.Context, *user_model.User, Config) error) error {
|
||||
func RegisterTask[T Config](name string, config T, fun func(context.Context, *user_model.User, T) error) error {
|
||||
log.Debug("Registering task: %s", name)
|
||||
|
||||
i18nKey := "admin.dashboard." + name
|
||||
@@ -185,7 +183,9 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
|
||||
task := &Task{
|
||||
Name: name,
|
||||
config: config,
|
||||
fun: fun,
|
||||
fun: func(ctx context.Context, doer *user_model.User, runConfig Config) error {
|
||||
return fun(ctx, doer, runConfig.(T)) //nolint:forcetypeassert // must be valid
|
||||
},
|
||||
}
|
||||
lock.Lock()
|
||||
locked := true
|
||||
@@ -218,7 +218,7 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
|
||||
}
|
||||
|
||||
// RegisterTaskFatal will register a task but if there is an error log.Fatal
|
||||
func RegisterTaskFatal(name string, config Config, fun func(context.Context, *user_model.User, Config) error) {
|
||||
func RegisterTaskFatal[T Config](name string, config T, fun func(context.Context, *user_model.User, T) error) {
|
||||
if err := RegisterTask(name, config, fun); err != nil {
|
||||
log.Fatal("Unable to register cron task %s Error: %v", name, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user