diff --git a/bool_test.go b/bool_test.go index 714b37a..eb9cb01 100644 --- a/bool_test.go +++ b/bool_test.go @@ -46,6 +46,13 @@ func TestUnmarshalBool(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullBool(t, null, "null json") + + var badType Bool + err = json.Unmarshal(intJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullBool(t, badType, "wrong type json") } func TestTextUnmarshalBool(t *testing.T) { diff --git a/float_test.go b/float_test.go index 9eeae01..a4ba3a4 100644 --- a/float_test.go +++ b/float_test.go @@ -45,6 +45,13 @@ func TestUnmarshalFloat(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullFloat(t, null, "null json") + + var badType Float + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullFloat(t, badType, "wrong type json") } func TestTextUnmarshalFloat(t *testing.T) { diff --git a/gin-bin b/gin-bin deleted file mode 100644 index 0ef32d9..0000000 Binary files a/gin-bin and /dev/null differ diff --git a/int_test.go b/int_test.go index 8fe2e26..708b89f 100644 --- a/int_test.go +++ b/int_test.go @@ -45,6 +45,13 @@ func TestUnmarshalInt(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullInt(t, null, "null json") + + var badType Int + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullInt(t, badType, "wrong type json") } func TestTextUnmarshalInt(t *testing.T) { diff --git a/string_test.go b/string_test.go index 88c484f..3a444d8 100644 --- a/string_test.go +++ b/string_test.go @@ -56,6 +56,13 @@ func TestUnmarshalString(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullStr(t, null, "null json") + + var badType String + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullStr(t, badType, "wrong type json") } func TestTextUnmarshalString(t *testing.T) { diff --git a/zero/bool.go b/zero/bool.go index e1b5153..e59cf96 100644 --- a/zero/bool.go +++ b/zero/bool.go @@ -4,6 +4,8 @@ import ( "database/sql" "encoding/json" "errors" + "fmt" + "reflect" ) // Bool is a nullable bool. @@ -49,6 +51,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 zero.Bool", reflect.TypeOf(v).Name()) } b.Valid = (err == nil) && b.Bool return err diff --git a/zero/bool_test.go b/zero/bool_test.go index 348f651..28f508c 100644 --- a/zero/bool_test.go +++ b/zero/bool_test.go @@ -59,6 +59,13 @@ func TestUnmarshalBool(t *testing.T) { panic("err should not be nil") } assertNullBool(t, invalid, "invalid json") + + var badType Bool + err = json.Unmarshal(intJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullBool(t, badType, "wrong type json") } func TestTextUnmarshalBool(t *testing.T) { diff --git a/zero/float.go b/zero/float.go index 8b36be1..247ea08 100644 --- a/zero/float.go +++ b/zero/float.go @@ -3,6 +3,8 @@ package zero import ( "database/sql" "encoding/json" + "fmt" + "reflect" "strconv" ) @@ -50,6 +52,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 zero.Float", reflect.TypeOf(v).Name()) } f.Valid = (err == nil) && (f.Float64 != 0) return err diff --git a/zero/float_test.go b/zero/float_test.go index cd4fd2f..d3d44f6 100644 --- a/zero/float_test.go +++ b/zero/float_test.go @@ -50,6 +50,13 @@ func TestUnmarshalFloat(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullFloat(t, null, "null json") + + var badType Float + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullFloat(t, badType, "wrong type json") } func TestTextUnmarshalFloat(t *testing.T) { diff --git a/zero/int.go b/zero/int.go index 9efb7bc..df79d0a 100644 --- a/zero/int.go +++ b/zero/int.go @@ -3,6 +3,8 @@ package zero import ( "database/sql" "encoding/json" + "fmt" + "reflect" "strconv" ) @@ -51,6 +53,8 @@ func (i *Int) UnmarshalJSON(data []byte) error { case nil: i.Valid = false return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int", reflect.TypeOf(v).Name()) } i.Valid = (err == nil) && (i.Int64 != 0) return err diff --git a/zero/int_test.go b/zero/int_test.go index 7007fbe..2910b6a 100644 --- a/zero/int_test.go +++ b/zero/int_test.go @@ -51,6 +51,13 @@ func TestUnmarshalInt(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullInt(t, null, "null json") + + var badType Int + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullInt(t, badType, "wrong type json") } func TestTextUnmarshalInt(t *testing.T) { diff --git a/zero/string.go b/zero/string.go index 3472153..a1aae5a 100644 --- a/zero/string.go +++ b/zero/string.go @@ -7,6 +7,8 @@ package zero import ( "database/sql" "encoding/json" + "fmt" + "reflect" ) // String is a nullable string. @@ -53,6 +55,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 zero.String", reflect.TypeOf(v).Name()) } s.Valid = (err == nil) && (s.String != "") return err diff --git a/zero/string_test.go b/zero/string_test.go index 8995b35..5e6385d 100644 --- a/zero/string_test.go +++ b/zero/string_test.go @@ -44,6 +44,13 @@ func TestUnmarshalString(t *testing.T) { err = json.Unmarshal(nullJSON, &null) maybePanic(err) assertNullStr(t, null, "null json") + + var badType String + err = json.Unmarshal(boolJSON, &badType) + if err == nil { + panic("err should not be nil") + } + assertNullStr(t, badType, "wrong type json") } func TestTextUnmarshalString(t *testing.T) {