From 90a40361ed38314b8ea45e703a14f0ed58925892 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 4 Jul 2020 08:21:23 +0800 Subject: [PATCH] Fix set bool field from null --- schema/field.go | 6 +++++- schema/field_test.go | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/schema/field.go b/schema/field.go index fbcb3cef..d72a26d5 100644 --- a/schema/field.go +++ b/schema/field.go @@ -479,7 +479,11 @@ func (field *Field) setupValuerAndSetter() { case bool: field.ReflectValueOf(value).SetBool(data) case *bool: - field.ReflectValueOf(value).SetBool(*data) + if data != nil { + field.ReflectValueOf(value).SetBool(*data) + } else { + field.ReflectValueOf(value).SetBool(false) + } case int64: if data > 0 { field.ReflectValueOf(value).SetBool(true) diff --git a/schema/field_test.go b/schema/field_test.go index 7027b11d..64f4a909 100644 --- a/schema/field_test.go +++ b/schema/field_test.go @@ -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