Support default tag for time.Time

This commit is contained in:
Jinzhu 2022-03-17 11:34:27 +08:00
parent 6befa0c947
commit 63ac66b569
2 changed files with 15 additions and 8 deletions

View File

@ -259,6 +259,11 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} else if fieldValue.Type().ConvertibleTo(TimePtrReflectType) { } else if fieldValue.Type().ConvertibleTo(TimePtrReflectType) {
field.DataType = Time 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: case reflect.Array, reflect.Slice:
if reflect.Indirect(fieldValue).Type().Elem() == ByteReflectType && field.DataType == "" { if reflect.Indirect(fieldValue).Type().Elem() == ByteReflectType && field.DataType == "" {
field.DataType = Bytes field.DataType = Bytes

View File

@ -2,6 +2,7 @@ package tests_test
import ( import (
"testing" "testing"
"time"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -9,12 +10,13 @@ import (
func TestDefaultValue(t *testing.T) { func TestDefaultValue(t *testing.T) {
type Harumph struct { type Harumph struct {
gorm.Model gorm.Model
Email string `gorm:"not null;index:,unique"` Email string `gorm:"not null;index:,unique"`
Name string `gorm:"notNull;default:foo"` Name string `gorm:"notNull;default:foo"`
Name2 string `gorm:"size:233;not null;default:'foo'"` Name2 string `gorm:"size:233;not null;default:'foo'"`
Name3 string `gorm:"size:233;notNull;default:''"` Name3 string `gorm:"size:233;notNull;default:''"`
Age int `gorm:"default:18"` Age int `gorm:"default:18"`
Enabled bool `gorm:"default:true"` Created time.Time `gorm:"default:2000-01-02"`
Enabled bool `gorm:"default:true"`
} }
DB.Migrator().DropTable(&Harumph{}) DB.Migrator().DropTable(&Harumph{})
@ -26,14 +28,14 @@ func TestDefaultValue(t *testing.T) {
harumph := Harumph{Email: "hello@gorm.io"} harumph := Harumph{Email: "hello@gorm.io"}
if err := DB.Create(&harumph).Error; err != nil { if err := DB.Create(&harumph).Error; err != nil {
t.Fatalf("Failed to create data with default value, got error: %v", err) 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) t.Fatalf("Failed to create data with default value, got: %+v", harumph)
} }
var result Harumph var result Harumph
if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil { if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil {
t.Fatalf("Failed to find created data, got error: %v", err) 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) t.Fatalf("Failed to find created data with default data, got %+v", result)
} }
} }