Fix some go vet/lint reports

This commit is contained in:
Jinzhu 2016-01-15 21:03:35 +08:00
parent 551c1e0c20
commit 8d716be896
12 changed files with 55 additions and 59 deletions

View File

@ -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{}) {

View File

@ -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 {

View File

@ -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
main.go
View File

@ -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)

View File

@ -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() {

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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) {

View File

@ -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

View File

@ -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)

View File

@ -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] {