Add byte type
This commit is contained in:
parent
133b805f06
commit
50834ac6bc
31 changed files with 43 additions and 122 deletions
bool.gobool_test.gobyte.gobyte_test.gofloat32.gofloat32_test.gofloat64.gofloat64_test.goint.goint16.goint16_test.goint32.goint32_test.goint64.goint64_test.goint8.goint8_test.goint_test.gostring.gotime.gotime_test.gouint.gouint16.gouint16_test.gouint32.gouint32_test.gouint64.gouint64_test.gouint8.gouint8_test.gouint_test.go
8
bool.go
8
bool.go
|
@ -54,11 +54,13 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (b *Bool) UnmarshalText(text []byte) error {
|
func (b *Bool) UnmarshalText(text []byte) error {
|
||||||
str := string(text)
|
if text == nil || len(text) == 0 {
|
||||||
switch str {
|
|
||||||
case "", "null":
|
|
||||||
b.Valid = false
|
b.Valid = false
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
str := string(text)
|
||||||
|
switch str {
|
||||||
case "true":
|
case "true":
|
||||||
b.Bool = true
|
b.Bool = true
|
||||||
case "false":
|
case "false":
|
||||||
|
|
|
@ -71,11 +71,6 @@ func TestTextUnmarshalBool(t *testing.T) {
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullBool(t, blank, "UnmarshalText() empty bool")
|
assertNullBool(t, blank, "UnmarshalText() empty bool")
|
||||||
|
|
||||||
var null Bool
|
|
||||||
err = null.UnmarshalText([]byte("null"))
|
|
||||||
maybePanic(err)
|
|
||||||
assertNullBool(t, null, `UnmarshalText() "null"`)
|
|
||||||
|
|
||||||
var invalid Bool
|
var invalid Bool
|
||||||
err = invalid.UnmarshalText([]byte(":D"))
|
err = invalid.UnmarshalText([]byte(":D"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
6
byte.go
6
byte.go
|
@ -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")
|
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
|
b.Valid = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (b *Byte) UnmarshalText(text []byte) error {
|
func (b *Byte) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 {
|
if text == nil || len(text) == 0 {
|
||||||
b.Valid = false
|
b.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func (b Byte) MarshalJSON() ([]byte, error) {
|
||||||
if !b.Valid {
|
if !b.Valid {
|
||||||
return NullBytes, nil
|
return NullBytes, nil
|
||||||
}
|
}
|
||||||
return []byte{b.Byte}, nil
|
return []byte{'"', b.Byte, '"'}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalText implements encoding.TextMarshaler.
|
// MarshalText implements encoding.TextMarshaler.
|
||||||
|
|
42
byte_test.go
42
byte_test.go
|
@ -10,7 +10,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestByteFrom(t *testing.T) {
|
func TestByteFrom(t *testing.T) {
|
||||||
i := ByteFrom(12345)
|
i := ByteFrom('b')
|
||||||
assertByte(t, i, "ByteFrom()")
|
assertByte(t, i, "ByteFrom()")
|
||||||
|
|
||||||
zero := ByteFrom(0)
|
zero := ByteFrom(0)
|
||||||
|
@ -20,7 +20,7 @@ func TestByteFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestByteFromPtr(t *testing.T) {
|
func TestByteFromPtr(t *testing.T) {
|
||||||
n := int(12345)
|
n := byte('b')
|
||||||
iptr := &n
|
iptr := &n
|
||||||
i := ByteFromPtr(iptr)
|
i := ByteFromPtr(iptr)
|
||||||
assertByte(t, i, "ByteFromPtr()")
|
assertByte(t, i, "ByteFromPtr()")
|
||||||
|
@ -30,13 +30,8 @@ func TestByteFromPtr(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalByte(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
|
var null Byte
|
||||||
err = json.Unmarshal(nullJSON, &null)
|
err := json.Unmarshal(nullJSON, &null)
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullByte(t, null, "null json")
|
assertNullByte(t, null, "null json")
|
||||||
|
|
||||||
|
@ -65,7 +60,7 @@ func TestUnmarshalNonByteegerNumber(t *testing.T) {
|
||||||
|
|
||||||
func TestTextUnmarshalByte(t *testing.T) {
|
func TestTextUnmarshalByte(t *testing.T) {
|
||||||
var i Byte
|
var i Byte
|
||||||
err := i.UnmarshalText([]byte("12345"))
|
err := i.UnmarshalText([]byte("b"))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertByte(t, i, "UnmarshalText() int")
|
assertByte(t, i, "UnmarshalText() int")
|
||||||
|
|
||||||
|
@ -73,18 +68,13 @@ func TestTextUnmarshalByte(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullByte(t, blank, "UnmarshalText() empty int")
|
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) {
|
func TestMarshalByte(t *testing.T) {
|
||||||
i := ByteFrom(12345)
|
i := ByteFrom('b')
|
||||||
data, err := json.Marshal(i)
|
data, err := json.Marshal(i)
|
||||||
maybePanic(err)
|
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
|
// invalid values should be encoded as null
|
||||||
null := NewByte(0, false)
|
null := NewByte(0, false)
|
||||||
|
@ -94,10 +84,10 @@ func TestMarshalByte(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshalByteText(t *testing.T) {
|
func TestMarshalByteText(t *testing.T) {
|
||||||
i := ByteFrom(12345)
|
i := ByteFrom('b')
|
||||||
data, err := i.MarshalText()
|
data, err := i.MarshalText()
|
||||||
maybePanic(err)
|
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
|
// invalid values should be encoded as null
|
||||||
null := NewByte(0, false)
|
null := NewByte(0, false)
|
||||||
|
@ -107,10 +97,10 @@ func TestMarshalByteText(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBytePointer(t *testing.T) {
|
func TestBytePointer(t *testing.T) {
|
||||||
i := ByteFrom(12345)
|
i := ByteFrom('b')
|
||||||
ptr := i.Ptr()
|
ptr := i.Ptr()
|
||||||
if *ptr != 12345 {
|
if *ptr != 'b' {
|
||||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345)
|
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 'b')
|
||||||
}
|
}
|
||||||
|
|
||||||
null := NewByte(0, false)
|
null := NewByte(0, false)
|
||||||
|
@ -121,7 +111,7 @@ func TestBytePointer(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestByteIsZero(t *testing.T) {
|
func TestByteIsZero(t *testing.T) {
|
||||||
i := ByteFrom(12345)
|
i := ByteFrom('b')
|
||||||
if i.IsZero() {
|
if i.IsZero() {
|
||||||
t.Errorf("IsZero() should be false")
|
t.Errorf("IsZero() should be false")
|
||||||
}
|
}
|
||||||
|
@ -140,13 +130,13 @@ func TestByteIsZero(t *testing.T) {
|
||||||
func TestByteSetValid(t *testing.T) {
|
func TestByteSetValid(t *testing.T) {
|
||||||
change := NewByte(0, false)
|
change := NewByte(0, false)
|
||||||
assertNullByte(t, change, "SetValid()")
|
assertNullByte(t, change, "SetValid()")
|
||||||
change.SetValid(12345)
|
change.SetValid('b')
|
||||||
assertByte(t, change, "SetValid()")
|
assertByte(t, change, "SetValid()")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestByteScan(t *testing.T) {
|
func TestByteScan(t *testing.T) {
|
||||||
var i Byte
|
var i Byte
|
||||||
err := i.Scan(12345)
|
err := i.Scan('b')
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertByte(t, i, "scanned int")
|
assertByte(t, i, "scanned int")
|
||||||
|
|
||||||
|
@ -157,8 +147,8 @@ func TestByteScan(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertByte(t *testing.T, i Byte, from string) {
|
func assertByte(t *testing.T, i Byte, from string) {
|
||||||
if i.Byte != 12345 {
|
if i.Byte != 'b' {
|
||||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 12345)
|
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 'b')
|
||||||
}
|
}
|
||||||
if !i.Valid {
|
if !i.Valid {
|
||||||
t.Error(from, "is invalid, but should be valid")
|
t.Error(from, "is invalid, but should be valid")
|
||||||
|
|
|
@ -56,7 +56,7 @@ func (f *Float32) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (f *Float32) UnmarshalText(text []byte) error {
|
func (f *Float32) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
f.Valid = false
|
f.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,11 +64,6 @@ func TestTextUnmarshalFloat32(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullFloat32(t, blank, "UnmarshalText() empty float32")
|
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) {
|
func TestMarshalFloat32(t *testing.T) {
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (f *Float64) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (f *Float64) UnmarshalText(text []byte) error {
|
func (f *Float64) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
f.Valid = false
|
f.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,11 +64,6 @@ func TestTextUnmarshalFloat64(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullFloat64(t, blank, "UnmarshalText() empty float64")
|
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) {
|
func TestMarshalFloat64(t *testing.T) {
|
||||||
|
|
2
int.go
2
int.go
|
@ -56,7 +56,7 @@ func (i *Int) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (i *Int) UnmarshalText(text []byte) error {
|
func (i *Int) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
i.Valid = false
|
i.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
2
int16.go
2
int16.go
|
@ -62,7 +62,7 @@ func (i *Int16) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (i *Int16) UnmarshalText(text []byte) error {
|
func (i *Int16) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
i.Valid = false
|
i.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package null
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -73,11 +72,9 @@ func TestUnmarshalInt16Overflow(t *testing.T) {
|
||||||
var i Int16
|
var i Int16
|
||||||
err := json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
|
err := json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
fmt.Println(i)
|
|
||||||
// Attempt to overflow
|
// Attempt to overflow
|
||||||
int16Overflow++
|
int16Overflow++
|
||||||
err = json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
|
err = json.Unmarshal([]byte(strconv.FormatUint(uint64(int16Overflow), 10)), &i)
|
||||||
fmt.Println(i)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
panic("err should be present; decoded value overflows int16")
|
panic("err should be present; decoded value overflows int16")
|
||||||
}
|
}
|
||||||
|
@ -93,11 +90,6 @@ func TestTextUnmarshalInt16(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullInt16(t, blank, "UnmarshalText() empty int16")
|
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) {
|
func TestMarshalInt16(t *testing.T) {
|
||||||
|
|
2
int32.go
2
int32.go
|
@ -62,7 +62,7 @@ func (i *Int32) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (i *Int32) UnmarshalText(text []byte) error {
|
func (i *Int32) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
i.Valid = false
|
i.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalInt32(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullInt32(t, blank, "UnmarshalText() empty int32")
|
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) {
|
func TestMarshalInt32(t *testing.T) {
|
||||||
|
|
2
int64.go
2
int64.go
|
@ -54,7 +54,7 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (i *Int64) UnmarshalText(text []byte) error {
|
func (i *Int64) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
i.Valid = false
|
i.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalInt64(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullInt64(t, blank, "UnmarshalText() empty int64")
|
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) {
|
func TestMarshalInt64(t *testing.T) {
|
||||||
|
|
2
int8.go
2
int8.go
|
@ -62,7 +62,7 @@ func (i *Int8) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (i *Int8) UnmarshalText(text []byte) error {
|
func (i *Int8) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
i.Valid = false
|
i.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalInt8(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullInt8(t, blank, "UnmarshalText() empty int8")
|
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) {
|
func TestMarshalInt8(t *testing.T) {
|
||||||
|
|
|
@ -73,11 +73,6 @@ func TestTextUnmarshalInt(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullInt(t, blank, "UnmarshalText() empty int")
|
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) {
|
func TestMarshalInt(t *testing.T) {
|
||||||
|
|
|
@ -69,8 +69,13 @@ func (s String) MarshalText() ([]byte, error) {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (s *String) UnmarshalText(text []byte) error {
|
func (s *String) UnmarshalText(text []byte) error {
|
||||||
|
if text == nil || len(text) == 0 {
|
||||||
|
s.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
s.String = string(text)
|
s.String = string(text)
|
||||||
s.Valid = s.String != ""
|
s.Valid = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
time.go
2
time.go
|
@ -68,7 +68,7 @@ func (t Time) MarshalText() ([]byte, error) {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (t *Time) UnmarshalText(text []byte) error {
|
func (t *Time) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
t.Valid = false
|
t.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,14 +58,6 @@ func TestUnmarshalTimeText(t *testing.T) {
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertTime(t, unmarshal, "unmarshal text")
|
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
|
var invalid Time
|
||||||
err = invalid.UnmarshalText([]byte("hello world"))
|
err = invalid.UnmarshalText([]byte("hello world"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
2
uint.go
2
uint.go
|
@ -56,7 +56,7 @@ func (u *Uint) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (u *Uint) UnmarshalText(text []byte) error {
|
func (u *Uint) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
u.Valid = false
|
u.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ func (u *Uint16) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (u *Uint16) UnmarshalText(text []byte) error {
|
func (u *Uint16) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
u.Valid = false
|
u.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalUint16(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullUint16(t, blank, "UnmarshalText() empty uint16")
|
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) {
|
func TestMarshalUint16(t *testing.T) {
|
||||||
|
|
|
@ -62,7 +62,7 @@ func (u *Uint32) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (u *Uint32) UnmarshalText(text []byte) error {
|
func (u *Uint32) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
u.Valid = false
|
u.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalUint32(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullUint32(t, blank, "UnmarshalText() empty uint32")
|
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) {
|
func TestMarshalUint32(t *testing.T) {
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (u *Uint64) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (u *Uint64) UnmarshalText(text []byte) error {
|
func (u *Uint64) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
u.Valid = false
|
u.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,11 +73,6 @@ func TestTextUnmarshalUint64(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullUint64(t, blank, "UnmarshalText() empty uint64")
|
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) {
|
func TestMarshalUint64(t *testing.T) {
|
||||||
|
|
2
uint8.go
2
uint8.go
|
@ -62,7 +62,7 @@ func (u *Uint8) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||||
func (u *Uint8) UnmarshalText(text []byte) error {
|
func (u *Uint8) UnmarshalText(text []byte) error {
|
||||||
if len(text) == 0 || bytes.Equal(text, NullBytes) {
|
if text == nil || len(text) == 0 {
|
||||||
u.Valid = false
|
u.Valid = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,11 +91,6 @@ func TestTextUnmarshalUint8(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullUint8(t, blank, "UnmarshalText() empty uint8")
|
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) {
|
func TestMarshalUint8(t *testing.T) {
|
||||||
|
|
|
@ -73,11 +73,6 @@ func TestTextUnmarshalUint(t *testing.T) {
|
||||||
err = blank.UnmarshalText([]byte(""))
|
err = blank.UnmarshalText([]byte(""))
|
||||||
maybePanic(err)
|
maybePanic(err)
|
||||||
assertNullUint(t, blank, "UnmarshalText() empty uint")
|
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) {
|
func TestMarshalUint(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue