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:
field.ReflectValueOf(value).SetBool(data)
case *bool:
if data != nil {
field.ReflectValueOf(value).SetBool(*data)
} else {
field.ReflectValueOf(value).SetBool(false)
}
case int64:
if data > 0 {
field.ReflectValueOf(value).SetBool(true)

View File

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