From 2f48ddbea8bc42c28353648894c8370421c9c7a7 Mon Sep 17 00:00:00 2001 From: black Date: Mon, 20 Feb 2023 16:26:22 +0800 Subject: [PATCH 1/4] distinguish between detected and custom type (#6033) --- schema/field.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/schema/field.go b/schema/field.go index 59151878f..224bb67d9 100644 --- a/schema/field.go +++ b/schema/field.go @@ -56,6 +56,7 @@ type Field struct { BindNames []string DataType DataType GORMDataType DataType + CustomDataType DataType PrimaryKey bool AutoIncrement bool AutoIncrementIncrement int64 @@ -310,6 +311,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { default: field.DataType = DataType(val) } + field.CustomDataType = DataType(val) } if field.Size == 0 { From 6cc0fb6f03555fce12a79bcb756382736616b593 Mon Sep 17 00:00:00 2001 From: black Date: Mon, 20 Feb 2023 16:47:59 +0800 Subject: [PATCH 2/4] add test --- schema/schema_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/schema/schema_test.go b/schema/schema_test.go index 8a752fb7b..14f023480 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -4,12 +4,38 @@ import ( "strings" "sync" "testing" + "time" "gorm.io/gorm" "gorm.io/gorm/schema" "gorm.io/gorm/utils/tests" ) +func TestParseCustomTypeSchema(t *testing.T) { + type Product struct { + Date time.Time `gorm:"type:date"` + Time time.Time `gorm:"type:time"` + DateTime time.Time + } + product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{}) + if err != nil { + t.Fatalf("failed to parse product, got err %v", err) + } + + fields := []schema.Field{ + {Name: "Date", DBName: "date", BindNames: []string{"Date"}, DataType: "date", CustomDataType: "date", Tag: `gorm:"type:date"`, TagSettings: map[string]string{"TYPE": "date"}}, + {Name: "Time", DBName: "time", BindNames: []string{"Time"}, DataType: schema.Time, CustomDataType: "time", Tag: `gorm:"type:time"`, TagSettings: map[string]string{"TYPE": "time"}}, + {Name: "DateTime", DBName: "date_time", BindNames: []string{"DateTime"}, DataType: schema.Time}, + } + for _, f := range fields { + checkSchemaField(t, product, &f, func(field *schema.Field) { + f.Creatable = true + f.Updatable = true + f.Readable = true + }) + } +} + func TestParseSchema(t *testing.T) { user, err := schema.Parse(&tests.User{}, &sync.Map{}, schema.NamingStrategy{}) if err != nil { From 08e9ba73a82bd96cd9a0675189e48b77db138372 Mon Sep 17 00:00:00 2001 From: black Date: Mon, 20 Feb 2023 18:05:49 +0800 Subject: [PATCH 3/4] fix for golangci-lint --- schema/schema_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/schema/schema_test.go b/schema/schema_test.go index 14f023480..a18505534 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -27,11 +27,11 @@ func TestParseCustomTypeSchema(t *testing.T) { {Name: "Time", DBName: "time", BindNames: []string{"Time"}, DataType: schema.Time, CustomDataType: "time", Tag: `gorm:"type:time"`, TagSettings: map[string]string{"TYPE": "time"}}, {Name: "DateTime", DBName: "date_time", BindNames: []string{"DateTime"}, DataType: schema.Time}, } - for _, f := range fields { - checkSchemaField(t, product, &f, func(field *schema.Field) { - f.Creatable = true - f.Updatable = true - f.Readable = true + for i := range fields { + checkSchemaField(t, product, &fields[i], func(field *schema.Field) { + field.Creatable = true + field.Updatable = true + field.Readable = true }) } } From 4499a248228ad3d71dbab6c29cd861e65e414533 Mon Sep 17 00:00:00 2001 From: black Date: Tue, 21 Feb 2023 14:35:27 +0800 Subject: [PATCH 4/4] rename variable --- schema/field.go | 4 ++-- schema/schema_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/schema/field.go b/schema/field.go index 224bb67d9..c24ff943f 100644 --- a/schema/field.go +++ b/schema/field.go @@ -56,7 +56,7 @@ type Field struct { BindNames []string DataType DataType GORMDataType DataType - CustomDataType DataType + IsTaggedType bool PrimaryKey bool AutoIncrement bool AutoIncrementIncrement int64 @@ -311,7 +311,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { default: field.DataType = DataType(val) } - field.CustomDataType = DataType(val) + field.IsTaggedType = true } if field.Size == 0 { diff --git a/schema/schema_test.go b/schema/schema_test.go index a18505534..2b8b816b9 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -23,9 +23,9 @@ func TestParseCustomTypeSchema(t *testing.T) { } fields := []schema.Field{ - {Name: "Date", DBName: "date", BindNames: []string{"Date"}, DataType: "date", CustomDataType: "date", Tag: `gorm:"type:date"`, TagSettings: map[string]string{"TYPE": "date"}}, - {Name: "Time", DBName: "time", BindNames: []string{"Time"}, DataType: schema.Time, CustomDataType: "time", Tag: `gorm:"type:time"`, TagSettings: map[string]string{"TYPE": "time"}}, - {Name: "DateTime", DBName: "date_time", BindNames: []string{"DateTime"}, DataType: schema.Time}, + {Name: "Date", DBName: "date", BindNames: []string{"Date"}, DataType: "date", IsTaggedType: true, Tag: `gorm:"type:date"`, TagSettings: map[string]string{"TYPE": "date"}}, + {Name: "Time", DBName: "time", BindNames: []string{"Time"}, DataType: schema.Time, IsTaggedType: true, Tag: `gorm:"type:time"`, TagSettings: map[string]string{"TYPE": "time"}}, + {Name: "DateTime", DBName: "date_time", BindNames: []string{"DateTime"}, DataType: schema.Time, IsTaggedType: false}, } for i := range fields { checkSchemaField(t, product, &fields[i], func(field *schema.Field) {