forked from mirror/gorm
Fix set bool field from null
This commit is contained in:
parent
f835a4deac
commit
90a40361ed
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue