mirror of https://github.com/go-gorm/gorm.git
Fix some go vet/lint reports
This commit is contained in:
parent
551c1e0c20
commit
8d716be896
|
@ -375,7 +375,7 @@ func toQueryMarks(primaryValues [][]interface{}) string {
|
||||||
|
|
||||||
for _, primaryValue := range primaryValues {
|
for _, primaryValue := range primaryValues {
|
||||||
var marks []string
|
var marks []string
|
||||||
for _, _ = range primaryValue {
|
for _ = range primaryValue {
|
||||||
marks = append(marks, "?")
|
marks = append(marks, "?")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -396,9 +396,8 @@ func toQueryCondition(scope *Scope, columns []string) string {
|
||||||
|
|
||||||
if len(columns) > 1 {
|
if len(columns) > 1 {
|
||||||
return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
|
return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
|
||||||
} else {
|
|
||||||
return strings.Join(newColumns, ",")
|
|
||||||
}
|
}
|
||||||
|
return strings.Join(newColumns, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func toQueryValues(primaryValues [][]interface{}) (values []interface{}) {
|
func toQueryValues(primaryValues [][]interface{}) (values []interface{}) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ func TestBelongsTo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := DB.Save(&post).Error; err != nil {
|
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 {
|
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 {
|
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 {
|
if user.CreditCard.UserId.Int64 == 0 {
|
||||||
|
@ -331,7 +331,7 @@ func TestHasMany(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := DB.Save(&post).Error; err != nil {
|
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 {
|
for _, comment := range post.Comments {
|
||||||
|
|
|
@ -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 "))).
|
return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTableName, strings.Join(joinConditions, " AND "))).
|
||||||
Where(condString, toQueryValues(foreignFieldValues)...)
|
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
28
main.go
|
@ -98,8 +98,8 @@ func (s *DB) New() *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewScope create scope for callbacks, including DB's search information
|
// NewScope create scope for callbacks, including DB's search information
|
||||||
func (db *DB) NewScope(value interface{}) *Scope {
|
func (s *DB) NewScope(value interface{}) *Scope {
|
||||||
dbClone := db.clone()
|
dbClone := s.clone()
|
||||||
dbClone.Value = value
|
dbClone.Value = value
|
||||||
return &Scope{db: dbClone, Search: dbClone.search.clone(), 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 {
|
func (s *DB) Exec(sql string, values ...interface{}) *DB {
|
||||||
scope := s.clone().NewScope(nil)
|
scope := s.clone().NewScope(nil)
|
||||||
generatedSql := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
|
generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
|
||||||
generatedSql = strings.TrimSuffix(strings.TrimPrefix(generatedSql, "("), ")")
|
generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
|
||||||
scope.Raw(generatedSql)
|
scope.Raw(generatedSQL)
|
||||||
return scope.Exec().db
|
return scope.Exec().db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,15 +372,16 @@ func (s *DB) RecordNotFound() bool {
|
||||||
return s.Error == RecordNotFound
|
return s.Error == RecordNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migrations
|
// CreateTable create table for models
|
||||||
func (s *DB) CreateTable(values ...interface{}) *DB {
|
func (s *DB) CreateTable(models ...interface{}) *DB {
|
||||||
db := s.clone()
|
db := s.clone()
|
||||||
for _, value := range values {
|
for _, model := range models {
|
||||||
db = db.NewScope(value).createTable().db
|
db = db.NewScope(model).createTable().db
|
||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DropTable drop table for models
|
||||||
func (s *DB) DropTable(values ...interface{}) *DB {
|
func (s *DB) DropTable(values ...interface{}) *DB {
|
||||||
db := s.clone()
|
db := s.clone()
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
|
@ -393,6 +394,7 @@ func (s *DB) DropTable(values ...interface{}) *DB {
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DropTableIfExists drop table for models only when it exists
|
||||||
func (s *DB) DropTableIfExists(values ...interface{}) *DB {
|
func (s *DB) DropTableIfExists(values ...interface{}) *DB {
|
||||||
db := s.clone()
|
db := s.clone()
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
|
@ -459,12 +461,8 @@ func (s *DB) CurrentDatabase() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// AddForeignKey Add foreign key to the given scope
|
||||||
Add foreign key to the given scope
|
// Example: db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
|
||||||
|
|
||||||
Example:
|
|
||||||
db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
|
|
||||||
*/
|
|
||||||
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
|
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
|
||||||
scope := s.clone().NewScope(s.Value)
|
scope := s.clone().NewScope(s.Value)
|
||||||
scope.addForeignKey(field, dest, onDelete, onUpdate)
|
scope.addForeignKey(field, dest, onDelete, onUpdate)
|
||||||
|
|
|
@ -115,7 +115,7 @@ func TestSetTable(t *testing.T) {
|
||||||
DB.Create(getPreparedUser("pluck_user3", "pluck_user"))
|
DB.Create(getPreparedUser("pluck_user3", "pluck_user"))
|
||||||
|
|
||||||
if err := DB.Table("users").Where("role = ?", "pluck_user").Pluck("age", &[]int{}).Error; err != nil {
|
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
|
var users []User
|
||||||
|
@ -545,7 +545,7 @@ func TestTimeWithZone(t *testing.T) {
|
||||||
DB.First(&findUser, "name = ?", name)
|
DB.First(&findUser, "name = ?", name)
|
||||||
foundBirthday = findUser.Birthday.UTC().Format(format)
|
foundBirthday = findUser.Birthday.UTC().Format(format)
|
||||||
if foundBirthday != expectedBirthday {
|
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() {
|
if DB.Where("id = ? AND birthday >= ?", findUser.Id, user.Birthday.Add(-time.Minute)).First(&findUser2).RecordNotFound() {
|
||||||
|
|
|
@ -560,9 +560,8 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
|
||||||
|
|
||||||
if strings.TrimSpace(additionalType) == "" {
|
if strings.TrimSpace(additionalType) == "" {
|
||||||
return sqlType
|
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 {
|
func parseTagSetting(tags reflect.StructTag) map[string]string {
|
||||||
|
|
|
@ -21,7 +21,7 @@ type Tag struct {
|
||||||
ID uint `gorm:"primary_key"`
|
ID uint `gorm:"primary_key"`
|
||||||
Locale string `gorm:"primary_key"`
|
Locale string `gorm:"primary_key"`
|
||||||
Value string
|
Value string
|
||||||
Blogs []*Blog `gorm:"many2many:"blogs_tags`
|
Blogs []*Blog `gorm:"many2many:blogs_tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareTags(tags []Tag, contents []string) bool {
|
func compareTags(tags []Tag, contents []string) bool {
|
||||||
|
|
|
@ -39,46 +39,46 @@ func TestPointerFields(t *testing.T) {
|
||||||
|
|
||||||
var nilPointerStruct = PointerStruct{}
|
var nilPointerStruct = PointerStruct{}
|
||||||
if err := DB.Create(&nilPointerStruct).Error; err != nil {
|
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
|
var pointerStruct2 PointerStruct
|
||||||
if err := DB.First(&pointerStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil {
|
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
|
var normalStruct2 NormalStruct
|
||||||
if err := DB.Table(tableName).First(&normalStruct2, "id = ?", nilPointerStruct.ID).Error; err != nil {
|
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}
|
var partialNilPointerStruct1 = PointerStruct{Num: &num}
|
||||||
if err := DB.Create(&partialNilPointerStruct1).Error; err != nil {
|
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
|
var pointerStruct3 PointerStruct
|
||||||
if err := DB.First(&pointerStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || *pointerStruct3.Num != num {
|
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
|
var normalStruct3 NormalStruct
|
||||||
if err := DB.Table(tableName).First(&normalStruct3, "id = ?", partialNilPointerStruct1.ID).Error; err != nil || normalStruct3.Num != num {
|
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}
|
var partialNilPointerStruct2 = PointerStruct{Name: &name}
|
||||||
if err := DB.Create(&partialNilPointerStruct2).Error; err != nil {
|
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
|
var pointerStruct4 PointerStruct
|
||||||
if err := DB.First(&pointerStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || *pointerStruct4.Name != name {
|
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
|
var normalStruct4 NormalStruct
|
||||||
if err := DB.Table(tableName).First(&normalStruct4, "id = ?", partialNilPointerStruct2.ID).Error; err != nil || normalStruct4.Name != name {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1133,7 +1133,7 @@ func TestNilPointerSlice(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(got) != 2 {
|
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) {
|
if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) {
|
||||||
|
|
30
scope.go
30
scope.go
|
@ -17,7 +17,7 @@ type Scope struct {
|
||||||
SqlVars []interface{}
|
SqlVars []interface{}
|
||||||
db *DB
|
db *DB
|
||||||
indirectValue *reflect.Value
|
indirectValue *reflect.Value
|
||||||
instanceId string
|
instanceID string
|
||||||
primaryKeyField *Field
|
primaryKeyField *Field
|
||||||
skipLeft bool
|
skipLeft bool
|
||||||
fields map[string]*Field
|
fields map[string]*Field
|
||||||
|
@ -83,9 +83,9 @@ func (scope *Scope) Quote(str string) string {
|
||||||
newStrs = append(newStrs, scope.Dialect().Quote(str))
|
newStrs = append(newStrs, scope.Dialect().Quote(str))
|
||||||
}
|
}
|
||||||
return strings.Join(newStrs, ".")
|
return strings.Join(newStrs, ".")
|
||||||
} else {
|
|
||||||
return scope.Dialect().Quote(str)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return scope.Dialect().Quote(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) QuoteIfPossible(str string) string {
|
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)
|
exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
|
||||||
}
|
}
|
||||||
return exp
|
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 {
|
type tabler interface {
|
||||||
|
@ -289,9 +289,9 @@ func (scope *Scope) QuotedTableName() (name string) {
|
||||||
return scope.Search.tableName
|
return scope.Search.tableName
|
||||||
}
|
}
|
||||||
return scope.Quote(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
|
// CombinedConditionSql get combined condition sql
|
||||||
|
@ -341,20 +341,20 @@ func (scope *Scope) Get(name string) (interface{}, bool) {
|
||||||
return scope.db.Get(name)
|
return scope.db.Get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InstanceId get InstanceId for scope
|
// InstanceID get InstanceID for scope
|
||||||
func (scope *Scope) InstanceId() string {
|
func (scope *Scope) InstanceID() string {
|
||||||
if scope.instanceId == "" {
|
if scope.instanceID == "" {
|
||||||
scope.instanceId = fmt.Sprintf("%v%v", &scope, &scope.db)
|
scope.instanceID = fmt.Sprintf("%v%v", &scope, &scope.db)
|
||||||
}
|
}
|
||||||
return scope.instanceId
|
return scope.instanceID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) InstanceSet(name string, value interface{}) *Scope {
|
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) {
|
func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
|
||||||
return scope.Get(name + scope.InstanceId())
|
return scope.Get(name + scope.InstanceID())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin start a transaction
|
// Begin start a transaction
|
||||||
|
|
|
@ -596,7 +596,7 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
||||||
func (scope *Scope) createTable() *Scope {
|
func (scope *Scope) createTable() *Scope {
|
||||||
var tags []string
|
var tags []string
|
||||||
var primaryKeys []string
|
var primaryKeys []string
|
||||||
var primaryKeyInColumnType bool = false
|
var primaryKeyInColumnType = false
|
||||||
for _, field := range scope.GetStructFields() {
|
for _, field := range scope.GetStructFields() {
|
||||||
if field.IsNormal {
|
if field.IsNormal {
|
||||||
sqlTag := scope.generateSqlTag(field)
|
sqlTag := scope.generateSqlTag(field)
|
||||||
|
|
8
utils.go
8
utils.go
|
@ -41,11 +41,11 @@ func newSafeMap() *safeMap {
|
||||||
|
|
||||||
var smap = newSafeMap()
|
var smap = newSafeMap()
|
||||||
|
|
||||||
type Case bool
|
type strCase bool
|
||||||
|
|
||||||
const (
|
const (
|
||||||
lower Case = false
|
lower strCase = false
|
||||||
upper Case = true
|
upper strCase = true
|
||||||
)
|
)
|
||||||
|
|
||||||
func ToDBName(name string) string {
|
func ToDBName(name string) string {
|
||||||
|
@ -56,7 +56,7 @@ func ToDBName(name string) string {
|
||||||
var (
|
var (
|
||||||
value = commonInitialismsReplacer.Replace(name)
|
value = commonInitialismsReplacer.Replace(name)
|
||||||
buf = bytes.NewBufferString("")
|
buf = bytes.NewBufferString("")
|
||||||
lastCase, currCase, nextCase Case
|
lastCase, currCase, nextCase strCase
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, v := range value[:len(value)-1] {
|
for i, v := range value[:len(value)-1] {
|
||||||
|
|
Loading…
Reference in New Issue