From 380cc64ff5b3f5379a076b19b23ed0ddd1638ba7 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Fri, 10 Dec 2021 17:45:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=20type=20alias=20AutoMigrate=20bug=EF=BC=88?= =?UTF-8?q?Add=20Test=20Case=EF=BC=89=20(#4888)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix type alias AutoMigrate bug. eg ```go package main type IDer interface{ GetID() int64 } // ID will add some method to implement some interface eg: GetID type ID int64 func (z ID) GetID() int64 { return int64(z) } type Test struct { ID Code string `gorm:"size:50"` Name string `gorm:"size:50"` } func main() { db, err := gorm.Open(postgres.New(postgres.Config{ DSN: `dsn`, PreferSimpleProtocol: false, }), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), SkipDefaultTransaction: true, }) if err != nil { log.Fatal(err) } if err = db.AutoMigrate(&Test{}); err != nil { // invalid embedded struct for Test's field ID, should be struct, but got main.ID log.Fatal(err) } } ``` * fix type alias AutoMigrate bug. eg ```go package main type IDer interface{ GetID() int64 } // ID will add some method to implement some interface eg: GetID type ID int64 func (z ID) GetID() int64 { return int64(z) } type Test struct { ID Code string `gorm:"size:50"` Name string `gorm:"size:50"` } func main() { db, err := gorm.Open(postgres.New(postgres.Config{ DSN: `dsn`, PreferSimpleProtocol: false, }), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), SkipDefaultTransaction: true, }) if err != nil { log.Fatal(err) } if err = db.AutoMigrate(&Test{}); err != nil { // invalid embedded struct for Test's field ID, should be struct, but got main.ID log.Fatal(err) } } ``` * Add typealis test. * try to fix golangci-lint --- schema/field.go | 7 +++-- schema/field_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/schema/field.go b/schema/field.go index f3189c7a..c6c89cc1 100644 --- a/schema/field.go +++ b/schema/field.go @@ -347,7 +347,9 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } if _, ok := field.TagSettings["EMBEDDED"]; ok || (fieldStruct.Anonymous && !isValuer && (field.Creatable || field.Updatable || field.Readable)) { - if reflect.Indirect(fieldValue).Kind() == reflect.Struct { + kind := reflect.Indirect(fieldValue).Kind() + switch kind { + case reflect.Struct: var err error field.Creatable = false field.Updatable = false @@ -396,7 +398,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { ef.TagSettings[k] = v } } - } else { + case reflect.Invalid, reflect.Uintptr, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, + reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer, reflect.Complex64, reflect.Complex128: schema.err = fmt.Errorf("invalid embedded struct for %s's field %s, should be struct, but got %v", field.Schema.Name, field.Name, field.FieldType) } } diff --git a/schema/field_test.go b/schema/field_test.go index 4be3e5ab..8768a4c3 100644 --- a/schema/field_test.go +++ b/schema/field_test.go @@ -244,7 +244,7 @@ func TestParseFieldWithPermission(t *testing.T) { t.Fatalf("Failed to parse user with permission, got error %v", err) } - fields := []schema.Field{ + fields := []*schema.Field{ {Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, Creatable: true, Updatable: true, Readable: true, HasDefaultValue: true, AutoIncrement: true}, {Name: "Name", DBName: "", BindNames: []string{"Name"}, DataType: "", Tag: `gorm:"-"`, Creatable: false, Updatable: false, Readable: false}, {Name: "Name2", DBName: "name2", BindNames: []string{"Name2"}, DataType: schema.String, Tag: `gorm:"->"`, Creatable: false, Updatable: false, Readable: true}, @@ -257,6 +257,68 @@ func TestParseFieldWithPermission(t *testing.T) { } for _, f := range fields { - checkSchemaField(t, user, &f, func(f *schema.Field) {}) + checkSchemaField(t, user, f, func(f *schema.Field) {}) } } + +type ID int64 +type INT int +type INT8 int8 +type INT16 int16 +type INT32 int32 +type INT64 int64 +type UINT uint +type UINT8 uint8 +type UINT16 uint16 +type UINT32 uint32 +type UINT64 uint64 +type FLOAT32 float32 +type FLOAT64 float64 +type BOOL bool +type STRING string +type TypeAlias struct { + ID + INT `gorm:"column:fint"` + INT8 `gorm:"column:fint8"` + INT16 `gorm:"column:fint16"` + INT32 `gorm:"column:fint32"` + INT64 `gorm:"column:fint64"` + UINT `gorm:"column:fuint"` + UINT8 `gorm:"column:fuint8"` + UINT16 `gorm:"column:fuint16"` + UINT32 `gorm:"column:fuint32"` + UINT64 `gorm:"column:fuint64"` + FLOAT32 `gorm:"column:ffloat32"` + FLOAT64 `gorm:"column:ffloat64"` + BOOL `gorm:"column:fbool"` + STRING `gorm:"column:fstring"` +} + +func TestTypeAliasField(t *testing.T){ + alias, err := schema.Parse(&TypeAlias{}, &sync.Map{}, schema.NamingStrategy{}) + if err != nil { + t.Fatalf("Failed to parse TypeAlias with permission, got error %v", err) + } + + fields := []*schema.Field{ + {Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 64, PrimaryKey: true, HasDefaultValue: true, AutoIncrement: true }, + {Name: "INT", DBName: "fint", BindNames: []string{"INT"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fint"`}, + {Name: "INT8", DBName: "fint8", BindNames: []string{"INT8"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 8, Tag: `gorm:"column:fint8"`}, + {Name: "INT16", DBName: "fint16", BindNames: []string{"INT16"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 16, Tag: `gorm:"column:fint16"`}, + {Name: "INT32", DBName: "fint32", BindNames: []string{"INT32"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:fint32"`}, + {Name: "INT64", DBName: "fint64", BindNames: []string{"INT64"}, DataType: schema.Int , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fint64"`}, + {Name: "UINT", DBName: "fuint", BindNames: []string{"UINT"}, DataType: schema.Uint , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fuint"`}, + {Name: "UINT8", DBName: "fuint8", BindNames: []string{"UINT8"}, DataType: schema.Uint , Creatable: true, Updatable: true, Readable: true, Size: 8, Tag: `gorm:"column:fuint8"`}, + {Name: "UINT16", DBName: "fuint16", BindNames: []string{"UINT16"}, DataType: schema.Uint , Creatable: true, Updatable: true, Readable: true, Size: 16, Tag: `gorm:"column:fuint16"`}, + {Name: "UINT32", DBName: "fuint32", BindNames: []string{"UINT32"}, DataType: schema.Uint , Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:fuint32"`}, + {Name: "UINT64", DBName: "fuint64", BindNames: []string{"UINT64"}, DataType: schema.Uint , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fuint64"`}, + {Name: "FLOAT32", DBName: "ffloat32", BindNames: []string{"FLOAT32"}, DataType: schema.Float , Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:ffloat32"`}, + {Name: "FLOAT64", DBName: "ffloat64", BindNames: []string{"FLOAT64"}, DataType: schema.Float , Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:ffloat64"`}, + {Name: "BOOL", DBName: "fbool", BindNames: []string{"BOOL"}, DataType: schema.Bool , Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbool"`}, + {Name: "STRING", DBName: "fstring", BindNames: []string{"STRING"}, DataType: schema.String, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fstring"`}, + } + + for _, f := range fields { + checkSchemaField(t, alias, f, func(f *schema.Field) {}) + } +} \ No newline at end of file