change the method of initializing slice (#4097)

* change the method of initializing slice and fixed the length to be specified as 0

* keep the association.go code in the var group

* keep the association.go code in the var group

* change to initializing in var group
This commit is contained in:
Sivchari 2021-03-04 20:44:15 +09:00 committed by GitHub
parent 664755270d
commit adf85d5b82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 12 deletions

View File

@ -39,7 +39,7 @@ func SaveBeforeAssociations(create bool) func(db *gorm.DB) {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
var (
objs []reflect.Value
objs = make([]reflect.Value, 0, db.Statement.ReflectValue.Len())
fieldType = rel.Field.FieldType
isPtr = fieldType.Kind() == reflect.Ptr
)
@ -140,7 +140,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
}
if elems.Len() > 0 {
assignmentColumns := []string{}
assignmentColumns := make([]string, 0, len(rel.References))
for _, ref := range rel.References {
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
}
@ -154,7 +154,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
f = f.Addr()
}
assignmentColumns := []string{}
assignmentColumns := make([]string, 0, len(rel.References))
for _, ref := range rel.References {
if ref.OwnPrimaryKey {
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.ReflectValue)
@ -219,7 +219,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
}
if elems.Len() > 0 {
assignmentColumns := []string{}
assignmentColumns := make([]string, 0, len(rel.References))
for _, ref := range rel.References {
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
}
@ -324,7 +324,7 @@ func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[
}
if len(defaultUpdatingColumns) > 0 {
var columns []clause.Column
columns := make([]clause.Column, 0, len(s.PrimaryFieldDBNames))
for _, dbName := range s.PrimaryFieldDBNames {
columns = append(columns, clause.Column{Name: dbName})
}

View File

@ -41,7 +41,7 @@ func DeleteBeforeAssociations(db *gorm.DB) {
}
if len(db.Statement.Selects) > 0 {
var selects []string
selects := make([]string, 0, len(db.Statement.Selects))
for _, s := range db.Statement.Selects {
if s == clause.Associations {
selects = append(selects, s)
@ -69,9 +69,9 @@ func DeleteBeforeAssociations(db *gorm.DB) {
}
case schema.Many2Many:
var (
queryConds []clause.Expression
foreignFields []*schema.Field
relForeignKeys []string
queryConds = make([]clause.Expression, 0, len(rel.References))
foreignFields = make([]*schema.Field, 0, len(rel.References))
relForeignKeys = make([]string, 0, len(rel.References))
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
table = rel.JoinTable.Table
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)

View File

@ -27,8 +27,13 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
})
if rel.JoinTable != nil {
var joinForeignFields, joinRelForeignFields []*schema.Field
var joinForeignKeys []string
var (
joinForeignFields = make([]*schema.Field, 0, len(rel.References))
joinRelForeignFields = make([]*schema.Field, 0, len(rel.References))
joinForeignKeys = make([]string, 0, len(rel.References))
)
for _, ref := range rel.References {
if ref.OwnPrimaryKey {
joinForeignKeys = append(joinForeignKeys, ref.ForeignKey.DBName)

View File

@ -92,7 +92,7 @@ var (
)
func init() {
var commonInitialismsForReplacer []string
commonInitialismsForReplacer := make([]string, 0, len(commonInitialisms))
for _, initialism := range commonInitialisms {
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
}