Add byte type

This commit is contained in:
Patrick O'brien 2016-11-11 17:15:01 +10:00
parent 133b805f06
commit 50834ac6bc
31 changed files with 43 additions and 122 deletions

View file

@ -54,11 +54,13 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Bool) UnmarshalText(text []byte) error {
str := string(text)
switch str {
case "", "null":
if text == nil || len(text) == 0 {
b.Valid = false
return nil
}
str := string(text)
switch str {
case "true":
b.Bool = true
case "false":

View file

@ -71,11 +71,6 @@ func TestTextUnmarshalBool(t *testing.T) {
maybePanic(err)
assertNullBool(t, blank, "UnmarshalText() empty bool")
var null Bool
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullBool(t, null, `UnmarshalText() "null"`)
var invalid Bool
err = invalid.UnmarshalText([]byte(":D"))
if err == nil {

View file

@ -53,14 +53,14 @@ func (b *Byte) UnmarshalJSON(data []byte) error {
return errors.New("json: cannot convert to byte, text len is greater than one")
}
b.Byte = []byte(x)[0]
b.Byte = x[0]
b.Valid = true
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Byte) UnmarshalText(text []byte) error {
if len(text) == 0 {
if text == nil || len(text) == 0 {
b.Valid = false
return nil
}
@ -79,7 +79,7 @@ func (b Byte) MarshalJSON() ([]byte, error) {
if !b.Valid {
return NullBytes, nil
}
return []byte{b.Byte}, nil
return []byte{'"', b.Byte, '"'}, nil
}
// MarshalText implements encoding.TextMarshaler.

View file

@ -10,7 +10,7 @@ var (
)
func TestByteFrom(t *testing.T) {
i := ByteFrom(12345)
i := ByteFrom('b')
assertByte(t, i, "ByteFrom()")
zero := ByteFrom(0)
@ -20,7 +20,7 @@ func TestByteFrom(t *testing.T) {
}
func TestByteFromPtr(t *testing.T) {
n := int(12345)
n := byte('b')
iptr := &n
i := ByteFromPtr(iptr)
assertByte(t, i, "ByteFromPtr()")
@ -30,13 +30,8 @@ func TestByteFromPtr(t *testing.T) {
}
func TestUnmarshalByte(t *testing.T) {
var i Byte
err := json.Unmarshal(intJSON, &i)
maybePanic(err)
assertByte(t, i, "int json")
var null Byte
err = json.Unmarshal(nullJSON, &null)
err := json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullByte(t, null, "null json")
@ -65,7 +60,7 @@ func TestUnmarshalNonByteegerNumber(t *testing.T) {
func TestTextUnmarshalByte(t *testing.T) {
var i Byte
err := i.UnmarshalText([]byte("12345"))
err := i.UnmarshalText([]byte("b"))
maybePanic(err)
assertByte(t, i, "UnmarshalText() int")
@ -73,18 +68,13 @@ func TestTextUnmarshalByte(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullByte(t, blank, "UnmarshalText() empty int")
var null Byte
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullByte(t, null, `UnmarshalText() "null"`)
}
func TestMarshalByte(t *testing.T) {
i := ByteFrom(12345)
i := ByteFrom('b')
data, err := json.Marshal(i)
maybePanic(err)
assertJSONEquals(t, data, "12345", "non-empty json marshal")
assertJSONEquals(t, data, `"b"`, "non-empty json marshal")
// invalid values should be encoded as null
null := NewByte(0, false)
@ -94,10 +84,10 @@ func TestMarshalByte(t *testing.T) {
}
func TestMarshalByteText(t *testing.T) {
i := ByteFrom(12345)
i := ByteFrom('b')
data, err := i.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "12345", "non-empty text marshal")
assertJSONEquals(t, data, "b", "non-empty text marshal")
// invalid values should be encoded as null
null := NewByte(0, false)
@ -107,10 +97,10 @@ func TestMarshalByteText(t *testing.T) {
}
func TestBytePointer(t *testing.T) {
i := ByteFrom(12345)
i := ByteFrom('b')
ptr := i.Ptr()
if *ptr != 12345 {
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345)
if *ptr != 'b' {
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 'b')
}
null := NewByte(0, false)
@ -121,7 +111,7 @@ func TestBytePointer(t *testing.T) {
}
func TestByteIsZero(t *testing.T) {
i := ByteFrom(12345)
i := ByteFrom('b')
if i.IsZero() {
t.Errorf("IsZero() should be false")
}
@ -140,13 +130,13 @@ func TestByteIsZero(t *testing.T) {
func TestByteSetValid(t *testing.T) {
change := NewByte(0, false)
assertNullByte(t, change, "SetValid()")
change.SetValid(12345)
change.SetValid('b')
assertByte(t, change, "SetValid()")
}
func TestByteScan(t *testing.T) {
var i Byte
err := i.Scan(12345)
err := i.Scan('b')
maybePanic(err)
assertByte(t, i, "scanned int")
@ -157,8 +147,8 @@ func TestByteScan(t *testing.T) {
}
func assertByte(t *testing.T, i Byte, from string) {
if i.Byte != 12345 {
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 12345)
if i.Byte != 'b' {
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 'b')
}
if !i.Valid {
t.Error(from, "is invalid, but should be valid")

View file

@ -56,7 +56,7 @@ func (f *Float32) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (f *Float32) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
f.Valid = false
return nil
}

View file

@ -64,11 +64,6 @@ func TestTextUnmarshalFloat32(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullFloat32(t, blank, "UnmarshalText() empty float32")
var null Float32
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullFloat32(t, null, `UnmarshalText() "null"`)
}
func TestMarshalFloat32(t *testing.T) {

View file

@ -54,7 +54,7 @@ func (f *Float64) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (f *Float64) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
f.Valid = false
return nil
}

View file

@ -64,11 +64,6 @@ func TestTextUnmarshalFloat64(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullFloat64(t, blank, "UnmarshalText() empty float64")
var null Float64
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullFloat64(t, null, `UnmarshalText() "null"`)
}
func TestMarshalFloat64(t *testing.T) {

2
int.go
View file

@ -56,7 +56,7 @@ func (i *Int) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}

View file

@ -62,7 +62,7 @@ func (i *Int16) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int16) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}

View file

@ -2,7 +2,6 @@ package null
import (
"encoding/json"
"fmt"
"math"
"strconv"
"testing"
@ -73,11 +72,9 @@ func TestUnmarshalInt16Overflow(t *testing.T) {
var i Int16
err := json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
maybePanic(err)
fmt.Println(i)
// Attempt to overflow
int16Overflow++
err = json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
fmt.Println(i)
if err == nil {
panic("err should be present; decoded value overflows int16")
}
@ -93,11 +90,6 @@ func TestTextUnmarshalInt16(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullInt16(t, blank, "UnmarshalText() empty int16")
var null Int16
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullInt16(t, null, `UnmarshalText() "null"`)
}
func TestMarshalInt16(t *testing.T) {

View file

@ -62,7 +62,7 @@ func (i *Int32) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int32) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalInt32(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullInt32(t, blank, "UnmarshalText() empty int32")
var null Int32
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullInt32(t, null, `UnmarshalText() "null"`)
}
func TestMarshalInt32(t *testing.T) {

View file

@ -54,7 +54,7 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int64) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalInt64(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullInt64(t, blank, "UnmarshalText() empty int64")
var null Int64
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullInt64(t, null, `UnmarshalText() "null"`)
}
func TestMarshalInt64(t *testing.T) {

View file

@ -62,7 +62,7 @@ func (i *Int8) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int8) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalInt8(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullInt8(t, blank, "UnmarshalText() empty int8")
var null Int8
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullInt8(t, null, `UnmarshalText() "null"`)
}
func TestMarshalInt8(t *testing.T) {

View file

@ -73,11 +73,6 @@ func TestTextUnmarshalInt(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullInt(t, blank, "UnmarshalText() empty int")
var null Int
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullInt(t, null, `UnmarshalText() "null"`)
}
func TestMarshalInt(t *testing.T) {

View file

@ -69,8 +69,13 @@ func (s String) MarshalText() ([]byte, error) {
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *String) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
s.Valid = false
return nil
}
s.String = string(text)
s.Valid = s.String != ""
s.Valid = true
return nil
}

View file

@ -68,7 +68,7 @@ func (t Time) MarshalText() ([]byte, error) {
// UnmarshalText implements encoding.TextUnmarshaler.
func (t *Time) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
t.Valid = false
return nil
}

View file

@ -58,14 +58,6 @@ func TestUnmarshalTimeText(t *testing.T) {
maybePanic(err)
assertTime(t, unmarshal, "unmarshal text")
var null Time
err = null.UnmarshalText(nullJSON)
maybePanic(err)
assertNullTime(t, null, "unmarshal null text")
txt, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, string(nullJSON), "marshal null text")
var invalid Time
err = invalid.UnmarshalText([]byte("hello world"))
if err == nil {

View file

@ -56,7 +56,7 @@ func (u *Uint) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Uint) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
u.Valid = false
return nil
}

View file

@ -62,7 +62,7 @@ func (u *Uint16) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Uint16) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
u.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalUint16(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullUint16(t, blank, "UnmarshalText() empty uint16")
var null Uint16
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullUint16(t, null, `UnmarshalText() "null"`)
}
func TestMarshalUint16(t *testing.T) {

View file

@ -62,7 +62,7 @@ func (u *Uint32) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Uint32) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
u.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalUint32(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullUint32(t, blank, "UnmarshalText() empty uint32")
var null Uint32
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullUint32(t, null, `UnmarshalText() "null"`)
}
func TestMarshalUint32(t *testing.T) {

View file

@ -54,7 +54,7 @@ func (u *Uint64) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Uint64) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
u.Valid = false
return nil
}

View file

@ -73,11 +73,6 @@ func TestTextUnmarshalUint64(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullUint64(t, blank, "UnmarshalText() empty uint64")
var null Uint64
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullUint64(t, null, `UnmarshalText() "null"`)
}
func TestMarshalUint64(t *testing.T) {

View file

@ -62,7 +62,7 @@ func (u *Uint8) UnmarshalJSON(data []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (u *Uint8) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.Equal(text, NullBytes) {
if text == nil || len(text) == 0 {
u.Valid = false
return nil
}

View file

@ -91,11 +91,6 @@ func TestTextUnmarshalUint8(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullUint8(t, blank, "UnmarshalText() empty uint8")
var null Uint8
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullUint8(t, null, `UnmarshalText() "null"`)
}
func TestMarshalUint8(t *testing.T) {

View file

@ -73,11 +73,6 @@ func TestTextUnmarshalUint(t *testing.T) {
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullUint(t, blank, "UnmarshalText() empty uint")
var null Uint
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullUint(t, null, `UnmarshalText() "null"`)
}
func TestMarshalUint(t *testing.T) {