From f5cd2b14ccf8dd06fb3562d4d88f8ecde2ca0b79 Mon Sep 17 00:00:00 2001 From: Joel Kek Date: Tue, 9 Sep 2014 00:55:40 +0800 Subject: [PATCH] UnmarshalJSON in null.Bool, null.Float, and null.String now return an error message when asked to unmarshal from an invalid type. This is in line with the behavior of the built-in sql library's null values. --- bool.go | 7 ++++--- float.go | 4 ++++ string.go | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bool.go b/bool.go index 86950a1..b913777 100644 --- a/bool.go +++ b/bool.go @@ -4,7 +4,8 @@ import ( "database/sql" "encoding/json" "errors" - // "fmt" + "fmt" + "reflect" ) // Bool is an even nuller nullable bool. @@ -53,8 +54,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error { case nil: b.Valid = false return nil - // default: - // err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", v.(type).Name()) + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", reflect.TypeOf(v).Name()) } b.Valid = err == nil return err diff --git a/float.go b/float.go index 42c5cdd..5b722b1 100644 --- a/float.go +++ b/float.go @@ -3,6 +3,8 @@ package null import ( "database/sql" "encoding/json" + "fmt" + "reflect" "strconv" ) @@ -52,6 +54,8 @@ func (f *Float) UnmarshalJSON(data []byte) error { case nil: f.Valid = false return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Float", reflect.TypeOf(v).Name()) } f.Valid = err == nil return err diff --git a/string.go b/string.go index c646908..630616a 100644 --- a/string.go +++ b/string.go @@ -6,6 +6,8 @@ package null import ( "database/sql" "encoding/json" + "fmt" + "reflect" ) // String is an even nuller nullable string. @@ -51,6 +53,8 @@ func (s *String) UnmarshalJSON(data []byte) error { case nil: s.Valid = false return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name()) } s.Valid = (err == nil) && (s.String != "") return err