From 6b98ced13dc3eb1b3bad01e7f3aac473c00b131f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 4 Jul 2020 07:45:07 +0800 Subject: [PATCH] Fix set time field from null, close #3108 --- schema/field.go | 6 +++++- schema/field_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/schema/field.go b/schema/field.go index f02968fa..fbcb3cef 100644 --- a/schema/field.go +++ b/schema/field.go @@ -655,7 +655,11 @@ func (field *Field) setupValuerAndSetter() { case time.Time: field.ReflectValueOf(value).Set(reflect.ValueOf(v)) case *time.Time: - field.ReflectValueOf(value).Set(reflect.ValueOf(v).Elem()) + if data != nil { + field.ReflectValueOf(value).Set(reflect.ValueOf(data).Elem()) + } else { + field.ReflectValueOf(value).Set(reflect.ValueOf(time.Time{})) + } case string: if t, err := now.Parse(data); err == nil { field.ReflectValueOf(value).Set(reflect.ValueOf(t)) diff --git a/schema/field_test.go b/schema/field_test.go index 7970b614..7027b11d 100644 --- a/schema/field_test.go +++ b/schema/field_test.go @@ -19,6 +19,7 @@ func TestFieldValuerAndSetter(t *testing.T) { Model: gorm.Model{ ID: 10, CreatedAt: time.Now(), + UpdatedAt: time.Now(), DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true}, }, Name: "valuer_and_setter", @@ -34,6 +35,7 @@ func TestFieldValuerAndSetter(t *testing.T) { "name": user.Name, "id": user.ID, "created_at": user.CreatedAt, + "updated_at": user.UpdatedAt, "deleted_at": user.DeletedAt, "age": user.Age, "birthday": user.Birthday, @@ -46,6 +48,7 @@ func TestFieldValuerAndSetter(t *testing.T) { "name": "valuer_and_setter_2", "id": 2, "created_at": time.Now(), + "updated_at": nil, "deleted_at": time.Now(), "age": 20, "birthday": time.Now(), @@ -57,14 +60,17 @@ func TestFieldValuerAndSetter(t *testing.T) { t.Errorf("no error should happen when assign value to field %v, but got %v", k, err) } } + newValues["updated_at"] = time.Time{} checkField(t, userSchema, reflectValue, newValues) // test valuer and other type age := myint(10) + var nilTime *time.Time newValues2 := map[string]interface{}{ "name": sql.NullString{String: "valuer_and_setter_3", Valid: true}, "id": &sql.NullInt64{Int64: 3, Valid: true}, "created_at": tests.Now(), + "updated_at": nilTime, "deleted_at": time.Now(), "age": &age, "birthday": mytime(time.Now()), @@ -76,6 +82,7 @@ func TestFieldValuerAndSetter(t *testing.T) { t.Errorf("no error should happen when assign value to field %v, but got %v", k, err) } } + newValues2["updated_at"] = time.Time{} checkField(t, userSchema, reflectValue, newValues2) }