From 8d716be896f5321a20d456541ef14baf9bfae22f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 15 Jan 2016 21:03:35 +0800 Subject: [PATCH] Fix some go vet/lint reports --- association.go | 5 ++--- association_test.go | 6 +++--- join_table_handler.go | 6 +++--- main.go | 28 +++++++++++++--------------- main_test.go | 4 ++-- model_struct.go | 3 +-- multi_primary_keys_test.go | 2 +- pointer_test.go | 18 +++++++++--------- preload_test.go | 2 +- scope.go | 30 +++++++++++++++--------------- scope_private.go | 2 +- utils.go | 8 ++++---- 12 files changed, 55 insertions(+), 59 deletions(-) diff --git a/association.go b/association.go index 30ea36b2..e0978f2a 100644 --- a/association.go +++ b/association.go @@ -375,7 +375,7 @@ func toQueryMarks(primaryValues [][]interface{}) string { for _, primaryValue := range primaryValues { var marks []string - for _, _ = range primaryValue { + for _ = range primaryValue { marks = append(marks, "?") } @@ -396,9 +396,8 @@ func toQueryCondition(scope *Scope, columns []string) string { if len(columns) > 1 { return fmt.Sprintf("(%v)", strings.Join(newColumns, ",")) - } else { - return strings.Join(newColumns, ",") } + return strings.Join(newColumns, ",") } func toQueryValues(primaryValues [][]interface{}) (values []interface{}) { diff --git a/association_test.go b/association_test.go index ab3abd91..c2f55d0e 100644 --- a/association_test.go +++ b/association_test.go @@ -16,7 +16,7 @@ func TestBelongsTo(t *testing.T) { } if err := DB.Save(&post).Error; err != nil { - t.Errorf("Got errors when save post", err.Error()) + t.Error("Got errors when save post", err) } if post.Category.ID == 0 || post.MainCategory.ID == 0 { @@ -184,7 +184,7 @@ func TestHasOne(t *testing.T) { } if err := DB.Save(&user).Error; err != nil { - t.Errorf("Got errors when save user", err.Error()) + t.Error("Got errors when save user", err.Error()) } if user.CreditCard.UserId.Int64 == 0 { @@ -331,7 +331,7 @@ func TestHasMany(t *testing.T) { } if err := DB.Save(&post).Error; err != nil { - t.Errorf("Got errors when save post", err.Error()) + t.Error("Got errors when save post", err) } for _, comment := range post.Comments { diff --git a/join_table_handler.go b/join_table_handler.go index 006701a6..6e7f9045 100644 --- a/join_table_handler.go +++ b/join_table_handler.go @@ -173,8 +173,8 @@ func (s JoinTableHandler) JoinWith(handler JoinTableHandlerInterface, db *DB, so return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTableName, strings.Join(joinConditions, " AND "))). Where(condString, toQueryValues(foreignFieldValues)...) - } else { - db.Error = errors.New("wrong source type for join table handler") - return db } + + db.Error = errors.New("wrong source type for join table handler") + return db } diff --git a/main.go b/main.go index ed902d0b..376a967f 100644 --- a/main.go +++ b/main.go @@ -98,8 +98,8 @@ func (s *DB) New() *DB { } // NewScope create scope for callbacks, including DB's search information -func (db *DB) NewScope(value interface{}) *Scope { - dbClone := db.clone() +func (s *DB) NewScope(value interface{}) *Scope { + dbClone := s.clone() dbClone.Value = value return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value} } @@ -311,9 +311,9 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB { func (s *DB) Exec(sql string, values ...interface{}) *DB { scope := s.clone().NewScope(nil) - generatedSql := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values}) - generatedSql = strings.TrimSuffix(strings.TrimPrefix(generatedSql, "("), ")") - scope.Raw(generatedSql) + generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values}) + generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")") + scope.Raw(generatedSQL) return scope.Exec().db } @@ -372,15 +372,16 @@ func (s *DB) RecordNotFound() bool { return s.Error == RecordNotFound } -// Migrations -func (s *DB) CreateTable(values ...interface{}) *DB { +// CreateTable create table for models +func (s *DB) CreateTable(models ...interface{}) *DB { db := s.clone() - for _, value := range values { - db = db.NewScope(value).createTable().db + for _, model := range models { + db = db.NewScope(model).createTable().db } return db } +// DropTable drop table for models func (s *DB) DropTable(values ...interface{}) *DB { db := s.clone() for _, value := range values { @@ -393,6 +394,7 @@ func (s *DB) DropTable(values ...interface{}) *DB { return db } +// DropTableIfExists drop table for models only when it exists func (s *DB) DropTableIfExists(values ...interface{}) *DB { db := s.clone() for _, value := range values { @@ -459,12 +461,8 @@ func (s *DB) CurrentDatabase() string { return name } -/* -Add foreign key to the given scope - -Example: - db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT") -*/ +// AddForeignKey Add foreign key to the given scope +// Example: db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT") func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB { scope := s.clone().NewScope(s.Value) scope.addForeignKey(field, dest, onDelete, onUpdate) diff --git a/main_test.go b/main_test.go index 9d90bbc7..251c19aa 100644 --- a/main_test.go +++ b/main_test.go @@ -115,7 +115,7 @@ func TestSetTable(t *testing.T) { DB.Create(getPreparedUser("pluck_user3", "pluck_user")) if err := DB.Table("users").Where("role = ?", "pluck_user").Pluck("age", &[]int{}).Error; err != nil { - t.Errorf("No errors should happen if set table for pluck", err.Error()) + t.Error("No errors should happen if set table for pluck", err) } var users []User @@ -545,7 +545,7 @@ func TestTimeWithZone(t *testing.T) { DB.First(&findUser, "name = ?", name) foundBirthday = findUser.Birthday.UTC().Format(format) if foundBirthday != expectedBirthday { - t.Errorf("User's birthday should not be changed after find for name=%s, expected bday=%+v but actual value=%+v or %+v", name, expectedBirthday, foundBirthday) + t.Errorf("User's birthday should not be changed after find for name=%s, expected bday=%+v but actual value=%+v", name, expectedBirthday, foundBirthday) } if DB.Where("id = ? AND birthday >= ?", findUser.Id, user.Birthday.Add(-time.Minute)).First(&findUser2).RecordNotFound() { diff --git a/model_struct.go b/model_struct.go index d80165c8..b47f8534 100644 --- a/model_struct.go +++ b/model_struct.go @@ -560,9 +560,8 @@ func (scope *Scope) generateSqlTag(field *StructField) string { if strings.TrimSpace(additionalType) == "" { return sqlType - } else { - return fmt.Sprintf("%v %v", sqlType, additionalType) } + return fmt.Sprintf("%v %v", sqlType, additionalType) } func parseTagSetting(tags reflect.StructTag) map[string]string { diff --git a/multi_primary_keys_test.go b/multi_primary_keys_test.go index ea80326e..8b275d18 100644 --- a/multi_primary_keys_test.go +++ b/multi_primary_keys_test.go @@ -21,7 +21,7 @@ type Tag struct { ID uint `gorm:"primary_key"` Locale string `gorm:"primary_key"` Value string - Blogs []*Blog `gorm:"many2many:"blogs_tags` + Blogs []*Blog `gorm:"many2many:blogs_tags"` } func compareTags(tags []Tag, contents []string) bool { diff --git a/pointer_test.go b/pointer_test.go index b47717f3..2a68a5ab 100644 --- a/pointer_test.go +++ b/pointer_test.go @@ -39,46 +39,46 @@ func TestPointerFields(t *testing.T) { var nilPointerStruct = PointerStruct{} if err := DB.Create(&nilPointerStruct).Error; err != nil { - t.Errorf("Failed to save nil pointer struct", err) + t.Error("Failed to save nil pointer struct", err) } var pointerStruct2 PointerStruct if err := DB.First(&pointerStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil { - t.Errorf("Failed to query saved nil pointer struct", err) + t.Error("Failed to query saved nil pointer struct", err) } var normalStruct2 NormalStruct if err := DB.Table(tableName).First(&normalStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil { - t.Errorf("Failed to query saved nil pointer struct", err) + t.Error("Failed to query saved nil pointer struct", err) } var partialNilPointerStruct1 = PointerStruct{Num: &num} if err := DB.Create(&partialNilPointerStruct1).Error; err != nil { - t.Errorf("Failed to save partial nil pointer struct", err) + t.Error("Failed to save partial nil pointer struct", err) } var pointerStruct3 PointerStruct if err := DB.First(&pointerStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || *pointerStruct3.Num != num { - t.Errorf("Failed to query saved partial nil pointer struct", err) + t.Error("Failed to query saved partial nil pointer struct", err) } var normalStruct3 NormalStruct if err := DB.Table(tableName).First(&normalStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || normalStruct3.Num != num { - t.Errorf("Failed to query saved partial pointer struct", err) + t.Error("Failed to query saved partial pointer struct", err) } var partialNilPointerStruct2 = PointerStruct{Name: &name} if err := DB.Create(&partialNilPointerStruct2).Error; err != nil { - t.Errorf("Failed to save partial nil pointer struct", err) + t.Error("Failed to save partial nil pointer struct", err) } var pointerStruct4 PointerStruct if err := DB.First(&pointerStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || *pointerStruct4.Name != name { - t.Errorf("Failed to query saved partial nil pointer struct", err) + t.Error("Failed to query saved partial nil pointer struct", err) } var normalStruct4 NormalStruct if err := DB.Table(tableName).First(&normalStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || normalStruct4.Name != name { - t.Errorf("Failed to query saved partial pointer struct", err) + t.Error("Failed to query saved partial pointer struct", err) } } diff --git a/preload_test.go b/preload_test.go index 4f65d1d8..7f4e7d50 100644 --- a/preload_test.go +++ b/preload_test.go @@ -1133,7 +1133,7 @@ func TestNilPointerSlice(t *testing.T) { } if len(got) != 2 { - t.Error("got %v items, expected 2", len(got)) + t.Errorf("got %v items, expected 2", len(got)) } if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) { diff --git a/scope.go b/scope.go index f7364e3d..0d6602b5 100644 --- a/scope.go +++ b/scope.go @@ -17,7 +17,7 @@ type Scope struct { SqlVars []interface{} db *DB indirectValue *reflect.Value - instanceId string + instanceID string primaryKeyField *Field skipLeft bool fields map[string]*Field @@ -83,9 +83,9 @@ func (scope *Scope) Quote(str string) string { newStrs = append(newStrs, scope.Dialect().Quote(str)) } return strings.Join(newStrs, ".") - } else { - return scope.Dialect().Quote(str) } + + return scope.Dialect().Quote(str) } func (scope *Scope) QuoteIfPossible(str string) string { @@ -251,10 +251,10 @@ func (scope *Scope) AddToVars(value interface{}) string { exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1) } return exp - } else { - scope.SqlVars = append(scope.SqlVars, value) - return scope.Dialect().BinVar(len(scope.SqlVars)) } + + scope.SqlVars = append(scope.SqlVars, value) + return scope.Dialect().BinVar(len(scope.SqlVars)) } type tabler interface { @@ -289,9 +289,9 @@ func (scope *Scope) QuotedTableName() (name string) { return scope.Search.tableName } return scope.Quote(scope.Search.tableName) - } else { - return scope.Quote(scope.TableName()) } + + return scope.Quote(scope.TableName()) } // CombinedConditionSql get combined condition sql @@ -341,20 +341,20 @@ func (scope *Scope) Get(name string) (interface{}, bool) { return scope.db.Get(name) } -// InstanceId get InstanceId for scope -func (scope *Scope) InstanceId() string { - if scope.instanceId == "" { - scope.instanceId = fmt.Sprintf("%v%v", &scope, &scope.db) +// InstanceID get InstanceID for scope +func (scope *Scope) InstanceID() string { + if scope.instanceID == "" { + scope.instanceID = fmt.Sprintf("%v%v", &scope, &scope.db) } - return scope.instanceId + return scope.instanceID } func (scope *Scope) InstanceSet(name string, value interface{}) *Scope { - return scope.Set(name+scope.InstanceId(), value) + return scope.Set(name+scope.InstanceID(), value) } func (scope *Scope) InstanceGet(name string) (interface{}, bool) { - return scope.Get(name + scope.InstanceId()) + return scope.Get(name + scope.InstanceID()) } // Begin start a transaction diff --git a/scope_private.go b/scope_private.go index a4d53366..a9288bb2 100644 --- a/scope_private.go +++ b/scope_private.go @@ -596,7 +596,7 @@ func (scope *Scope) createJoinTable(field *StructField) { func (scope *Scope) createTable() *Scope { var tags []string var primaryKeys []string - var primaryKeyInColumnType bool = false + var primaryKeyInColumnType = false for _, field := range scope.GetStructFields() { if field.IsNormal { sqlTag := scope.generateSqlTag(field) diff --git a/utils.go b/utils.go index 158d8a3b..d47065bf 100644 --- a/utils.go +++ b/utils.go @@ -41,11 +41,11 @@ func newSafeMap() *safeMap { var smap = newSafeMap() -type Case bool +type strCase bool const ( - lower Case = false - upper Case = true + lower strCase = false + upper strCase = true ) func ToDBName(name string) string { @@ -56,7 +56,7 @@ func ToDBName(name string) string { var ( value = commonInitialismsReplacer.Replace(name) buf = bytes.NewBufferString("") - lastCase, currCase, nextCase Case + lastCase, currCase, nextCase strCase ) for i, v := range value[:len(value)-1] {