reduce DB's Use method complexity and make it easier to understand (#3930)

This commit is contained in:
Qt 2021-01-10 10:15:48 +08:00 committed by GitHub
parent d888c799d7
commit f9131e309d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 8 deletions

15
gorm.go
View File

@ -380,15 +380,14 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac
return nil
}
func (db *DB) Use(plugin Plugin) (err error) {
func (db *DB) Use(plugin Plugin) error {
name := plugin.Name()
if _, ok := db.Plugins[name]; !ok {
if err = plugin.Initialize(db); err == nil {
db.Plugins[name] = plugin
}
} else {
if _, ok := db.Plugins[name]; ok {
return ErrRegistered
}
return err
if err := plugin.Initialize(db); err != nil {
return err
}
db.Plugins[name] = plugin
return nil
}