forked from mirror/gorm
Fix set time field from null, close #3108
This commit is contained in:
parent
d4f8a52442
commit
6b98ced13d
|
@ -655,7 +655,11 @@ func (field *Field) setupValuerAndSetter() {
|
||||||
case time.Time:
|
case time.Time:
|
||||||
field.ReflectValueOf(value).Set(reflect.ValueOf(v))
|
field.ReflectValueOf(value).Set(reflect.ValueOf(v))
|
||||||
case *time.Time:
|
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:
|
case string:
|
||||||
if t, err := now.Parse(data); err == nil {
|
if t, err := now.Parse(data); err == nil {
|
||||||
field.ReflectValueOf(value).Set(reflect.ValueOf(t))
|
field.ReflectValueOf(value).Set(reflect.ValueOf(t))
|
||||||
|
|
|
@ -19,6 +19,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
|
||||||
Model: gorm.Model{
|
Model: gorm.Model{
|
||||||
ID: 10,
|
ID: 10,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
|
DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
|
||||||
},
|
},
|
||||||
Name: "valuer_and_setter",
|
Name: "valuer_and_setter",
|
||||||
|
@ -34,6 +35,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
|
||||||
"name": user.Name,
|
"name": user.Name,
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
"created_at": user.CreatedAt,
|
"created_at": user.CreatedAt,
|
||||||
|
"updated_at": user.UpdatedAt,
|
||||||
"deleted_at": user.DeletedAt,
|
"deleted_at": user.DeletedAt,
|
||||||
"age": user.Age,
|
"age": user.Age,
|
||||||
"birthday": user.Birthday,
|
"birthday": user.Birthday,
|
||||||
|
@ -46,6 +48,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
|
||||||
"name": "valuer_and_setter_2",
|
"name": "valuer_and_setter_2",
|
||||||
"id": 2,
|
"id": 2,
|
||||||
"created_at": time.Now(),
|
"created_at": time.Now(),
|
||||||
|
"updated_at": nil,
|
||||||
"deleted_at": time.Now(),
|
"deleted_at": time.Now(),
|
||||||
"age": 20,
|
"age": 20,
|
||||||
"birthday": time.Now(),
|
"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)
|
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)
|
checkField(t, userSchema, reflectValue, newValues)
|
||||||
|
|
||||||
// test valuer and other type
|
// test valuer and other type
|
||||||
age := myint(10)
|
age := myint(10)
|
||||||
|
var nilTime *time.Time
|
||||||
newValues2 := map[string]interface{}{
|
newValues2 := map[string]interface{}{
|
||||||
"name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
|
"name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
|
||||||
"id": &sql.NullInt64{Int64: 3, Valid: true},
|
"id": &sql.NullInt64{Int64: 3, Valid: true},
|
||||||
"created_at": tests.Now(),
|
"created_at": tests.Now(),
|
||||||
|
"updated_at": nilTime,
|
||||||
"deleted_at": time.Now(),
|
"deleted_at": time.Now(),
|
||||||
"age": &age,
|
"age": &age,
|
||||||
"birthday": mytime(time.Now()),
|
"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)
|
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)
|
checkField(t, userSchema, reflectValue, newValues2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue