mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-11 10:16:18 +00: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:
+16
-20
@@ -98,20 +98,12 @@ type RegisterableSource interface {
|
||||
|
||||
var registeredConfigs = map[Type]func() Config{}
|
||||
|
||||
// RegisterTypeConfig register a config for a provided type
|
||||
func RegisterTypeConfig(typ Type, exemplar Config) {
|
||||
if reflect.TypeOf(exemplar).Kind() == reflect.Pointer {
|
||||
// Pointer:
|
||||
registeredConfigs[typ] = func() Config {
|
||||
return reflect.New(reflect.ValueOf(exemplar).Elem().Type()).Interface().(Config)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Not a Pointer
|
||||
registeredConfigs[typ] = func() Config {
|
||||
return reflect.New(reflect.TypeOf(exemplar)).Elem().Interface().(Config)
|
||||
}
|
||||
// RegisterTypeConfig register a config for a provided type, the exemplar argument only serves type inference
|
||||
func RegisterTypeConfig[T interface {
|
||||
*E
|
||||
Config
|
||||
}, E any](typ Type, _ T) {
|
||||
registeredConfigs[typ] = func() Config { return T(new(E)) }
|
||||
}
|
||||
|
||||
// Source represents an external way for authorizing users.
|
||||
@@ -188,6 +180,16 @@ func (source *Source) IsSSPI() bool {
|
||||
return source.Type == SSPI
|
||||
}
|
||||
|
||||
// MustSourceCfg returns the source's config as T. The registry populates Cfg from the
|
||||
// source type, so a mismatch is a programming error the caller can't recover from.
|
||||
func MustSourceCfg[T Config](source *Source) T {
|
||||
cfg, ok := source.Cfg.(T)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("auth source %q (id=%d, type=%s) has config %T, expected %s", source.Name, source.ID, source.Type, source.Cfg, reflect.TypeFor[T]()))
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// HasTLS returns true of this source supports TLS.
|
||||
func (source *Source) HasTLS() bool {
|
||||
hasTLSer, ok := source.Cfg.(HasTLSer)
|
||||
@@ -371,12 +373,6 @@ type ErrSourceAlreadyExist struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// IsErrSourceAlreadyExist checks if an error is a ErrSourceAlreadyExist.
|
||||
func IsErrSourceAlreadyExist(err error) bool {
|
||||
_, ok := err.(ErrSourceAlreadyExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrSourceAlreadyExist) Error() string {
|
||||
return fmt.Sprintf("login source already exists [name: %s]", err.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user