Skip to content

Commit

Permalink
Fix some go vet/lint reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 15, 2016
1 parent 551c1e0 commit 8d716be
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 59 deletions.
5 changes: 2 additions & 3 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "?")
}

Expand All @@ -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{}) {
Expand Down
6 changes: 3 additions & 3 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions join_table_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 13 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion multi_primary_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion preload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 15 additions & 15 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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] {
Expand Down

0 comments on commit 8d716be

Please sign in to comment.