mirror of https://github.com/go-gorm/gorm.git
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:
|
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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue