UnmarshalJSON returns an error for invalid zero.* types, tests

This commit is contained in:
Greg 2014-09-09 12:31:58 +09:00
parent b550c62e10
commit 214012c56c
13 changed files with 72 additions and 0 deletions

View file

@ -46,6 +46,13 @@ func TestUnmarshalBool(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullBool(t, null, "null json") 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) { func TestTextUnmarshalBool(t *testing.T) {

View file

@ -45,6 +45,13 @@ func TestUnmarshalFloat(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullFloat(t, null, "null json") 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) { func TestTextUnmarshalFloat(t *testing.T) {

BIN
gin-bin

Binary file not shown.

View file

@ -45,6 +45,13 @@ func TestUnmarshalInt(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullInt(t, null, "null json") 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) { func TestTextUnmarshalInt(t *testing.T) {

View file

@ -56,6 +56,13 @@ func TestUnmarshalString(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullStr(t, null, "null json") 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) { func TestTextUnmarshalString(t *testing.T) {

View file

@ -4,6 +4,8 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"reflect"
) )
// Bool is a nullable bool. // Bool is a nullable bool.
@ -49,6 +51,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
case nil: case nil:
b.Valid = false b.Valid = false
return nil 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 b.Valid = (err == nil) && b.Bool
return err return err

View file

@ -59,6 +59,13 @@ func TestUnmarshalBool(t *testing.T) {
panic("err should not be nil") panic("err should not be nil")
} }
assertNullBool(t, invalid, "invalid json") 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) { func TestTextUnmarshalBool(t *testing.T) {

View file

@ -3,6 +3,8 @@ package zero
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"reflect"
"strconv" "strconv"
) )
@ -50,6 +52,8 @@ func (f *Float) UnmarshalJSON(data []byte) error {
case nil: case nil:
f.Valid = false f.Valid = false
return nil 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) f.Valid = (err == nil) && (f.Float64 != 0)
return err return err

View file

@ -50,6 +50,13 @@ func TestUnmarshalFloat(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullFloat(t, null, "null json") 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) { func TestTextUnmarshalFloat(t *testing.T) {

View file

@ -3,6 +3,8 @@ package zero
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"reflect"
"strconv" "strconv"
) )
@ -51,6 +53,8 @@ func (i *Int) UnmarshalJSON(data []byte) error {
case nil: case nil:
i.Valid = false i.Valid = false
return nil 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) i.Valid = (err == nil) && (i.Int64 != 0)
return err return err

View file

@ -51,6 +51,13 @@ func TestUnmarshalInt(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullInt(t, null, "null json") 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) { func TestTextUnmarshalInt(t *testing.T) {

View file

@ -7,6 +7,8 @@ package zero
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"reflect"
) )
// String is a nullable string. // String is a nullable string.
@ -53,6 +55,8 @@ func (s *String) UnmarshalJSON(data []byte) error {
case nil: case nil:
s.Valid = false s.Valid = false
return nil 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 != "") s.Valid = (err == nil) && (s.String != "")
return err return err

View file

@ -44,6 +44,13 @@ func TestUnmarshalString(t *testing.T) {
err = json.Unmarshal(nullJSON, &null) err = json.Unmarshal(nullJSON, &null)
maybePanic(err) maybePanic(err)
assertNullStr(t, null, "null json") 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) { func TestTextUnmarshalString(t *testing.T) {