enable revive linter

This commit is contained in:
Matthieu MOREL 2021-12-27 11:45:36 +01:00 committed by GitHub
parent d49308d779
commit aef818e0f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 21 deletions

View File

@ -15,6 +15,7 @@ linters:
- misspell - misspell
- nilerr - nilerr
- prealloc - prealloc
- revive
- unconvert - unconvert
- unparam - unparam
issues: issues:
@ -29,6 +30,9 @@ issues:
- linters: - linters:
- gocritic - gocritic
text: "ifElseChain" text: "ifElseChain"
- linters:
- revive
text: "exported"
linters-settings: linters-settings:
gci: gci:
local-prefixes: gorm.io/gorm local-prefixes: gorm.io/gorm

View File

@ -207,7 +207,7 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
return tx return tx
} }
func (tx *DB) assignInterfacesToValue(values ...interface{}) { func (db *DB) assignInterfacesToValue(values ...interface{}) {
for _, value := range values { for _, value := range values {
switch v := value.(type) { switch v := value.(type) {
case []clause.Expression: case []clause.Expression:
@ -215,40 +215,40 @@ func (tx *DB) assignInterfacesToValue(values ...interface{}) {
if eq, ok := expr.(clause.Eq); ok { if eq, ok := expr.(clause.Eq); ok {
switch column := eq.Column.(type) { switch column := eq.Column.(type) {
case string: case string:
if field := tx.Statement.Schema.LookUpField(column); field != nil { if field := db.Statement.Schema.LookUpField(column); field != nil {
tx.AddError(field.Set(tx.Statement.ReflectValue, eq.Value)) db.AddError(field.Set(db.Statement.ReflectValue, eq.Value))
} }
case clause.Column: case clause.Column:
if field := tx.Statement.Schema.LookUpField(column.Name); field != nil { if field := db.Statement.Schema.LookUpField(column.Name); field != nil {
tx.AddError(field.Set(tx.Statement.ReflectValue, eq.Value)) db.AddError(field.Set(db.Statement.ReflectValue, eq.Value))
} }
} }
} else if andCond, ok := expr.(clause.AndConditions); ok { } else if andCond, ok := expr.(clause.AndConditions); ok {
tx.assignInterfacesToValue(andCond.Exprs) db.assignInterfacesToValue(andCond.Exprs)
} }
} }
case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}: case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}:
if exprs := tx.Statement.BuildCondition(value); len(exprs) > 0 { if exprs := db.Statement.BuildCondition(value); len(exprs) > 0 {
tx.assignInterfacesToValue(exprs) db.assignInterfacesToValue(exprs)
} }
default: default:
if s, err := schema.Parse(value, tx.cacheStore, tx.NamingStrategy); err == nil { if s, err := schema.Parse(value, db.cacheStore, db.NamingStrategy); err == nil {
reflectValue := reflect.Indirect(reflect.ValueOf(value)) reflectValue := reflect.Indirect(reflect.ValueOf(value))
switch reflectValue.Kind() { switch reflectValue.Kind() {
case reflect.Struct: case reflect.Struct:
for _, f := range s.Fields { for _, f := range s.Fields {
if f.Readable { if f.Readable {
if v, isZero := f.ValueOf(reflectValue); !isZero { if v, isZero := f.ValueOf(reflectValue); !isZero {
if field := tx.Statement.Schema.LookUpField(f.Name); field != nil { if field := db.Statement.Schema.LookUpField(f.Name); field != nil {
tx.AddError(field.Set(tx.Statement.ReflectValue, v)) db.AddError(field.Set(db.Statement.ReflectValue, v))
} }
} }
} }
} }
} }
} else if len(values) > 0 { } else if len(values) > 0 {
if exprs := tx.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 { if exprs := db.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 {
tx.assignInterfacesToValue(exprs) db.assignInterfacesToValue(exprs)
} }
return return
} }

View File

@ -274,9 +274,8 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) { if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) {
if schema.parseRelation(field); schema.err != nil { if schema.parseRelation(field); schema.err != nil {
return schema, schema.err return schema, schema.err
} else {
schema.FieldsByName[field.Name] = field
} }
schema.FieldsByName[field.Name] = field
} }
fieldValue := reflect.New(field.IndirectFieldType) fieldValue := reflect.New(field.IndirectFieldType)

View File

@ -180,7 +180,7 @@ func TestForeignKeyConstraintsBelongsTo(t *testing.T) {
func TestFullSaveAssociations(t *testing.T) { func TestFullSaveAssociations(t *testing.T) {
coupon := &Coupon{ coupon := &Coupon{
AppliesToProduct: []*CouponProduct{ AppliesToProduct: []*CouponProduct{
{ProductId: "full-save-association-product1"}, {ProductID: "full-save-association-product1"},
}, },
AmountOff: 10, AmountOff: 10,
PercentOff: 0.0, PercentOff: 0.0,

View File

@ -58,11 +58,11 @@ func (DummyDialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteByte('`') writer.WriteByte('`')
underQuoted = true underQuoted = true
if selfQuoted = continuousBacktick > 0; selfQuoted { if selfQuoted = continuousBacktick > 0; selfQuoted {
continuousBacktick -= 1 continuousBacktick--
} }
} }
for ; continuousBacktick > 0; continuousBacktick -= 1 { for ; continuousBacktick > 0; continuousBacktick-- {
writer.WriteString("``") writer.WriteString("``")
} }

View File

@ -63,14 +63,14 @@ type Language struct {
type Coupon struct { type Coupon struct {
ID int `gorm:"primarykey; size:255"` ID int `gorm:"primarykey; size:255"`
AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"` AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponID;constraint:OnDelete:CASCADE"`
AmountOff uint32 `gorm:"amount_off"` AmountOff uint32 `gorm:"amount_off"`
PercentOff float32 `gorm:"percent_off"` PercentOff float32 `gorm:"percent_off"`
} }
type CouponProduct struct { type CouponProduct struct {
CouponId int `gorm:"primarykey;size:255"` CouponID int `gorm:"primarykey;size:255"`
ProductId string `gorm:"primarykey;size:255"` ProductID string `gorm:"primarykey;size:255"`
Desc string Desc string
} }