Skip to content

Commit 8d716be

Browse files
committed
Fix some go vet/lint reports
1 parent 551c1e0 commit 8d716be

12 files changed

+55
-59
lines changed

association.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func toQueryMarks(primaryValues [][]interface{}) string {
375375

376376
for _, primaryValue := range primaryValues {
377377
var marks []string
378-
for _, _ = range primaryValue {
378+
for _ = range primaryValue {
379379
marks = append(marks, "?")
380380
}
381381

@@ -396,9 +396,8 @@ func toQueryCondition(scope *Scope, columns []string) string {
396396

397397
if len(columns) > 1 {
398398
return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
399-
} else {
400-
return strings.Join(newColumns, ",")
401399
}
400+
return strings.Join(newColumns, ",")
402401
}
403402

404403
func toQueryValues(primaryValues [][]interface{}) (values []interface{}) {

association_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestBelongsTo(t *testing.T) {
1616
}
1717

1818
if err := DB.Save(&post).Error; err != nil {
19-
t.Errorf("Got errors when save post", err.Error())
19+
t.Error("Got errors when save post", err)
2020
}
2121

2222
if post.Category.ID == 0 || post.MainCategory.ID == 0 {
@@ -184,7 +184,7 @@ func TestHasOne(t *testing.T) {
184184
}
185185

186186
if err := DB.Save(&user).Error; err != nil {
187-
t.Errorf("Got errors when save user", err.Error())
187+
t.Error("Got errors when save user", err.Error())
188188
}
189189

190190
if user.CreditCard.UserId.Int64 == 0 {
@@ -331,7 +331,7 @@ func TestHasMany(t *testing.T) {
331331
}
332332

333333
if err := DB.Save(&post).Error; err != nil {
334-
t.Errorf("Got errors when save post", err.Error())
334+
t.Error("Got errors when save post", err)
335335
}
336336

337337
for _, comment := range post.Comments {

join_table_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ func (s JoinTableHandler) JoinWith(handler JoinTableHandlerInterface, db *DB, so
173173

174174
return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTableName, strings.Join(joinConditions, " AND "))).
175175
Where(condString, toQueryValues(foreignFieldValues)...)
176-
} else {
177-
db.Error = errors.New("wrong source type for join table handler")
178-
return db
179176
}
177+
178+
db.Error = errors.New("wrong source type for join table handler")
179+
return db
180180
}

main.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func (s *DB) New() *DB {
9898
}
9999

100100
// NewScope create scope for callbacks, including DB's search information
101-
func (db *DB) NewScope(value interface{}) *Scope {
102-
dbClone := db.clone()
101+
func (s *DB) NewScope(value interface{}) *Scope {
102+
dbClone := s.clone()
103103
dbClone.Value = value
104104
return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value}
105105
}
@@ -311,9 +311,9 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB {
311311

312312
func (s *DB) Exec(sql string, values ...interface{}) *DB {
313313
scope := s.clone().NewScope(nil)
314-
generatedSql := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
315-
generatedSql = strings.TrimSuffix(strings.TrimPrefix(generatedSql, "("), ")")
316-
scope.Raw(generatedSql)
314+
generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
315+
generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
316+
scope.Raw(generatedSQL)
317317
return scope.Exec().db
318318
}
319319

@@ -372,15 +372,16 @@ func (s *DB) RecordNotFound() bool {
372372
return s.Error == RecordNotFound
373373
}
374374

375-
// Migrations
376-
func (s *DB) CreateTable(values ...interface{}) *DB {
375+
// CreateTable create table for models
376+
func (s *DB) CreateTable(models ...interface{}) *DB {
377377
db := s.clone()
378-
for _, value := range values {
379-
db = db.NewScope(value).createTable().db
378+
for _, model := range models {
379+
db = db.NewScope(model).createTable().db
380380
}
381381
return db
382382
}
383383

384+
// DropTable drop table for models
384385
func (s *DB) DropTable(values ...interface{}) *DB {
385386
db := s.clone()
386387
for _, value := range values {
@@ -393,6 +394,7 @@ func (s *DB) DropTable(values ...interface{}) *DB {
393394
return db
394395
}
395396

397+
// DropTableIfExists drop table for models only when it exists
396398
func (s *DB) DropTableIfExists(values ...interface{}) *DB {
397399
db := s.clone()
398400
for _, value := range values {
@@ -459,12 +461,8 @@ func (s *DB) CurrentDatabase() string {
459461
return name
460462
}
461463

462-
/*
463-
Add foreign key to the given scope
464-
465-
Example:
466-
db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
467-
*/
464+
// AddForeignKey Add foreign key to the given scope
465+
// Example: db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
468466
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
469467
scope := s.clone().NewScope(s.Value)
470468
scope.addForeignKey(field, dest, onDelete, onUpdate)

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestSetTable(t *testing.T) {
115115
DB.Create(getPreparedUser("pluck_user3", "pluck_user"))
116116

117117
if err := DB.Table("users").Where("role = ?", "pluck_user").Pluck("age", &[]int{}).Error; err != nil {
118-
t.Errorf("No errors should happen if set table for pluck", err.Error())
118+
t.Error("No errors should happen if set table for pluck", err)
119119
}
120120

121121
var users []User
@@ -545,7 +545,7 @@ func TestTimeWithZone(t *testing.T) {
545545
DB.First(&findUser, "name = ?", name)
546546
foundBirthday = findUser.Birthday.UTC().Format(format)
547547
if foundBirthday != expectedBirthday {
548-
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)
548+
t.Errorf("User's birthday should not be changed after find for name=%s, expected bday=%+v but actual value=%+v", name, expectedBirthday, foundBirthday)
549549
}
550550

551551
if DB.Where("id = ? AND birthday >= ?", findUser.Id, user.Birthday.Add(-time.Minute)).First(&findUser2).RecordNotFound() {

model_struct.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,8 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
560560

561561
if strings.TrimSpace(additionalType) == "" {
562562
return sqlType
563-
} else {
564-
return fmt.Sprintf("%v %v", sqlType, additionalType)
565563
}
564+
return fmt.Sprintf("%v %v", sqlType, additionalType)
566565
}
567566

568567
func parseTagSetting(tags reflect.StructTag) map[string]string {

multi_primary_keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Tag struct {
2121
ID uint `gorm:"primary_key"`
2222
Locale string `gorm:"primary_key"`
2323
Value string
24-
Blogs []*Blog `gorm:"many2many:"blogs_tags`
24+
Blogs []*Blog `gorm:"many2many:blogs_tags"`
2525
}
2626

2727
func compareTags(tags []Tag, contents []string) bool {

pointer_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,46 @@ func TestPointerFields(t *testing.T) {
3939

4040
var nilPointerStruct = PointerStruct{}
4141
if err := DB.Create(&nilPointerStruct).Error; err != nil {
42-
t.Errorf("Failed to save nil pointer struct", err)
42+
t.Error("Failed to save nil pointer struct", err)
4343
}
4444

4545
var pointerStruct2 PointerStruct
4646
if err := DB.First(&pointerStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil {
47-
t.Errorf("Failed to query saved nil pointer struct", err)
47+
t.Error("Failed to query saved nil pointer struct", err)
4848
}
4949

5050
var normalStruct2 NormalStruct
5151
if err := DB.Table(tableName).First(&normalStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil {
52-
t.Errorf("Failed to query saved nil pointer struct", err)
52+
t.Error("Failed to query saved nil pointer struct", err)
5353
}
5454

5555
var partialNilPointerStruct1 = PointerStruct{Num: &num}
5656
if err := DB.Create(&partialNilPointerStruct1).Error; err != nil {
57-
t.Errorf("Failed to save partial nil pointer struct", err)
57+
t.Error("Failed to save partial nil pointer struct", err)
5858
}
5959

6060
var pointerStruct3 PointerStruct
6161
if err := DB.First(&pointerStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || *pointerStruct3.Num != num {
62-
t.Errorf("Failed to query saved partial nil pointer struct", err)
62+
t.Error("Failed to query saved partial nil pointer struct", err)
6363
}
6464

6565
var normalStruct3 NormalStruct
6666
if err := DB.Table(tableName).First(&normalStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || normalStruct3.Num != num {
67-
t.Errorf("Failed to query saved partial pointer struct", err)
67+
t.Error("Failed to query saved partial pointer struct", err)
6868
}
6969

7070
var partialNilPointerStruct2 = PointerStruct{Name: &name}
7171
if err := DB.Create(&partialNilPointerStruct2).Error; err != nil {
72-
t.Errorf("Failed to save partial nil pointer struct", err)
72+
t.Error("Failed to save partial nil pointer struct", err)
7373
}
7474

7575
var pointerStruct4 PointerStruct
7676
if err := DB.First(&pointerStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || *pointerStruct4.Name != name {
77-
t.Errorf("Failed to query saved partial nil pointer struct", err)
77+
t.Error("Failed to query saved partial nil pointer struct", err)
7878
}
7979

8080
var normalStruct4 NormalStruct
8181
if err := DB.Table(tableName).First(&normalStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || normalStruct4.Name != name {
82-
t.Errorf("Failed to query saved partial pointer struct", err)
82+
t.Error("Failed to query saved partial pointer struct", err)
8383
}
8484
}

preload_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ func TestNilPointerSlice(t *testing.T) {
11331133
}
11341134

11351135
if len(got) != 2 {
1136-
t.Error("got %v items, expected 2", len(got))
1136+
t.Errorf("got %v items, expected 2", len(got))
11371137
}
11381138

11391139
if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) {

scope.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Scope struct {
1717
SqlVars []interface{}
1818
db *DB
1919
indirectValue *reflect.Value
20-
instanceId string
20+
instanceID string
2121
primaryKeyField *Field
2222
skipLeft bool
2323
fields map[string]*Field
@@ -83,9 +83,9 @@ func (scope *Scope) Quote(str string) string {
8383
newStrs = append(newStrs, scope.Dialect().Quote(str))
8484
}
8585
return strings.Join(newStrs, ".")
86-
} else {
87-
return scope.Dialect().Quote(str)
8886
}
87+
88+
return scope.Dialect().Quote(str)
8989
}
9090

9191
func (scope *Scope) QuoteIfPossible(str string) string {
@@ -251,10 +251,10 @@ func (scope *Scope) AddToVars(value interface{}) string {
251251
exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
252252
}
253253
return exp
254-
} else {
255-
scope.SqlVars = append(scope.SqlVars, value)
256-
return scope.Dialect().BinVar(len(scope.SqlVars))
257254
}
255+
256+
scope.SqlVars = append(scope.SqlVars, value)
257+
return scope.Dialect().BinVar(len(scope.SqlVars))
258258
}
259259

260260
type tabler interface {
@@ -289,9 +289,9 @@ func (scope *Scope) QuotedTableName() (name string) {
289289
return scope.Search.tableName
290290
}
291291
return scope.Quote(scope.Search.tableName)
292-
} else {
293-
return scope.Quote(scope.TableName())
294292
}
293+
294+
return scope.Quote(scope.TableName())
295295
}
296296

297297
// CombinedConditionSql get combined condition sql
@@ -341,20 +341,20 @@ func (scope *Scope) Get(name string) (interface{}, bool) {
341341
return scope.db.Get(name)
342342
}
343343

344-
// InstanceId get InstanceId for scope
345-
func (scope *Scope) InstanceId() string {
346-
if scope.instanceId == "" {
347-
scope.instanceId = fmt.Sprintf("%v%v", &scope, &scope.db)
344+
// InstanceID get InstanceID for scope
345+
func (scope *Scope) InstanceID() string {
346+
if scope.instanceID == "" {
347+
scope.instanceID = fmt.Sprintf("%v%v", &scope, &scope.db)
348348
}
349-
return scope.instanceId
349+
return scope.instanceID
350350
}
351351

352352
func (scope *Scope) InstanceSet(name string, value interface{}) *Scope {
353-
return scope.Set(name+scope.InstanceId(), value)
353+
return scope.Set(name+scope.InstanceID(), value)
354354
}
355355

356356
func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
357-
return scope.Get(name + scope.InstanceId())
357+
return scope.Get(name + scope.InstanceID())
358358
}
359359

360360
// Begin start a transaction

scope_private.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ func (scope *Scope) createJoinTable(field *StructField) {
596596
func (scope *Scope) createTable() *Scope {
597597
var tags []string
598598
var primaryKeys []string
599-
var primaryKeyInColumnType bool = false
599+
var primaryKeyInColumnType = false
600600
for _, field := range scope.GetStructFields() {
601601
if field.IsNormal {
602602
sqlTag := scope.generateSqlTag(field)

utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ func newSafeMap() *safeMap {
4141

4242
var smap = newSafeMap()
4343

44-
type Case bool
44+
type strCase bool
4545

4646
const (
47-
lower Case = false
48-
upper Case = true
47+
lower strCase = false
48+
upper strCase = true
4949
)
5050

5151
func ToDBName(name string) string {
@@ -56,7 +56,7 @@ func ToDBName(name string) string {
5656
var (
5757
value = commonInitialismsReplacer.Replace(name)
5858
buf = bytes.NewBufferString("")
59-
lastCase, currCase, nextCase Case
59+
lastCase, currCase, nextCase strCase
6060
)
6161

6262
for i, v := range value[:len(value)-1] {

0 commit comments

Comments
 (0)