From 0348b1d3c155b1c8b2f0ae3968a7c71be6e68ad1 Mon Sep 17 00:00:00 2001 From: Shubhendra Singh Chauhan Date: Mon, 8 Mar 2021 08:16:43 +0530 Subject: [PATCH] chore: improve code quality (#4123) * Combine multiple `append`s into a single call * Clean up copied struct fields with type conversion * Remove unnecessary use of slice --- schema/relationship.go | 4 +--- schema/utils.go | 2 +- soft_delete.go | 2 +- statement.go | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/schema/relationship.go b/schema/relationship.go index 1b93ef88..a8863bfe 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -428,9 +428,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu lookUpNames := []string{lookUpName} if len(primaryFields) == 1 { - lookUpNames = append(lookUpNames, strings.TrimSuffix(lookUpName, primaryField.Name)+"ID") - lookUpNames = append(lookUpNames, strings.TrimSuffix(lookUpName, primaryField.Name)+"Id") - lookUpNames = append(lookUpNames, schema.namer.ColumnName(foreignSchema.Table, strings.TrimSuffix(lookUpName, primaryField.Name)+"ID")) + lookUpNames = append(lookUpNames, strings.TrimSuffix(lookUpName, primaryField.Name)+"ID", strings.TrimSuffix(lookUpName, primaryField.Name)+"Id", schema.namer.ColumnName(foreignSchema.Table, strings.TrimSuffix(lookUpName, primaryField.Name)+"ID")) } for _, name := range lookUpNames { diff --git a/schema/utils.go b/schema/utils.go index 6e5fd528..d311c61b 100644 --- a/schema/utils.go +++ b/schema/utils.go @@ -142,7 +142,7 @@ func GetIdentityFieldValuesMap(reflectValue reflect.Value, fields []*Field) (map if notZero { dataKey := utils.ToStringKey(fieldValues...) if _, ok := dataResults[dataKey]; !ok { - results = append(results, fieldValues[:]) + results = append(results, fieldValues) dataResults[dataKey] = []reflect.Value{elem} } else { dataResults[dataKey] = append(dataResults[dataKey], elem) diff --git a/soft_delete.go b/soft_delete.go index bdbf03c2..b16041f1 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -129,7 +129,7 @@ func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) { if _, ok := stmt.Clauses["WHERE"]; !stmt.DB.AllowGlobalUpdate && !ok { stmt.DB.AddError(ErrMissingWhereClause) } else { - SoftDeleteQueryClause{Field: sd.Field}.ModifyStatement(stmt) + SoftDeleteQueryClause(sd).ModifyStatement(stmt) } stmt.AddClauseIfNotExists(clause.Update{}) diff --git a/statement.go b/statement.go index 7580d965..6f336799 100644 --- a/statement.go +++ b/statement.go @@ -288,7 +288,7 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) [] if where, ok := cs.Expression.(clause.Where); ok { if len(where.Exprs) == 1 { if orConds, ok := where.Exprs[0].(clause.OrConditions); ok { - where.Exprs[0] = clause.AndConditions{Exprs: orConds.Exprs} + where.Exprs[0] = clause.AndConditions(orConds) } } conds = append(conds, clause.And(where.Exprs...))