Merge pull request #2 from guregu/errors
UnmarshalJSON should return an error for invalid types
This commit is contained in:
commit
f565b8c15b
16 changed files with 88 additions and 0 deletions
4
bool.go
4
bool.go
|
@ -4,6 +4,8 @@ import (
|
|||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Bool is an even nuller nullable bool.
|
||||
|
@ -52,6 +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", reflect.TypeOf(v).Name())
|
||||
}
|
||||
b.Valid = err == nil
|
||||
return err
|
||||
|
|
|
@ -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) {
|
||||
|
|
4
float.go
4
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
4
int.go
4
int.go
|
@ -3,6 +3,8 @@ package null
|
|||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -52,6 +54,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 null.Int", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = err == nil
|
||||
return err
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue