Fix set bool field from null

This commit is contained in:
Jinzhu 2020-07-04 08:21:23 +08:00
parent f835a4deac
commit 90a40361ed
2 changed files with 8 additions and 2 deletions

View File

@ -479,7 +479,11 @@ func (field *Field) setupValuerAndSetter() {
case bool: case bool:
field.ReflectValueOf(value).SetBool(data) field.ReflectValueOf(value).SetBool(data)
case *bool: case *bool:
field.ReflectValueOf(value).SetBool(*data) if data != nil {
field.ReflectValueOf(value).SetBool(*data)
} else {
field.ReflectValueOf(value).SetBool(false)
}
case int64: case int64:
if data > 0 { if data > 0 {
field.ReflectValueOf(value).SetBool(true) field.ReflectValueOf(value).SetBool(true)

View File

@ -43,6 +43,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
} }
checkField(t, userSchema, reflectValue, values) checkField(t, userSchema, reflectValue, values)
var f *bool
// test setter // test setter
newValues := map[string]interface{}{ newValues := map[string]interface{}{
"name": "valuer_and_setter_2", "name": "valuer_and_setter_2",
@ -52,7 +53,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
"deleted_at": time.Now(), "deleted_at": time.Now(),
"age": 20, "age": 20,
"birthday": time.Now(), "birthday": time.Now(),
"active": false, "active": f,
} }
for k, v := range newValues { for k, v := range newValues {
@ -61,6 +62,7 @@ func TestFieldValuerAndSetter(t *testing.T) {
} }
} }
newValues["updated_at"] = time.Time{} newValues["updated_at"] = time.Time{}
newValues["active"] = false
checkField(t, userSchema, reflectValue, newValues) checkField(t, userSchema, reflectValue, newValues)
// test valuer and other type // test valuer and other type