From 63ac66b56988e1a22c8a3b41d4f1fbf9a8f5d0bc Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 17 Mar 2022 11:34:27 +0800 Subject: [PATCH] Support default tag for time.Time --- schema/field.go | 5 +++++ tests/default_value_test.go | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/schema/field.go b/schema/field.go index 826680c5..0d7085a9 100644 --- a/schema/field.go +++ b/schema/field.go @@ -259,6 +259,11 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } else if fieldValue.Type().ConvertibleTo(TimePtrReflectType) { field.DataType = Time } + if field.HasDefaultValue && !skipParseDefaultValue && field.DataType == Time { + if field.DefaultValueInterface, err = now.Parse(field.DefaultValue); err != nil { + schema.err = fmt.Errorf("failed to parse default value `%v` for field %v", field.DefaultValue, field.Name) + } + } case reflect.Array, reflect.Slice: if reflect.Indirect(fieldValue).Type().Elem() == ByteReflectType && field.DataType == "" { field.DataType = Bytes diff --git a/tests/default_value_test.go b/tests/default_value_test.go index 5e00b154..918f0796 100644 --- a/tests/default_value_test.go +++ b/tests/default_value_test.go @@ -2,6 +2,7 @@ package tests_test import ( "testing" + "time" "gorm.io/gorm" ) @@ -9,12 +10,13 @@ import ( func TestDefaultValue(t *testing.T) { type Harumph struct { gorm.Model - Email string `gorm:"not null;index:,unique"` - Name string `gorm:"notNull;default:foo"` - Name2 string `gorm:"size:233;not null;default:'foo'"` - Name3 string `gorm:"size:233;notNull;default:''"` - Age int `gorm:"default:18"` - Enabled bool `gorm:"default:true"` + Email string `gorm:"not null;index:,unique"` + Name string `gorm:"notNull;default:foo"` + Name2 string `gorm:"size:233;not null;default:'foo'"` + Name3 string `gorm:"size:233;notNull;default:''"` + Age int `gorm:"default:18"` + Created time.Time `gorm:"default:2000-01-02"` + Enabled bool `gorm:"default:true"` } DB.Migrator().DropTable(&Harumph{}) @@ -26,14 +28,14 @@ func TestDefaultValue(t *testing.T) { harumph := Harumph{Email: "hello@gorm.io"} if err := DB.Create(&harumph).Error; err != nil { t.Fatalf("Failed to create data with default value, got error: %v", err) - } else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Name3 != "" || harumph.Age != 18 || !harumph.Enabled { + } else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Name3 != "" || harumph.Age != 18 || !harumph.Enabled || harumph.Created.Format("20060102") != "20000102" { t.Fatalf("Failed to create data with default value, got: %+v", harumph) } var result Harumph if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil { t.Fatalf("Failed to find created data, got error: %v", err) - } else if result.Name != "foo" || result.Name2 != "foo" || result.Name3 != "" || result.Age != 18 || !result.Enabled { + } else if result.Name != "foo" || result.Name2 != "foo" || result.Name3 != "" || result.Age != 18 || !result.Enabled || result.Created.Format("20060102") != "20000102" { t.Fatalf("Failed to find created data with default data, got %+v", result) } }