Add all additional types to zero package
* Add all additional float, int and uint types to zero package * Moved convert to its own package * Updated README to reflect changes
This commit is contained in:
parent
bbb7b84bb6
commit
4f91fe41ce
38 changed files with 4176 additions and 278 deletions
69
README.md
69
README.md
|
@ -98,16 +98,6 @@ Nullable string.
|
|||
|
||||
Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullString` JSON input.
|
||||
|
||||
#### zero.Int
|
||||
Nullable int64.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input.
|
||||
|
||||
#### zero.Float
|
||||
Nullable float64.
|
||||
|
||||
Will marshal to 0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input.
|
||||
|
||||
#### zero.Bool
|
||||
Nullable bool.
|
||||
|
||||
|
@ -117,6 +107,65 @@ Will marshal to false if null. `false` produces a null Float. Null values and ze
|
|||
|
||||
Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input.
|
||||
|
||||
#### zero.Float32
|
||||
Nullable float32.
|
||||
|
||||
Will marshal to 0 if null. 0.0 produces a null Float32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullFloat32` JSON input.
|
||||
|
||||
#### zero.Float64
|
||||
Nullable float64.
|
||||
|
||||
Will marshal to 0 if null. 0.0 produces a null Float64. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input.
|
||||
|
||||
#### zero.Int
|
||||
Nullable int.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt` JSON input.
|
||||
|
||||
#### zero.Int8
|
||||
Nullable int8.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int8. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt8` JSON input.
|
||||
|
||||
#### zero.Int16
|
||||
Nullable int16.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int16. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt16` JSON input.
|
||||
|
||||
#### zero.Int32
|
||||
Nullable int32.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt32` JSON input.
|
||||
|
||||
#### zero.Int64
|
||||
Nullable int64.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Int64. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input.
|
||||
|
||||
#### zero.Uint
|
||||
Nullable uint.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Uint. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint` JSON input.
|
||||
|
||||
#### zero.Uint8
|
||||
Nullable uint8.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Uint8. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint8` JSON input.
|
||||
|
||||
#### zero.Uint16
|
||||
Nullable uint16.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Uint16. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint16` JSON input.
|
||||
|
||||
#### zero.Uint32
|
||||
Nullable uint32.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Uint32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint32` JSON input.
|
||||
|
||||
#### zero.Uint64
|
||||
Nullable uint64.
|
||||
|
||||
Will marshal to 0 if null. 0 produces a null Uint64. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint64` JSON input.
|
||||
|
||||
### Bugs
|
||||
`json`'s `",omitempty"` struct tag does not work correctly right now. It will never omit a null or empty String. This might be [fixed eventually](https://github.com/golang/go/issues/4357).
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package null
|
||||
package convert
|
||||
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
|
@ -19,10 +19,10 @@ import (
|
|||
|
||||
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
|
||||
|
||||
// convertAssign copies to dest the value in src, converting it if possible.
|
||||
// ConvertAssign copies to dest the value in src, converting it if possible.
|
||||
// An error is returned if the copy would result in loss of information.
|
||||
// dest should be a pointer type.
|
||||
func convertAssign(dest, src interface{}) error {
|
||||
func ConvertAssign(dest, src interface{}) error {
|
||||
// Common cases, without reflect.
|
||||
switch s := src.(type) {
|
||||
case string:
|
||||
|
@ -172,7 +172,7 @@ func convertAssign(dest, src interface{}) error {
|
|||
return nil
|
||||
} else {
|
||||
dv.Set(reflect.New(dv.Type().Elem()))
|
||||
return convertAssign(dv.Interface(), src)
|
||||
return ConvertAssign(dv.Interface(), src)
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
s := asString(src)
|
382
convert/convert_test.go
Normal file
382
convert/convert_test.go
Normal file
|
@ -0,0 +1,382 @@
|
|||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// These functions are copied from database/sql/convert_test.go build 1.6.2
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var someTime = time.Unix(123, 0)
|
||||
var answer int64 = 42
|
||||
|
||||
type userDefined float64
|
||||
|
||||
type userDefinedSlice []int
|
||||
|
||||
type conversionTest struct {
|
||||
s, d interface{} // source and destination
|
||||
|
||||
// following are used if they're non-zero
|
||||
wantint int64
|
||||
wantuint uint64
|
||||
wantstr string
|
||||
wantbytes []byte
|
||||
wantraw sql.RawBytes
|
||||
wantf32 float32
|
||||
wantf64 float64
|
||||
wanttime time.Time
|
||||
wantbool bool // used if d is of type *bool
|
||||
wanterr string
|
||||
wantiface interface{}
|
||||
wantptr *int64 // if non-nil, *d's pointed value must be equal to *wantptr
|
||||
wantnil bool // if true, *d must be *int64(nil)
|
||||
wantusrdef userDefined
|
||||
}
|
||||
|
||||
// Target variables for scanning into.
|
||||
var (
|
||||
scanstr string
|
||||
scanbytes []byte
|
||||
scanraw sql.RawBytes
|
||||
scanint int
|
||||
scanint8 int8
|
||||
scanint16 int16
|
||||
scanint32 int32
|
||||
scanuint8 uint8
|
||||
scanuint16 uint16
|
||||
scanbool bool
|
||||
scanf32 float32
|
||||
scanf64 float64
|
||||
scantime time.Time
|
||||
scanptr *int64
|
||||
scaniface interface{}
|
||||
)
|
||||
|
||||
var conversionTests = []conversionTest{
|
||||
// Exact conversions (destination pointer type matches source type)
|
||||
{s: "foo", d: &scanstr, wantstr: "foo"},
|
||||
{s: 123, d: &scanint, wantint: 123},
|
||||
{s: someTime, d: &scantime, wanttime: someTime},
|
||||
|
||||
// To strings
|
||||
{s: "string", d: &scanstr, wantstr: "string"},
|
||||
{s: []byte("byteslice"), d: &scanstr, wantstr: "byteslice"},
|
||||
{s: 123, d: &scanstr, wantstr: "123"},
|
||||
{s: int8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: int64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint16(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint32(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: 1.5, d: &scanstr, wantstr: "1.5"},
|
||||
|
||||
// From time.Time:
|
||||
{s: time.Unix(1, 0).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01Z"},
|
||||
{s: time.Unix(1453874597, 0).In(time.FixedZone("here", -3600*8)), d: &scanstr, wantstr: "2016-01-26T22:03:17-08:00"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01.000000002Z"},
|
||||
{s: time.Time{}, d: &scanstr, wantstr: "0001-01-01T00:00:00Z"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanbytes, wantbytes: []byte("1970-01-01T00:00:01.000000002Z")},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scaniface, wantiface: time.Unix(1, 2).UTC()},
|
||||
|
||||
// To []byte
|
||||
{s: nil, d: &scanbytes, wantbytes: nil},
|
||||
{s: "string", d: &scanbytes, wantbytes: []byte("string")},
|
||||
{s: []byte("byteslice"), d: &scanbytes, wantbytes: []byte("byteslice")},
|
||||
{s: 123, d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint16(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint32(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: 1.5, d: &scanbytes, wantbytes: []byte("1.5")},
|
||||
|
||||
// To sql.RawBytes
|
||||
{s: nil, d: &scanraw, wantraw: nil},
|
||||
{s: []byte("byteslice"), d: &scanraw, wantraw: sql.RawBytes("byteslice")},
|
||||
{s: 123, d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: int8(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: int64(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: uint8(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: uint16(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: uint32(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: uint64(123), d: &scanraw, wantraw: sql.RawBytes("123")},
|
||||
{s: 1.5, d: &scanraw, wantraw: sql.RawBytes("1.5")},
|
||||
|
||||
// Strings to integers
|
||||
{s: "255", d: &scanuint8, wantuint: 255},
|
||||
{s: "256", d: &scanuint8, wanterr: "converting driver.Value type string (\"256\") to a uint8: value out of range"},
|
||||
{s: "256", d: &scanuint16, wantuint: 256},
|
||||
{s: "-1", d: &scanint, wantint: -1},
|
||||
{s: "foo", d: &scanint, wanterr: "converting driver.Value type string (\"foo\") to a int: invalid syntax"},
|
||||
|
||||
// int64 to smaller integers
|
||||
{s: int64(5), d: &scanuint8, wantuint: 5},
|
||||
{s: int64(256), d: &scanuint8, wanterr: "converting driver.Value type int64 (\"256\") to a uint8: value out of range"},
|
||||
{s: int64(256), d: &scanuint16, wantuint: 256},
|
||||
{s: int64(65536), d: &scanuint16, wanterr: "converting driver.Value type int64 (\"65536\") to a uint16: value out of range"},
|
||||
|
||||
// True bools
|
||||
{s: true, d: &scanbool, wantbool: true},
|
||||
{s: "True", d: &scanbool, wantbool: true},
|
||||
{s: "TRUE", d: &scanbool, wantbool: true},
|
||||
{s: "1", d: &scanbool, wantbool: true},
|
||||
{s: 1, d: &scanbool, wantbool: true},
|
||||
{s: int64(1), d: &scanbool, wantbool: true},
|
||||
{s: uint16(1), d: &scanbool, wantbool: true},
|
||||
|
||||
// False bools
|
||||
{s: false, d: &scanbool, wantbool: false},
|
||||
{s: "false", d: &scanbool, wantbool: false},
|
||||
{s: "FALSE", d: &scanbool, wantbool: false},
|
||||
{s: "0", d: &scanbool, wantbool: false},
|
||||
{s: 0, d: &scanbool, wantbool: false},
|
||||
{s: int64(0), d: &scanbool, wantbool: false},
|
||||
{s: uint16(0), d: &scanbool, wantbool: false},
|
||||
|
||||
// Not bools
|
||||
{s: "yup", d: &scanbool, wanterr: `sql/driver: couldn't convert "yup" into type bool`},
|
||||
{s: 2, d: &scanbool, wanterr: `sql/driver: couldn't convert 2 into type bool`},
|
||||
|
||||
// Floats
|
||||
{s: float64(1.5), d: &scanf64, wantf64: float64(1.5)},
|
||||
{s: int64(1), d: &scanf64, wantf64: float64(1)},
|
||||
{s: float64(1.5), d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf64, wantf64: float64(1.5)},
|
||||
|
||||
// Pointers
|
||||
{s: interface{}(nil), d: &scanptr, wantnil: true},
|
||||
{s: int64(42), d: &scanptr, wantptr: &answer},
|
||||
|
||||
// To interface{}
|
||||
{s: float64(1.5), d: &scaniface, wantiface: float64(1.5)},
|
||||
{s: int64(1), d: &scaniface, wantiface: int64(1)},
|
||||
{s: "str", d: &scaniface, wantiface: "str"},
|
||||
{s: []byte("byteslice"), d: &scaniface, wantiface: []byte("byteslice")},
|
||||
{s: true, d: &scaniface, wantiface: true},
|
||||
{s: nil, d: &scaniface},
|
||||
{s: []byte(nil), d: &scaniface, wantiface: []byte(nil)},
|
||||
|
||||
// To a user-defined type
|
||||
{s: 1.5, d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: int64(123), d: new(userDefined), wantusrdef: 123},
|
||||
{s: "1.5", d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: []byte{1, 2, 3}, d: new(userDefinedSlice), wanterr: `unsupported Scan, storing driver.Value type []uint8 into type *convert.userDefinedSlice`},
|
||||
|
||||
// Other errors
|
||||
{s: complex(1, 2), d: &scanstr, wanterr: `unsupported Scan, storing driver.Value type complex128 into type *string`},
|
||||
}
|
||||
|
||||
func intPtrValue(intptr interface{}) interface{} {
|
||||
return reflect.Indirect(reflect.Indirect(reflect.ValueOf(intptr))).Int()
|
||||
}
|
||||
|
||||
func intValue(intptr interface{}) int64 {
|
||||
return reflect.Indirect(reflect.ValueOf(intptr)).Int()
|
||||
}
|
||||
|
||||
func uintValue(intptr interface{}) uint64 {
|
||||
return reflect.Indirect(reflect.ValueOf(intptr)).Uint()
|
||||
}
|
||||
|
||||
func float64Value(ptr interface{}) float64 {
|
||||
return *(ptr.(*float64))
|
||||
}
|
||||
|
||||
func float32Value(ptr interface{}) float32 {
|
||||
return *(ptr.(*float32))
|
||||
}
|
||||
|
||||
func getTimeValue(ptr interface{}) time.Time {
|
||||
return *(ptr.(*time.Time))
|
||||
}
|
||||
|
||||
func TestConversions(t *testing.T) {
|
||||
for n, ct := range conversionTests {
|
||||
err := ConvertAssign(ct.d, ct.s)
|
||||
errstr := ""
|
||||
if err != nil {
|
||||
errstr = err.Error()
|
||||
}
|
||||
errf := func(format string, args ...interface{}) {
|
||||
base := fmt.Sprintf("ConvertAssign #%d: for %v (%T) -> %T, ", n, ct.s, ct.s, ct.d)
|
||||
t.Errorf(base+format, args...)
|
||||
}
|
||||
if errstr != ct.wanterr {
|
||||
errf("got error %q, want error %q", errstr, ct.wanterr)
|
||||
}
|
||||
if ct.wantstr != "" && ct.wantstr != scanstr {
|
||||
errf("want string %q, got %q", ct.wantstr, scanstr)
|
||||
}
|
||||
if ct.wantint != 0 && ct.wantint != intValue(ct.d) {
|
||||
errf("want int %d, got %d", ct.wantint, intValue(ct.d))
|
||||
}
|
||||
if ct.wantuint != 0 && ct.wantuint != uintValue(ct.d) {
|
||||
errf("want uint %d, got %d", ct.wantuint, uintValue(ct.d))
|
||||
}
|
||||
if ct.wantf32 != 0 && ct.wantf32 != float32Value(ct.d) {
|
||||
errf("want float32 %v, got %v", ct.wantf32, float32Value(ct.d))
|
||||
}
|
||||
if ct.wantf64 != 0 && ct.wantf64 != float64Value(ct.d) {
|
||||
errf("want float32 %v, got %v", ct.wantf64, float64Value(ct.d))
|
||||
}
|
||||
if bp, boolTest := ct.d.(*bool); boolTest && *bp != ct.wantbool && ct.wanterr == "" {
|
||||
errf("want bool %v, got %v", ct.wantbool, *bp)
|
||||
}
|
||||
if !ct.wanttime.IsZero() && !ct.wanttime.Equal(getTimeValue(ct.d)) {
|
||||
errf("want time %v, got %v", ct.wanttime, getTimeValue(ct.d))
|
||||
}
|
||||
if ct.wantnil && *ct.d.(**int64) != nil {
|
||||
errf("want nil, got %v", intPtrValue(ct.d))
|
||||
}
|
||||
if ct.wantptr != nil {
|
||||
if *ct.d.(**int64) == nil {
|
||||
errf("want pointer to %v, got nil", *ct.wantptr)
|
||||
} else if *ct.wantptr != intPtrValue(ct.d) {
|
||||
errf("want pointer to %v, got %v", *ct.wantptr, intPtrValue(ct.d))
|
||||
}
|
||||
}
|
||||
if ifptr, ok := ct.d.(*interface{}); ok {
|
||||
if !reflect.DeepEqual(ct.wantiface, scaniface) {
|
||||
errf("want interface %#v, got %#v", ct.wantiface, scaniface)
|
||||
continue
|
||||
}
|
||||
if srcBytes, ok := ct.s.([]byte); ok {
|
||||
dstBytes := (*ifptr).([]byte)
|
||||
if len(srcBytes) > 0 && &dstBytes[0] == &srcBytes[0] {
|
||||
errf("copy into interface{} didn't copy []byte data")
|
||||
}
|
||||
}
|
||||
}
|
||||
if ct.wantusrdef != 0 && ct.wantusrdef != *ct.d.(*userDefined) {
|
||||
errf("want userDefined %f, got %f", ct.wantusrdef, *ct.d.(*userDefined))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNullString(t *testing.T) {
|
||||
var ns sql.NullString
|
||||
ConvertAssign(&ns, []byte("foo"))
|
||||
if !ns.Valid {
|
||||
t.Errorf("expecting not null")
|
||||
}
|
||||
if ns.String != "foo" {
|
||||
t.Errorf("expecting foo; got %q", ns.String)
|
||||
}
|
||||
ConvertAssign(&ns, nil)
|
||||
if ns.Valid {
|
||||
t.Errorf("expecting null on nil")
|
||||
}
|
||||
if ns.String != "" {
|
||||
t.Errorf("expecting blank on nil; got %q", ns.String)
|
||||
}
|
||||
}
|
||||
|
||||
type valueConverterTest struct {
|
||||
c driver.ValueConverter
|
||||
in, out interface{}
|
||||
err string
|
||||
}
|
||||
|
||||
var valueConverterTests = []valueConverterTest{
|
||||
{driver.DefaultParameterConverter, sql.NullString{"hi", true}, "hi", ""},
|
||||
{driver.DefaultParameterConverter, sql.NullString{"", false}, nil, ""},
|
||||
}
|
||||
|
||||
func TestValueConverters(t *testing.T) {
|
||||
for i, tt := range valueConverterTests {
|
||||
out, err := tt.c.ConvertValue(tt.in)
|
||||
goterr := ""
|
||||
if err != nil {
|
||||
goterr = err.Error()
|
||||
}
|
||||
if goterr != tt.err {
|
||||
t.Errorf("test %d: %T(%T(%v)) error = %q; want error = %q",
|
||||
i, tt.c, tt.in, tt.in, goterr, tt.err)
|
||||
}
|
||||
if tt.err != "" {
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(out, tt.out) {
|
||||
t.Errorf("test %d: %T(%T(%v)) = %v (%T); want %v (%T)",
|
||||
i, tt.c, tt.in, tt.in, out, out, tt.out, tt.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that assigning to sql.RawBytes doesn't allocate (and also works).
|
||||
func TestRawBytesAllocs(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
in interface{}
|
||||
want string
|
||||
}{
|
||||
{"uint64", uint64(12345678), "12345678"},
|
||||
{"uint32", uint32(1234), "1234"},
|
||||
{"uint16", uint16(12), "12"},
|
||||
{"uint8", uint8(1), "1"},
|
||||
{"uint", uint(123), "123"},
|
||||
{"int", int(123), "123"},
|
||||
{"int8", int8(1), "1"},
|
||||
{"int16", int16(12), "12"},
|
||||
{"int32", int32(1234), "1234"},
|
||||
{"int64", int64(12345678), "12345678"},
|
||||
{"float32", float32(1.5), "1.5"},
|
||||
{"float64", float64(64), "64"},
|
||||
{"bool", false, "false"},
|
||||
}
|
||||
|
||||
buf := make(sql.RawBytes, 10)
|
||||
test := func(name string, in interface{}, want string) {
|
||||
if err := ConvertAssign(&buf, in); err != nil {
|
||||
t.Fatalf("%s: ConvertAssign = %v", name, err)
|
||||
}
|
||||
match := len(buf) == len(want)
|
||||
if match {
|
||||
for i, b := range buf {
|
||||
if want[i] != b {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
t.Fatalf("%s: got %q (len %d); want %q (len %d)", name, buf, len(buf), want, len(want))
|
||||
}
|
||||
}
|
||||
|
||||
n := testing.AllocsPerRun(100, func() {
|
||||
for _, tt := range tests {
|
||||
test(tt.name, tt.in, tt.want)
|
||||
}
|
||||
})
|
||||
|
||||
// The numbers below are only valid for 64-bit interface word sizes,
|
||||
// and gc. With 32-bit words there are more convT2E allocs, and
|
||||
// with gccgo, only pointers currently go in interface data.
|
||||
// So only care on amd64 gc for now.
|
||||
measureAllocs := runtime.GOARCH == "amd64" && runtime.Compiler == "gc"
|
||||
|
||||
if n > 0.5 && measureAllocs {
|
||||
t.Fatalf("allocs = %v; want 0", n)
|
||||
}
|
||||
|
||||
// This one involves a convT2E allocation, string -> interface{}
|
||||
n = testing.AllocsPerRun(100, func() {
|
||||
test("string", "foo", "foo")
|
||||
})
|
||||
if n > 1.5 && measureAllocs {
|
||||
t.Fatalf("allocs = %v; want max 1", n)
|
||||
}
|
||||
}
|
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullFloat32 is a replica of sql.NullFloat64 for float32 types.
|
||||
|
@ -132,7 +134,7 @@ func (n *NullFloat32) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Float32, value)
|
||||
return convert.ConvertAssign(&n.Float32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
int.go
4
int.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullInt is a replica of sql.NullInt64 for int types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullInt) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Int, value)
|
||||
return convert.ConvertAssign(&n.Int, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
int16.go
4
int16.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullInt16 is a replica of sql.NullInt64 for int16 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullInt16) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Int16, value)
|
||||
return convert.ConvertAssign(&n.Int16, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
int32.go
4
int32.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullInt32 is a replica of sql.NullInt64 for int32 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullInt32) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Int32, value)
|
||||
return convert.ConvertAssign(&n.Int32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
int8.go
4
int8.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullInt8 is a replica of sql.NullInt64 for int8 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullInt8) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Int8, value)
|
||||
return convert.ConvertAssign(&n.Int8, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
uint.go
4
uint.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint is a replica of sql.NullInt64 for uint types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullUint) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Uint, value)
|
||||
return convert.ConvertAssign(&n.Uint, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint16 is a replica of sql.NullInt64 for uint16 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullUint16) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Uint16, value)
|
||||
return convert.ConvertAssign(&n.Uint16, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint32 is a replica of sql.NullInt64 for uint32 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullUint32) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Uint32, value)
|
||||
return convert.ConvertAssign(&n.Uint32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint64 is a replica of sql.NullInt64 for uint64 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullUint64) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Uint64, value)
|
||||
return convert.ConvertAssign(&n.Uint64, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
4
uint8.go
4
uint8.go
|
@ -6,6 +6,8 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint8 is a replica of sql.NullInt64 for uint8 types.
|
||||
|
@ -133,7 +135,7 @@ func (n *NullUint8) Scan(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convertAssign(&n.Uint8, value)
|
||||
return convert.ConvertAssign(&n.Uint8, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
|
|
144
zero/float32.go
Normal file
144
zero/float32.go
Normal file
|
@ -0,0 +1,144 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullFloat32 struct {
|
||||
Float32 float32
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Float32 is a nullable float32. Zero input will be considered null.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Float32 struct {
|
||||
NullFloat32
|
||||
}
|
||||
|
||||
// NewFloat32 creates a new Float32
|
||||
func NewFloat32(f float32, valid bool) Float32 {
|
||||
return Float32{
|
||||
NullFloat32: NullFloat32{
|
||||
Float32: f,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Float32From creates a new Float32 that will be null if zero.
|
||||
func Float32From(f float32) Float32 {
|
||||
return NewFloat32(f, f != 0)
|
||||
}
|
||||
|
||||
// Float32FromPtr creates a new Float32 that be null if f is nil.
|
||||
func Float32FromPtr(f *float32) Float32 {
|
||||
if f == nil {
|
||||
return NewFloat32(0, false)
|
||||
}
|
||||
return NewFloat32(*f, true)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Float32.
|
||||
// It also supports unmarshalling a sql.NullFloat32.
|
||||
func (f *Float32) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case float64:
|
||||
f.Float32 = float32(x)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &f.NullFloat32)
|
||||
case nil:
|
||||
f.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float32", reflect.TypeOf(v).Name())
|
||||
}
|
||||
f.Valid = (err == nil) && (f.Float32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Float32 if the input is a blank, zero, or not a float.
|
||||
// It will return an error if the input is not a float, blank, or "null".
|
||||
func (f *Float32) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
f.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseFloat(string(text), 32)
|
||||
f.Float32 = float32(res)
|
||||
f.Valid = (err == nil) && (f.Float32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode null if this Float32 is null.
|
||||
func (f Float32) MarshalJSON() ([]byte, error) {
|
||||
n := f.Float32
|
||||
if !f.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatFloat(float64(n), 'f', -1, 32)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Float32 is null.
|
||||
func (f Float32) MarshalText() ([]byte, error) {
|
||||
n := f.Float32
|
||||
if !f.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatFloat(float64(n), 'f', -1, 32)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Float32's value and also sets it to be non-null.
|
||||
func (f *Float32) SetValid(v float32) {
|
||||
f.Float32 = v
|
||||
f.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a poFloater to this Float32's value, or a nil poFloater if this Float32 is null.
|
||||
func (f Float32) Ptr() *float32 {
|
||||
if !f.Valid {
|
||||
return nil
|
||||
}
|
||||
return &f.Float32
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Float32's, for future omitempty support (Go 1.4?)
|
||||
func (f Float32) IsZero() bool {
|
||||
return !f.Valid || f.Float32 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullFloat32) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Float32, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Float32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullFloat32) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Float32, nil
|
||||
}
|
180
zero/float32_test.go
Normal file
180
zero/float32_test.go
Normal file
|
@ -0,0 +1,180 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
float32JSON = []byte(`1.2345`)
|
||||
nullFloat32JSON = []byte(`{"Float32":1.2345,"Valid":true}`)
|
||||
)
|
||||
|
||||
func TestFloat32From(t *testing.T) {
|
||||
f := Float32From(1.2345)
|
||||
assertFloat32(t, f, "Float32From()")
|
||||
|
||||
zero := Float32From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Float32From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat32FromPtr(t *testing.T) {
|
||||
n := float32(1.2345)
|
||||
iptr := &n
|
||||
f := Float32FromPtr(iptr)
|
||||
assertFloat32(t, f, "Float32FromPtr()")
|
||||
|
||||
null := Float32FromPtr(nil)
|
||||
assertNullFloat32(t, null, "Float32FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalFloat32(t *testing.T) {
|
||||
var f Float32
|
||||
err := json.Unmarshal(float32JSON, &f)
|
||||
maybePanic(err)
|
||||
assertFloat32(t, f, "float json")
|
||||
|
||||
var nf Float32
|
||||
err = json.Unmarshal(nullFloat32JSON, &nf)
|
||||
maybePanic(err)
|
||||
assertFloat32(t, nf, "sql.NullFloat32 json")
|
||||
|
||||
var zero Float32
|
||||
err = json.Unmarshal(zeroJSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, zero, "zero json")
|
||||
|
||||
var null Float32
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, null, "null json")
|
||||
|
||||
var badType Float32
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullFloat32(t, badType, "wrong type json")
|
||||
|
||||
var invalid Float32
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullFloat32(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestTextUnmarshalFloat32(t *testing.T) {
|
||||
var f Float32
|
||||
err := f.UnmarshalText([]byte("1.2345"))
|
||||
maybePanic(err)
|
||||
assertFloat32(t, f, "UnmarshalText() float")
|
||||
|
||||
var zero Float32
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, zero, "UnmarshalText() zero float")
|
||||
|
||||
var blank Float32
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, blank, "UnmarshalText() empty float")
|
||||
|
||||
var null Float32
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalFloat32(t *testing.T) {
|
||||
f := Float32From(1.2345)
|
||||
data, err := json.Marshal(f)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewFloat32(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalFloat32Text(t *testing.T) {
|
||||
f := Float32From(1.2345)
|
||||
data, err := f.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewFloat32(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestFloat32Pointer(t *testing.T) {
|
||||
f := Float32From(1.2345)
|
||||
ptr := f.Ptr()
|
||||
if *ptr != 1.2345 {
|
||||
t.Errorf("bad %s Float32: %#v ≠ %v\n", "pointer", ptr, 1.2345)
|
||||
}
|
||||
|
||||
null := NewFloat32(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s Float32: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat32IsZero(t *testing.T) {
|
||||
f := Float32From(1.2345)
|
||||
if f.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewFloat32(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewFloat32(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat32SetValid(t *testing.T) {
|
||||
change := NewFloat32(0, false)
|
||||
assertNullFloat32(t, change, "SetValid()")
|
||||
change.SetValid(1.2345)
|
||||
assertFloat32(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestFloat32Scan(t *testing.T) {
|
||||
var f Float32
|
||||
err := f.Scan(1.2345)
|
||||
maybePanic(err)
|
||||
assertFloat32(t, f, "scanned float")
|
||||
|
||||
var null Float32
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullFloat32(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func assertFloat32(t *testing.T, f Float32, from string) {
|
||||
if f.Float32 != 1.2345 {
|
||||
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float32, 1.2345)
|
||||
}
|
||||
if !f.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullFloat32(t *testing.T, f Float32, from string) {
|
||||
if f.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
|
@ -8,16 +8,16 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// Float is a nullable float64. Zero input will be considered null.
|
||||
// Float64 is a nullable float64. Zero input will be considered null.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Float struct {
|
||||
type Float64 struct {
|
||||
sql.NullFloat64
|
||||
}
|
||||
|
||||
// NewFloat creates a new Float
|
||||
func NewFloat(f float64, valid bool) Float {
|
||||
return Float{
|
||||
// NewFloat64 creates a new Float64
|
||||
func NewFloat64(f float64, valid bool) Float64 {
|
||||
return Float64{
|
||||
NullFloat64: sql.NullFloat64{
|
||||
Float64: f,
|
||||
Valid: valid,
|
||||
|
@ -25,24 +25,24 @@ func NewFloat(f float64, valid bool) Float {
|
|||
}
|
||||
}
|
||||
|
||||
// FloatFrom creates a new Float that will be null if zero.
|
||||
func FloatFrom(f float64) Float {
|
||||
return NewFloat(f, f != 0)
|
||||
// Float64From creates a new Float64 that will be null if zero.
|
||||
func Float64From(f float64) Float64 {
|
||||
return NewFloat64(f, f != 0)
|
||||
}
|
||||
|
||||
// FloatFromPtr creates a new Float that be null if f is nil.
|
||||
func FloatFromPtr(f *float64) Float {
|
||||
// Float64FromPtr creates a new Float64 that be null if f is nil.
|
||||
func Float64FromPtr(f *float64) Float64 {
|
||||
if f == nil {
|
||||
return NewFloat(0, false)
|
||||
return NewFloat64(0, false)
|
||||
}
|
||||
return NewFloat(*f, true)
|
||||
return NewFloat64(*f, true)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Float.
|
||||
// 0 will be considered a null Float64.
|
||||
// It also supports unmarshalling a sql.NullFloat64.
|
||||
func (f *Float) UnmarshalJSON(data []byte) error {
|
||||
func (f *Float64) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
|
@ -57,16 +57,16 @@ func (f *Float) UnmarshalJSON(data []byte) error {
|
|||
f.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float", reflect.TypeOf(v).Name())
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float64", reflect.TypeOf(v).Name())
|
||||
}
|
||||
f.Valid = (err == nil) && (f.Float64 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Float if the input is a blank, zero, or not a float.
|
||||
// It will unmarshal to a null Float64 if the input is a blank, zero, or not a float.
|
||||
// It will return an error if the input is not a float, blank, or "null".
|
||||
func (f *Float) UnmarshalText(text []byte) error {
|
||||
func (f *Float64) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
f.Valid = false
|
||||
|
@ -79,8 +79,8 @@ func (f *Float) UnmarshalText(text []byte) error {
|
|||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode null if this Float is null.
|
||||
func (f Float) MarshalJSON() ([]byte, error) {
|
||||
// It will encode null if this Float64 is null.
|
||||
func (f Float64) MarshalJSON() ([]byte, error) {
|
||||
n := f.Float64
|
||||
if !f.Valid {
|
||||
n = 0
|
||||
|
@ -89,8 +89,8 @@ func (f Float) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Float is null.
|
||||
func (f Float) MarshalText() ([]byte, error) {
|
||||
// It will encode a zero if this Float64 is null.
|
||||
func (f Float64) MarshalText() ([]byte, error) {
|
||||
n := f.Float64
|
||||
if !f.Valid {
|
||||
n = 0
|
||||
|
@ -98,21 +98,21 @@ func (f Float) MarshalText() ([]byte, error) {
|
|||
return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Float's value and also sets it to be non-null.
|
||||
func (f *Float) SetValid(v float64) {
|
||||
// SetValid changes this Float64's value and also sets it to be non-null.
|
||||
func (f *Float64) SetValid(v float64) {
|
||||
f.Float64 = v
|
||||
f.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.
|
||||
func (f Float) Ptr() *float64 {
|
||||
// Ptr returns a poFloater to this Float64's value, or a nil poFloater if this Float64 is null.
|
||||
func (f Float64) Ptr() *float64 {
|
||||
if !f.Valid {
|
||||
return nil
|
||||
}
|
||||
return &f.Float64
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Floats, for future omitempty support (Go 1.4?)
|
||||
func (f Float) IsZero() bool {
|
||||
// IsZero returns true for null or zero Float64's, for future omitempty support (Go 1.4?)
|
||||
func (f Float64) IsZero() bool {
|
||||
return !f.Valid || f.Float64 == 0
|
||||
}
|
180
zero/float64_test.go
Normal file
180
zero/float64_test.go
Normal file
|
@ -0,0 +1,180 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
floatJSON = []byte(`1.2345`)
|
||||
nullFloat64JSON = []byte(`{"Float64":1.2345,"Valid":true}`)
|
||||
)
|
||||
|
||||
func TestFloat64From(t *testing.T) {
|
||||
f := Float64From(1.2345)
|
||||
assertFloat64(t, f, "Float64From()")
|
||||
|
||||
zero := Float64From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Float64From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64FromPtr(t *testing.T) {
|
||||
n := float64(1.2345)
|
||||
iptr := &n
|
||||
f := Float64FromPtr(iptr)
|
||||
assertFloat64(t, f, "Float64FromPtr()")
|
||||
|
||||
null := Float64FromPtr(nil)
|
||||
assertNullFloat64(t, null, "Float64FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalFloat64(t *testing.T) {
|
||||
var f Float64
|
||||
err := json.Unmarshal(floatJSON, &f)
|
||||
maybePanic(err)
|
||||
assertFloat64(t, f, "float json")
|
||||
|
||||
var nf Float64
|
||||
err = json.Unmarshal(nullFloat64JSON, &nf)
|
||||
maybePanic(err)
|
||||
assertFloat64(t, nf, "sql.NullFloat64 json")
|
||||
|
||||
var zero Float64
|
||||
err = json.Unmarshal(zeroJSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, zero, "zero json")
|
||||
|
||||
var null Float64
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, null, "null json")
|
||||
|
||||
var badType Float64
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullFloat64(t, badType, "wrong type json")
|
||||
|
||||
var invalid Float64
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullFloat64(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestTextUnmarshalFloat64(t *testing.T) {
|
||||
var f Float64
|
||||
err := f.UnmarshalText([]byte("1.2345"))
|
||||
maybePanic(err)
|
||||
assertFloat64(t, f, "UnmarshalText() float")
|
||||
|
||||
var zero Float64
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, zero, "UnmarshalText() zero float")
|
||||
|
||||
var blank Float64
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, blank, "UnmarshalText() empty float")
|
||||
|
||||
var null Float64
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalFloat64(t *testing.T) {
|
||||
f := Float64From(1.2345)
|
||||
data, err := json.Marshal(f)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewFloat64(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalFloat64Text(t *testing.T) {
|
||||
f := Float64From(1.2345)
|
||||
data, err := f.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewFloat64(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestFloat64Pointer(t *testing.T) {
|
||||
f := Float64From(1.2345)
|
||||
ptr := f.Ptr()
|
||||
if *ptr != 1.2345 {
|
||||
t.Errorf("bad %s Float64: %#v ≠ %v\n", "pointer", ptr, 1.2345)
|
||||
}
|
||||
|
||||
null := NewFloat64(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s Float64: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64IsZero(t *testing.T) {
|
||||
f := Float64From(1.2345)
|
||||
if f.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewFloat64(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewFloat64(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64SetValid(t *testing.T) {
|
||||
change := NewFloat64(0, false)
|
||||
assertNullFloat64(t, change, "SetValid()")
|
||||
change.SetValid(1.2345)
|
||||
assertFloat64(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestFloat64Scan(t *testing.T) {
|
||||
var f Float64
|
||||
err := f.Scan(1.2345)
|
||||
maybePanic(err)
|
||||
assertFloat64(t, f, "scanned float")
|
||||
|
||||
var null Float64
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullFloat64(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func assertFloat64(t *testing.T, f Float64, from string) {
|
||||
if f.Float64 != 1.2345 {
|
||||
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
|
||||
}
|
||||
if !f.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullFloat64(t *testing.T, f Float64, from string) {
|
||||
if f.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
|
@ -1,180 +0,0 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
floatJSON = []byte(`1.2345`)
|
||||
nullFloatJSON = []byte(`{"Float64":1.2345,"Valid":true}`)
|
||||
)
|
||||
|
||||
func TestFloatFrom(t *testing.T) {
|
||||
f := FloatFrom(1.2345)
|
||||
assertFloat(t, f, "FloatFrom()")
|
||||
|
||||
zero := FloatFrom(0)
|
||||
if zero.Valid {
|
||||
t.Error("FloatFrom(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatFromPtr(t *testing.T) {
|
||||
n := float64(1.2345)
|
||||
iptr := &n
|
||||
f := FloatFromPtr(iptr)
|
||||
assertFloat(t, f, "FloatFromPtr()")
|
||||
|
||||
null := FloatFromPtr(nil)
|
||||
assertNullFloat(t, null, "FloatFromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalFloat(t *testing.T) {
|
||||
var f Float
|
||||
err := json.Unmarshal(floatJSON, &f)
|
||||
maybePanic(err)
|
||||
assertFloat(t, f, "float json")
|
||||
|
||||
var nf Float
|
||||
err = json.Unmarshal(nullFloatJSON, &nf)
|
||||
maybePanic(err)
|
||||
assertFloat(t, nf, "sql.NullFloat64 json")
|
||||
|
||||
var zero Float
|
||||
err = json.Unmarshal(zeroJSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullFloat(t, zero, "zero json")
|
||||
|
||||
var null Float
|
||||
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")
|
||||
|
||||
var invalid Float
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullFloat(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestTextUnmarshalFloat(t *testing.T) {
|
||||
var f Float
|
||||
err := f.UnmarshalText([]byte("1.2345"))
|
||||
maybePanic(err)
|
||||
assertFloat(t, f, "UnmarshalText() float")
|
||||
|
||||
var zero Float
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullFloat(t, zero, "UnmarshalText() zero float")
|
||||
|
||||
var blank Float
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullFloat(t, blank, "UnmarshalText() empty float")
|
||||
|
||||
var null Float
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullFloat(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalFloat(t *testing.T) {
|
||||
f := FloatFrom(1.2345)
|
||||
data, err := json.Marshal(f)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewFloat(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalFloatText(t *testing.T) {
|
||||
f := FloatFrom(1.2345)
|
||||
data, err := f.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewFloat(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestFloatPointer(t *testing.T) {
|
||||
f := FloatFrom(1.2345)
|
||||
ptr := f.Ptr()
|
||||
if *ptr != 1.2345 {
|
||||
t.Errorf("bad %s Float: %#v ≠ %v\n", "pointer", ptr, 1.2345)
|
||||
}
|
||||
|
||||
null := NewFloat(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s Float: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatIsZero(t *testing.T) {
|
||||
f := FloatFrom(1.2345)
|
||||
if f.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewFloat(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewFloat(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatSetValid(t *testing.T) {
|
||||
change := NewFloat(0, false)
|
||||
assertNullFloat(t, change, "SetValid()")
|
||||
change.SetValid(1.2345)
|
||||
assertFloat(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func TestFloatScan(t *testing.T) {
|
||||
var f Float
|
||||
err := f.Scan(1.2345)
|
||||
maybePanic(err)
|
||||
assertFloat(t, f, "scanned float")
|
||||
|
||||
var null Float
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullFloat(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func assertFloat(t *testing.T, f Float, from string) {
|
||||
if f.Float64 != 1.2345 {
|
||||
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
|
||||
}
|
||||
if !f.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullFloat(t *testing.T, f Float, from string) {
|
||||
if f.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
74
zero/int.go
74
zero/int.go
|
@ -1,37 +1,44 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// Int is a nullable int64.
|
||||
type NullInt struct {
|
||||
Int int
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Int is a nullable int.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Int struct {
|
||||
sql.NullInt64
|
||||
NullInt
|
||||
}
|
||||
|
||||
// NewInt creates a new Int
|
||||
func NewInt(i int64, valid bool) Int {
|
||||
func NewInt(i int, valid bool) Int {
|
||||
return Int{
|
||||
NullInt64: sql.NullInt64{
|
||||
Int64: i,
|
||||
NullInt: NullInt{
|
||||
Int: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// IntFrom creates a new Int that will be null if zero.
|
||||
func IntFrom(i int64) Int {
|
||||
func IntFrom(i int) Int {
|
||||
return NewInt(i, i != 0)
|
||||
}
|
||||
|
||||
// IntFromPtr creates a new Int that be null if i is nil.
|
||||
func IntFromPtr(i *int64) Int {
|
||||
func IntFromPtr(i *int) Int {
|
||||
if i == nil {
|
||||
return NewInt(0, false)
|
||||
}
|
||||
|
@ -42,7 +49,7 @@ func IntFromPtr(i *int64) Int {
|
|||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Int.
|
||||
// It also supports unmarshalling a sql.NullInt64.
|
||||
// It also supports unmarshalling a sql.NullInt.
|
||||
func (i *Int) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
|
@ -51,17 +58,17 @@ func (i *Int) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to int64, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int64)
|
||||
// Unmarshal again, directly to int, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullInt64)
|
||||
err = json.Unmarshal(data, &i.NullInt)
|
||||
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)
|
||||
i.Valid = (err == nil) && (i.Int != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -75,46 +82,65 @@ func (i *Int) UnmarshalText(text []byte) error {
|
|||
return nil
|
||||
}
|
||||
var err error
|
||||
i.Int64, err = strconv.ParseInt(string(text), 10, 64)
|
||||
i.Valid = (err == nil) && (i.Int64 != 0)
|
||||
res, err := strconv.ParseInt(string(text), 10, 0)
|
||||
i.Int = int(res)
|
||||
i.Valid = (err == nil) && (i.Int != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Int is null.
|
||||
func (i Int) MarshalJSON() ([]byte, error) {
|
||||
n := i.Int64
|
||||
n := i.Int
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(n, 10)), nil
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Int is null.
|
||||
func (i Int) MarshalText() ([]byte, error) {
|
||||
n := i.Int64
|
||||
n := i.Int
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(n, 10)), nil
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int's value and also sets it to be non-null.
|
||||
func (i *Int) SetValid(n int64) {
|
||||
i.Int64 = n
|
||||
func (i *Int) SetValid(n int) {
|
||||
i.Int = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
|
||||
func (i Int) Ptr() *int64 {
|
||||
func (i Int) Ptr() *int {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Int64
|
||||
return &i.Int
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Ints, for future omitempty support (Go 1.4?)
|
||||
func (i Int) IsZero() bool {
|
||||
return !i.Valid || i.Int64 == 0
|
||||
return !i.Valid || i.Int == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullInt) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Int, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Int, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullInt) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Int, nil
|
||||
}
|
||||
|
|
146
zero/int16.go
Normal file
146
zero/int16.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullInt16 struct {
|
||||
Int16 int16
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Int16 is a nullable int16.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Int16 struct {
|
||||
NullInt16
|
||||
}
|
||||
|
||||
// NewInt16 creates a new Int16
|
||||
func NewInt16(i int16, valid bool) Int16 {
|
||||
return Int16{
|
||||
NullInt16: NullInt16{
|
||||
Int16: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Int16From creates a new Int16 that will be null if zero.
|
||||
func Int16From(i int16) Int16 {
|
||||
return NewInt16(i, i != 0)
|
||||
}
|
||||
|
||||
// Int16FromPtr creates a new Int16 that be null if i is nil.
|
||||
func Int16FromPtr(i *int16) Int16 {
|
||||
if i == nil {
|
||||
return NewInt16(0, false)
|
||||
}
|
||||
n := NewInt16(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Int16.
|
||||
// It also supports unmarshalling a sql.NullInt16.
|
||||
func (i *Int16) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to int16, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int16)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullInt16)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int16", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Int16 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Int16 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Int16) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseInt(string(text), 10, 16)
|
||||
i.Int16 = int16(res)
|
||||
i.Valid = (err == nil) && (i.Int16 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Int16 is null.
|
||||
func (i Int16) MarshalJSON() ([]byte, error) {
|
||||
n := i.Int16
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Int16 is null.
|
||||
func (i Int16) MarshalText() ([]byte, error) {
|
||||
n := i.Int16
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int16's value and also sets it to be non-null.
|
||||
func (i *Int16) SetValid(n int16) {
|
||||
i.Int16 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int16's value, or a nil pointer if this Int16 is null.
|
||||
func (i Int16) Ptr() *int16 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Int16
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Int16s, for future omitempty support (Go 1.4?)
|
||||
func (i Int16) IsZero() bool {
|
||||
return !i.Valid || i.Int16 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullInt16) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Int16, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Int16, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullInt16) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Int16, nil
|
||||
}
|
207
zero/int16_test.go
Normal file
207
zero/int16_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
int16JSON = []byte(`32766`)
|
||||
nullInt16JSON = []byte(`{"Int16":32766,"Valid":true}`)
|
||||
zero16JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestInt16From(t *testing.T) {
|
||||
i := Int16From(32766)
|
||||
assertInt16(t, i, "Int16From()")
|
||||
|
||||
zero := Int16From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Int16From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt16FromPtr(t *testing.T) {
|
||||
n := int16(32766)
|
||||
iptr := &n
|
||||
i := Int16FromPtr(iptr)
|
||||
assertInt16(t, i, "Int16FromPtr()")
|
||||
|
||||
null := Int16FromPtr(nil)
|
||||
assertNullInt16(t, null, "Int16FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalInt16(t *testing.T) {
|
||||
var i Int16
|
||||
err := json.Unmarshal(int16JSON, &i)
|
||||
maybePanic(err)
|
||||
assertInt16(t, i, "int json")
|
||||
|
||||
var ni Int16
|
||||
err = json.Unmarshal(nullInt16JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertInt16(t, ni, "sql.NullInt16 json")
|
||||
|
||||
var zero Int16
|
||||
err = json.Unmarshal(zero16JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, zero, "zero json")
|
||||
|
||||
var null Int16
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, null, "null json")
|
||||
|
||||
var badType Int16
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullInt16(t, badType, "wrong type json")
|
||||
|
||||
var invalid Int16
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullInt16(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumber16(t *testing.T) {
|
||||
var i Int16
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalInt16Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxInt16)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Int16
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalInt16(t *testing.T) {
|
||||
var i Int16
|
||||
err := i.UnmarshalText([]byte("32766"))
|
||||
maybePanic(err)
|
||||
assertInt16(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Int16
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Int16
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Int16
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalInt16(t *testing.T) {
|
||||
i := Int16From(32766)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "32766", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewInt16(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalInt16Text(t *testing.T) {
|
||||
i := Int16From(32766)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "32766", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewInt16(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestInt16Pointer(t *testing.T) {
|
||||
i := Int16From(32766)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 32766 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 32766)
|
||||
}
|
||||
|
||||
null := NewInt16(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt16IsZero(t *testing.T) {
|
||||
i := Int16From(32766)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewInt16(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewInt16(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt16Scan(t *testing.T) {
|
||||
var i Int16
|
||||
err := i.Scan(32766)
|
||||
maybePanic(err)
|
||||
assertInt16(t, i, "scanned int")
|
||||
|
||||
var null Int16
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullInt16(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestInt16SetValid(t *testing.T) {
|
||||
change := NewInt16(0, false)
|
||||
assertNullInt16(t, change, "SetValid()")
|
||||
change.SetValid(32766)
|
||||
assertInt16(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertInt16(t *testing.T, i Int16, from string) {
|
||||
if i.Int16 != 32766 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int16, 32766)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullInt16(t *testing.T, i Int16, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
146
zero/int32.go
Normal file
146
zero/int32.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullInt32 struct {
|
||||
Int32 int32
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Int32 is a nullable int32.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Int32 struct {
|
||||
NullInt32
|
||||
}
|
||||
|
||||
// NewInt32 creates a new Int32
|
||||
func NewInt32(i int32, valid bool) Int32 {
|
||||
return Int32{
|
||||
NullInt32: NullInt32{
|
||||
Int32: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Int32From creates a new Int32 that will be null if zero.
|
||||
func Int32From(i int32) Int32 {
|
||||
return NewInt32(i, i != 0)
|
||||
}
|
||||
|
||||
// Int32FromPtr creates a new Int32 that be null if i is nil.
|
||||
func Int32FromPtr(i *int32) Int32 {
|
||||
if i == nil {
|
||||
return NewInt32(0, false)
|
||||
}
|
||||
n := NewInt32(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Int32.
|
||||
// It also supports unmarshalling a sql.NullInt32.
|
||||
func (i *Int32) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to int32, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int32)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullInt32)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int32", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Int32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Int32 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Int32) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseInt(string(text), 10, 32)
|
||||
i.Int32 = int32(res)
|
||||
i.Valid = (err == nil) && (i.Int32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Int32 is null.
|
||||
func (i Int32) MarshalJSON() ([]byte, error) {
|
||||
n := i.Int32
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Int32 is null.
|
||||
func (i Int32) MarshalText() ([]byte, error) {
|
||||
n := i.Int32
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int32's value and also sets it to be non-null.
|
||||
func (i *Int32) SetValid(n int32) {
|
||||
i.Int32 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int32's value, or a nil pointer if this Int32 is null.
|
||||
func (i Int32) Ptr() *int32 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Int32
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Int32s, for future omitempty support (Go 1.4?)
|
||||
func (i Int32) IsZero() bool {
|
||||
return !i.Valid || i.Int32 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullInt32) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Int32, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Int32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullInt32) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Int32, nil
|
||||
}
|
207
zero/int32_test.go
Normal file
207
zero/int32_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
int32JSON = []byte(`2147483646`)
|
||||
nullInt32JSON = []byte(`{"Int32":2147483646,"Valid":true}`)
|
||||
zero32JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestInt32From(t *testing.T) {
|
||||
i := Int32From(2147483646)
|
||||
assertInt32(t, i, "Int32From()")
|
||||
|
||||
zero := Int32From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Int32From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32FromPtr(t *testing.T) {
|
||||
n := int32(2147483646)
|
||||
iptr := &n
|
||||
i := Int32FromPtr(iptr)
|
||||
assertInt32(t, i, "Int32FromPtr()")
|
||||
|
||||
null := Int32FromPtr(nil)
|
||||
assertNullInt32(t, null, "Int32FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalInt32(t *testing.T) {
|
||||
var i Int32
|
||||
err := json.Unmarshal(int32JSON, &i)
|
||||
maybePanic(err)
|
||||
assertInt32(t, i, "int json")
|
||||
|
||||
var ni Int32
|
||||
err = json.Unmarshal(nullInt32JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertInt32(t, ni, "sql.NullInt32 json")
|
||||
|
||||
var zero Int32
|
||||
err = json.Unmarshal(zero32JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, zero, "zero json")
|
||||
|
||||
var null Int32
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, null, "null json")
|
||||
|
||||
var badType Int32
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullInt32(t, badType, "wrong type json")
|
||||
|
||||
var invalid Int32
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullInt32(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumber32(t *testing.T) {
|
||||
var i Int32
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalInt32Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxInt32)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Int32
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalInt32(t *testing.T) {
|
||||
var i Int32
|
||||
err := i.UnmarshalText([]byte("2147483646"))
|
||||
maybePanic(err)
|
||||
assertInt32(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Int32
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Int32
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Int32
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalInt32(t *testing.T) {
|
||||
i := Int32From(2147483646)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "2147483646", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewInt32(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalInt32Text(t *testing.T) {
|
||||
i := Int32From(2147483646)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "2147483646", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewInt32(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestInt32Pointer(t *testing.T) {
|
||||
i := Int32From(2147483646)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 2147483646 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 2147483646)
|
||||
}
|
||||
|
||||
null := NewInt32(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32IsZero(t *testing.T) {
|
||||
i := Int32From(2147483646)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewInt32(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewInt32(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32Scan(t *testing.T) {
|
||||
var i Int32
|
||||
err := i.Scan(2147483646)
|
||||
maybePanic(err)
|
||||
assertInt32(t, i, "scanned int")
|
||||
|
||||
var null Int32
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullInt32(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestInt32SetValid(t *testing.T) {
|
||||
change := NewInt32(0, false)
|
||||
assertNullInt32(t, change, "SetValid()")
|
||||
change.SetValid(2147483646)
|
||||
assertInt32(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertInt32(t *testing.T, i Int32, from string) {
|
||||
if i.Int32 != 2147483646 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int32, 2147483646)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullInt32(t *testing.T, i Int32, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
120
zero/int64.go
Normal file
120
zero/int64.go
Normal file
|
@ -0,0 +1,120 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Int64 is a nullable int64.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Int64 struct {
|
||||
sql.NullInt64
|
||||
}
|
||||
|
||||
// NewInt64 creates a new Int64
|
||||
func NewInt64(i int64, valid bool) Int64 {
|
||||
return Int64{
|
||||
NullInt64: sql.NullInt64{
|
||||
Int64: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Int64From creates a new Int64 that will be null if zero.
|
||||
func Int64From(i int64) Int64 {
|
||||
return NewInt64(i, i != 0)
|
||||
}
|
||||
|
||||
// Int64FromPtr creates a new Int64 that be null if i is nil.
|
||||
func Int64FromPtr(i *int64) Int64 {
|
||||
if i == nil {
|
||||
return NewInt64(0, false)
|
||||
}
|
||||
n := NewInt64(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Int64.
|
||||
// It also supports unmarshalling a sql.NullInt64.
|
||||
func (i *Int64) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to int64, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int64)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullInt64)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int64", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Int64 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Int64 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Int64) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
i.Int64, err = strconv.ParseInt(string(text), 10, 64)
|
||||
i.Valid = (err == nil) && (i.Int64 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Int64 is null.
|
||||
func (i Int64) MarshalJSON() ([]byte, error) {
|
||||
n := i.Int64
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(n, 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Int64 is null.
|
||||
func (i Int64) MarshalText() ([]byte, error) {
|
||||
n := i.Int64
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(n, 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int64's value and also sets it to be non-null.
|
||||
func (i *Int64) SetValid(n int64) {
|
||||
i.Int64 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int64's value, or a nil pointer if this Int64 is null.
|
||||
func (i Int64) Ptr() *int64 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Int64
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Int64s, for future omitempty support (Go 1.4?)
|
||||
func (i Int64) IsZero() bool {
|
||||
return !i.Valid || i.Int64 == 0
|
||||
}
|
207
zero/int64_test.go
Normal file
207
zero/int64_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
int64JSON = []byte(`9223372036854775806`)
|
||||
nullInt64JSON = []byte(`{"Int64":9223372036854775806,"Valid":true}`)
|
||||
zero64JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestInt64From(t *testing.T) {
|
||||
i := Int64From(9223372036854775806)
|
||||
assertInt64(t, i, "Int64From()")
|
||||
|
||||
zero := Int64From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Int64From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64FromPtr(t *testing.T) {
|
||||
n := int64(9223372036854775806)
|
||||
iptr := &n
|
||||
i := Int64FromPtr(iptr)
|
||||
assertInt64(t, i, "Int64FromPtr()")
|
||||
|
||||
null := Int64FromPtr(nil)
|
||||
assertNullInt64(t, null, "Int64FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalInt64(t *testing.T) {
|
||||
var i Int64
|
||||
err := json.Unmarshal(int64JSON, &i)
|
||||
maybePanic(err)
|
||||
assertInt64(t, i, "int json")
|
||||
|
||||
var ni Int64
|
||||
err = json.Unmarshal(nullInt64JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertInt64(t, ni, "sql.NullInt64 json")
|
||||
|
||||
var zero Int64
|
||||
err = json.Unmarshal(zero64JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, zero, "zero json")
|
||||
|
||||
var null Int64
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, null, "null json")
|
||||
|
||||
var badType Int64
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullInt64(t, badType, "wrong type json")
|
||||
|
||||
var invalid Int64
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullInt64(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumber64(t *testing.T) {
|
||||
var i Int64
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalInt64Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxInt64)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Int64
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalInt64(t *testing.T) {
|
||||
var i Int64
|
||||
err := i.UnmarshalText([]byte("9223372036854775806"))
|
||||
maybePanic(err)
|
||||
assertInt64(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Int64
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Int64
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Int64
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalInt64(t *testing.T) {
|
||||
i := Int64From(9223372036854775806)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "9223372036854775806", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewInt64(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalInt64Text(t *testing.T) {
|
||||
i := Int64From(9223372036854775806)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "9223372036854775806", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewInt64(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestInt64Pointer(t *testing.T) {
|
||||
i := Int64From(9223372036854775806)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 9223372036854775806 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 9223372036854775806)
|
||||
}
|
||||
|
||||
null := NewInt64(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64IsZero(t *testing.T) {
|
||||
i := Int64From(9223372036854775806)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewInt64(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewInt64(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt64Scan(t *testing.T) {
|
||||
var i Int64
|
||||
err := i.Scan(9223372036854775806)
|
||||
maybePanic(err)
|
||||
assertInt64(t, i, "scanned int")
|
||||
|
||||
var null Int64
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullInt64(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestInt64SetValid(t *testing.T) {
|
||||
change := NewInt64(0, false)
|
||||
assertNullInt64(t, change, "SetValid()")
|
||||
change.SetValid(9223372036854775806)
|
||||
assertInt64(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertInt64(t *testing.T, i Int64, from string) {
|
||||
if i.Int64 != 9223372036854775806 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 9223372036854775806)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullInt64(t *testing.T, i Int64, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
146
zero/int8.go
Normal file
146
zero/int8.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullInt8 struct {
|
||||
Int8 int8
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Int8 is a nullable int8.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Int8 struct {
|
||||
NullInt8
|
||||
}
|
||||
|
||||
// NewInt8 creates a new Int8
|
||||
func NewInt8(i int8, valid bool) Int8 {
|
||||
return Int8{
|
||||
NullInt8: NullInt8{
|
||||
Int8: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Int8From creates a new Int8 that will be null if zero.
|
||||
func Int8From(i int8) Int8 {
|
||||
return NewInt8(i, i != 0)
|
||||
}
|
||||
|
||||
// Int8FromPtr creates a new Int8 that be null if i is nil.
|
||||
func Int8FromPtr(i *int8) Int8 {
|
||||
if i == nil {
|
||||
return NewInt8(0, false)
|
||||
}
|
||||
n := NewInt8(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Int8.
|
||||
// It also supports unmarshalling a sql.NullInt8.
|
||||
func (i *Int8) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to int8, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Int8)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullInt8)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int8", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Int8 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Int8 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Int8) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseInt(string(text), 10, 8)
|
||||
i.Int8 = int8(res)
|
||||
i.Valid = (err == nil) && (i.Int8 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Int8 is null.
|
||||
func (i Int8) MarshalJSON() ([]byte, error) {
|
||||
n := i.Int8
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Int8 is null.
|
||||
func (i Int8) MarshalText() ([]byte, error) {
|
||||
n := i.Int8
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatInt(int64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Int8's value and also sets it to be non-null.
|
||||
func (i *Int8) SetValid(n int8) {
|
||||
i.Int8 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Int8's value, or a nil pointer if this Int8 is null.
|
||||
func (i Int8) Ptr() *int8 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Int8
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Int8s, for future omitempty support (Go 1.4?)
|
||||
func (i Int8) IsZero() bool {
|
||||
return !i.Valid || i.Int8 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullInt8) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Int8, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Int8, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullInt8) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Int8, nil
|
||||
}
|
207
zero/int8_test.go
Normal file
207
zero/int8_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
int8JSON = []byte(`126`)
|
||||
nullInt8JSON = []byte(`{"Int8":126,"Valid":true}`)
|
||||
zero8JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestInt8From(t *testing.T) {
|
||||
i := Int8From(126)
|
||||
assertInt8(t, i, "Int8From()")
|
||||
|
||||
zero := Int8From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Int8From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt8FromPtr(t *testing.T) {
|
||||
n := int8(126)
|
||||
iptr := &n
|
||||
i := Int8FromPtr(iptr)
|
||||
assertInt8(t, i, "Int8FromPtr()")
|
||||
|
||||
null := Int8FromPtr(nil)
|
||||
assertNullInt8(t, null, "Int8FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalInt8(t *testing.T) {
|
||||
var i Int8
|
||||
err := json.Unmarshal(int8JSON, &i)
|
||||
maybePanic(err)
|
||||
assertInt8(t, i, "int json")
|
||||
|
||||
var ni Int8
|
||||
err = json.Unmarshal(nullInt8JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertInt8(t, ni, "sql.NullInt8 json")
|
||||
|
||||
var zero Int8
|
||||
err = json.Unmarshal(zero8JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, zero, "zero json")
|
||||
|
||||
var null Int8
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, null, "null json")
|
||||
|
||||
var badType Int8
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullInt8(t, badType, "wrong type json")
|
||||
|
||||
var invalid Int8
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullInt8(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumber8(t *testing.T) {
|
||||
var i Int8
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalInt8Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxInt8)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Int8
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalInt8(t *testing.T) {
|
||||
var i Int8
|
||||
err := i.UnmarshalText([]byte("126"))
|
||||
maybePanic(err)
|
||||
assertInt8(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Int8
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Int8
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Int8
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalInt8(t *testing.T) {
|
||||
i := Int8From(126)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "126", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewInt8(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalInt8Text(t *testing.T) {
|
||||
i := Int8From(126)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "126", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewInt8(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestInt8Pointer(t *testing.T) {
|
||||
i := Int8From(126)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 126 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 126)
|
||||
}
|
||||
|
||||
null := NewInt8(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt8IsZero(t *testing.T) {
|
||||
i := Int8From(126)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewInt8(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewInt8(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt8Scan(t *testing.T) {
|
||||
var i Int8
|
||||
err := i.Scan(126)
|
||||
maybePanic(err)
|
||||
assertInt8(t, i, "scanned int")
|
||||
|
||||
var null Int8
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullInt8(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestInt8SetValid(t *testing.T) {
|
||||
change := NewInt8(0, false)
|
||||
assertNullInt8(t, change, "SetValid()")
|
||||
change.SetValid(126)
|
||||
assertInt8(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertInt8(t *testing.T, i Int8, from string) {
|
||||
if i.Int8 != 126 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int8, 126)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullInt8(t *testing.T, i Int8, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
|
@ -2,14 +2,12 @@ package zero
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
intJSON = []byte(`12345`)
|
||||
nullIntJSON = []byte(`{"Int64":12345,"Valid":true}`)
|
||||
nullIntJSON = []byte(`{"Int":12345,"Valid":true}`)
|
||||
zeroJSON = []byte(`0`)
|
||||
)
|
||||
|
||||
|
@ -24,7 +22,7 @@ func TestIntFrom(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIntFromPtr(t *testing.T) {
|
||||
n := int64(12345)
|
||||
n := int(12345)
|
||||
iptr := &n
|
||||
i := IntFromPtr(iptr)
|
||||
assertInt(t, i, "IntFromPtr()")
|
||||
|
@ -42,7 +40,7 @@ func TestUnmarshalInt(t *testing.T) {
|
|||
var ni Int
|
||||
err = json.Unmarshal(nullIntJSON, &ni)
|
||||
maybePanic(err)
|
||||
assertInt(t, ni, "sql.NullInt64 json")
|
||||
assertInt(t, ni, "sql.NullInt json")
|
||||
|
||||
var zero Int
|
||||
err = json.Unmarshal(zeroJSON, &zero)
|
||||
|
@ -77,22 +75,6 @@ func TestUnmarshalNonIntegerNumber(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalInt64Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxInt64)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Int
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalInt(t *testing.T) {
|
||||
var i Int
|
||||
err := i.UnmarshalText([]byte("12345"))
|
||||
|
@ -192,8 +174,8 @@ func TestIntSetValid(t *testing.T) {
|
|||
}
|
||||
|
||||
func assertInt(t *testing.T, i Int, from string) {
|
||||
if i.Int64 != 12345 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345)
|
||||
if i.Int != 12345 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int, 12345)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
|
|
146
zero/uint.go
Normal file
146
zero/uint.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullUint struct {
|
||||
Uint uint
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Uint is a nullable uint.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Uint struct {
|
||||
NullUint
|
||||
}
|
||||
|
||||
// NewUint creates a new Uint
|
||||
func NewUint(i uint, valid bool) Uint {
|
||||
return Uint{
|
||||
NullUint: NullUint{
|
||||
Uint: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// UintFrom creates a new Uint that will be null if zero.
|
||||
func UintFrom(i uint) Uint {
|
||||
return NewUint(i, i != 0)
|
||||
}
|
||||
|
||||
// UintFromPtr creates a new Uint that be null if i is nil.
|
||||
func UintFromPtr(i *uint) Uint {
|
||||
if i == nil {
|
||||
return NewUint(0, false)
|
||||
}
|
||||
n := NewUint(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Uint.
|
||||
// It also supports unmarshalling a sql.NullUint.
|
||||
func (i *Uint) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to uint, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Uint)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullUint)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Uint", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Uint != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Uint if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Uint) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseUint(string(text), 10, 0)
|
||||
i.Uint = uint(res)
|
||||
i.Valid = (err == nil) && (i.Uint != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Uint is null.
|
||||
func (i Uint) MarshalJSON() ([]byte, error) {
|
||||
n := i.Uint
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Uint is null.
|
||||
func (i Uint) MarshalText() ([]byte, error) {
|
||||
n := i.Uint
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Uint's value and also sets it to be non-null.
|
||||
func (i *Uint) SetValid(n uint) {
|
||||
i.Uint = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Uint's value, or a nil pointer if this Uint is null.
|
||||
func (i Uint) Ptr() *uint {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Uint
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Uints, for future omitempty support (Go 1.4?)
|
||||
func (i Uint) IsZero() bool {
|
||||
return !i.Valid || i.Uint == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullUint) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Uint, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Uint, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullUint) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Uint, nil
|
||||
}
|
146
zero/uint16.go
Normal file
146
zero/uint16.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullUint16 struct {
|
||||
Uint16 uint16
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Uint16 is a nullable uint16.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Uint16 struct {
|
||||
NullUint16
|
||||
}
|
||||
|
||||
// NewUint16 creates a new Uint16
|
||||
func NewUint16(i uint16, valid bool) Uint16 {
|
||||
return Uint16{
|
||||
NullUint16: NullUint16{
|
||||
Uint16: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Uint16From creates a new Uint16 that will be null if zero.
|
||||
func Uint16From(i uint16) Uint16 {
|
||||
return NewUint16(i, i != 0)
|
||||
}
|
||||
|
||||
// Uint16FromPtr creates a new Uint16 that be null if i is nil.
|
||||
func Uint16FromPtr(i *uint16) Uint16 {
|
||||
if i == nil {
|
||||
return NewUint16(0, false)
|
||||
}
|
||||
n := NewUint16(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Uint16.
|
||||
// It also supports unmarshalling a sql.NullUint16.
|
||||
func (i *Uint16) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to uint16, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Uint16)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullUint16)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Uint16", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Uint16 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Uint16 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Uint16) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseUint(string(text), 10, 16)
|
||||
i.Uint16 = uint16(res)
|
||||
i.Valid = (err == nil) && (i.Uint16 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Uint16 is null.
|
||||
func (i Uint16) MarshalJSON() ([]byte, error) {
|
||||
n := i.Uint16
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Uint16 is null.
|
||||
func (i Uint16) MarshalText() ([]byte, error) {
|
||||
n := i.Uint16
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Uint16's value and also sets it to be non-null.
|
||||
func (i *Uint16) SetValid(n uint16) {
|
||||
i.Uint16 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Uint16's value, or a nil pointer if this Uint16 is null.
|
||||
func (i Uint16) Ptr() *uint16 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Uint16
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Uint16s, for future omitempty support (Go 1.4?)
|
||||
func (i Uint16) IsZero() bool {
|
||||
return !i.Valid || i.Uint16 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullUint16) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Uint16, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Uint16, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullUint16) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Uint16, nil
|
||||
}
|
207
zero/uint16_test.go
Normal file
207
zero/uint16_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
uint16JSON = []byte(`65534`)
|
||||
nullUint16JSON = []byte(`{"Uint16":65534,"Valid":true}`)
|
||||
zeroU16JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestUint16From(t *testing.T) {
|
||||
i := Uint16From(65534)
|
||||
assertUint16(t, i, "Uint16From()")
|
||||
|
||||
zero := Uint16From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Uint16From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint16FromPtr(t *testing.T) {
|
||||
n := uint16(65534)
|
||||
iptr := &n
|
||||
i := Uint16FromPtr(iptr)
|
||||
assertUint16(t, i, "Uint16FromPtr()")
|
||||
|
||||
null := Uint16FromPtr(nil)
|
||||
assertNullUint16(t, null, "Uint16FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalUint16(t *testing.T) {
|
||||
var i Uint16
|
||||
err := json.Unmarshal(uint16JSON, &i)
|
||||
maybePanic(err)
|
||||
assertUint16(t, i, "int json")
|
||||
|
||||
var ni Uint16
|
||||
err = json.Unmarshal(nullUint16JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertUint16(t, ni, "sql.NullUint16 json")
|
||||
|
||||
var zero Uint16
|
||||
err = json.Unmarshal(zeroU16JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, zero, "zero json")
|
||||
|
||||
var null Uint16
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, null, "null json")
|
||||
|
||||
var badType Uint16
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullUint16(t, badType, "wrong type json")
|
||||
|
||||
var invalid Uint16
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullUint16(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumberU16(t *testing.T) {
|
||||
var i Uint16
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalUint16Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxUint16)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Uint16
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalUint16(t *testing.T) {
|
||||
var i Uint16
|
||||
err := i.UnmarshalText([]byte("65534"))
|
||||
maybePanic(err)
|
||||
assertUint16(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Uint16
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Uint16
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Uint16
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalUint16(t *testing.T) {
|
||||
i := Uint16From(65534)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "65534", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewUint16(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalUint16Text(t *testing.T) {
|
||||
i := Uint16From(65534)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "65534", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewUint16(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestUint16Pointer(t *testing.T) {
|
||||
i := Uint16From(65534)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 65534 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 65534)
|
||||
}
|
||||
|
||||
null := NewUint16(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint16IsZero(t *testing.T) {
|
||||
i := Uint16From(65534)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewUint16(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewUint16(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint16Scan(t *testing.T) {
|
||||
var i Uint16
|
||||
err := i.Scan(65534)
|
||||
maybePanic(err)
|
||||
assertUint16(t, i, "scanned int")
|
||||
|
||||
var null Uint16
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullUint16(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestUint16SetValid(t *testing.T) {
|
||||
change := NewUint16(0, false)
|
||||
assertNullUint16(t, change, "SetValid()")
|
||||
change.SetValid(65534)
|
||||
assertUint16(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertUint16(t *testing.T, i Uint16, from string) {
|
||||
if i.Uint16 != 65534 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Uint16, 65534)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullUint16(t *testing.T, i Uint16, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
146
zero/uint32.go
Normal file
146
zero/uint32.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullUint32 struct {
|
||||
Uint32 uint32
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Uint32 is a nullable uint32.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Uint32 struct {
|
||||
NullUint32
|
||||
}
|
||||
|
||||
// NewUint32 creates a new Uint32
|
||||
func NewUint32(i uint32, valid bool) Uint32 {
|
||||
return Uint32{
|
||||
NullUint32: NullUint32{
|
||||
Uint32: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Uint32From creates a new Uint32 that will be null if zero.
|
||||
func Uint32From(i uint32) Uint32 {
|
||||
return NewUint32(i, i != 0)
|
||||
}
|
||||
|
||||
// Uint32FromPtr creates a new Uint32 that be null if i is nil.
|
||||
func Uint32FromPtr(i *uint32) Uint32 {
|
||||
if i == nil {
|
||||
return NewUint32(0, false)
|
||||
}
|
||||
n := NewUint32(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Uint32.
|
||||
// It also supports unmarshalling a sql.NullUint32.
|
||||
func (i *Uint32) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to uint32, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Uint32)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullUint32)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Uint32", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Uint32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Uint32 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Uint32) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseUint(string(text), 10, 32)
|
||||
i.Uint32 = uint32(res)
|
||||
i.Valid = (err == nil) && (i.Uint32 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Uint32 is null.
|
||||
func (i Uint32) MarshalJSON() ([]byte, error) {
|
||||
n := i.Uint32
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Uint32 is null.
|
||||
func (i Uint32) MarshalText() ([]byte, error) {
|
||||
n := i.Uint32
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Uint32's value and also sets it to be non-null.
|
||||
func (i *Uint32) SetValid(n uint32) {
|
||||
i.Uint32 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Uint32's value, or a nil pointer if this Uint32 is null.
|
||||
func (i Uint32) Ptr() *uint32 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Uint32
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Uint32s, for future omitempty support (Go 1.4?)
|
||||
func (i Uint32) IsZero() bool {
|
||||
return !i.Valid || i.Uint32 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullUint32) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Uint32, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Uint32, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullUint32) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Uint32, nil
|
||||
}
|
207
zero/uint32_test.go
Normal file
207
zero/uint32_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
uint32JSON = []byte(`4294967294`)
|
||||
nullUint32JSON = []byte(`{"Uint32":4294967294,"Valid":true}`)
|
||||
zeroU32JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestUint32From(t *testing.T) {
|
||||
i := Uint32From(4294967294)
|
||||
assertUint32(t, i, "Uint32From()")
|
||||
|
||||
zero := Uint32From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Uint32From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint32FromPtr(t *testing.T) {
|
||||
n := uint32(4294967294)
|
||||
iptr := &n
|
||||
i := Uint32FromPtr(iptr)
|
||||
assertUint32(t, i, "Uint32FromPtr()")
|
||||
|
||||
null := Uint32FromPtr(nil)
|
||||
assertNullUint32(t, null, "Uint32FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalUint32(t *testing.T) {
|
||||
var i Uint32
|
||||
err := json.Unmarshal(uint32JSON, &i)
|
||||
maybePanic(err)
|
||||
assertUint32(t, i, "int json")
|
||||
|
||||
var ni Uint32
|
||||
err = json.Unmarshal(nullUint32JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertUint32(t, ni, "sql.NullUint32 json")
|
||||
|
||||
var zero Uint32
|
||||
err = json.Unmarshal(zeroU32JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, zero, "zero json")
|
||||
|
||||
var null Uint32
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, null, "null json")
|
||||
|
||||
var badType Uint32
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullUint32(t, badType, "wrong type json")
|
||||
|
||||
var invalid Uint32
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullUint32(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumberU32(t *testing.T) {
|
||||
var i Uint32
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalUint32Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxUint32)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Uint32
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalUint32(t *testing.T) {
|
||||
var i Uint32
|
||||
err := i.UnmarshalText([]byte("4294967294"))
|
||||
maybePanic(err)
|
||||
assertUint32(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Uint32
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Uint32
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Uint32
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalUint32(t *testing.T) {
|
||||
i := Uint32From(4294967294)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "4294967294", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewUint32(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalUint32Text(t *testing.T) {
|
||||
i := Uint32From(4294967294)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "4294967294", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewUint32(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestUint32Pointer(t *testing.T) {
|
||||
i := Uint32From(4294967294)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 4294967294 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 4294967294)
|
||||
}
|
||||
|
||||
null := NewUint32(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint32IsZero(t *testing.T) {
|
||||
i := Uint32From(4294967294)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewUint32(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewUint32(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint32Scan(t *testing.T) {
|
||||
var i Uint32
|
||||
err := i.Scan(4294967294)
|
||||
maybePanic(err)
|
||||
assertUint32(t, i, "scanned int")
|
||||
|
||||
var null Uint32
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullUint32(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestUint32SetValid(t *testing.T) {
|
||||
change := NewUint32(0, false)
|
||||
assertNullUint32(t, change, "SetValid()")
|
||||
change.SetValid(4294967294)
|
||||
assertUint32(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertUint32(t *testing.T, i Uint32, from string) {
|
||||
if i.Uint32 != 4294967294 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Uint32, 4294967294)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullUint32(t *testing.T, i Uint32, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
146
zero/uint64.go
Normal file
146
zero/uint64.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
// NullUint64 is a replica of sql.NullInt64 for uint64 types.
|
||||
type NullUint64 struct {
|
||||
Uint64 uint64
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Uint64 is an nullable uint64.
|
||||
// It does not consider zero values to be null.
|
||||
// It will decode to null, not zero, if null.
|
||||
type Uint64 struct {
|
||||
NullUint64
|
||||
}
|
||||
|
||||
// NewUint64 creates a new Uint64
|
||||
func NewUint64(i uint64, valid bool) Uint64 {
|
||||
return Uint64{
|
||||
NullUint64: NullUint64{
|
||||
Uint64: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Uint64From creates a new Uint64 that will be null if zero.
|
||||
func Uint64From(i uint64) Uint64 {
|
||||
return NewUint64(i, i != 0)
|
||||
}
|
||||
|
||||
// Uint64FromPtr creates a new Uint64 that be null if i is nil.
|
||||
func Uint64FromPtr(i *uint64) Uint64 {
|
||||
if i == nil {
|
||||
return NewUint64(0, false)
|
||||
}
|
||||
n := NewUint64(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Uint64.
|
||||
// It also supports unmarshalling a sql.NullUint64.
|
||||
func (i *Uint64) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to uint64, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Uint64)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullUint64)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Uint64", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Uint64 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Uint64 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Uint64) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
i.Uint64, err = strconv.ParseUint(string(text), 10, 64)
|
||||
i.Valid = (err == nil) && (i.Uint64 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Uint64 is null.
|
||||
func (i Uint64) MarshalJSON() ([]byte, error) {
|
||||
n := i.Uint64
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(n, 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Uint64 is null.
|
||||
func (i Uint64) MarshalText() ([]byte, error) {
|
||||
n := i.Uint64
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(n, 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Uint64's value and also sets it to be non-null.
|
||||
func (i *Uint64) SetValid(n uint64) {
|
||||
i.Uint64 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Uint64's value, or a nil pointer if this Uint64 is null.
|
||||
func (i Uint64) Ptr() *uint64 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Uint64
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Uint64s, for future omitempty support (Go 1.4?)
|
||||
func (i Uint64) IsZero() bool {
|
||||
return !i.Valid || i.Uint64 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullUint64) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Uint64, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Uint64, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullUint64) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Uint64, nil
|
||||
}
|
189
zero/uint64_test.go
Normal file
189
zero/uint64_test.go
Normal file
|
@ -0,0 +1,189 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
uint64JSON = []byte(`18446744073709551614`)
|
||||
nullUint64JSON = []byte(`{"Uint64":18446744073709551614,"Valid":true}`)
|
||||
zeroU64JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestUint64From(t *testing.T) {
|
||||
i := Uint64From(18446744073709551614)
|
||||
assertUint64(t, i, "Uint64From()")
|
||||
|
||||
zero := Uint64From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Uint64From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint64FromPtr(t *testing.T) {
|
||||
n := uint64(18446744073709551614)
|
||||
iptr := &n
|
||||
i := Uint64FromPtr(iptr)
|
||||
assertUint64(t, i, "Uint64FromPtr()")
|
||||
|
||||
null := Uint64FromPtr(nil)
|
||||
assertNullUint64(t, null, "Uint64FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalUint64(t *testing.T) {
|
||||
var i Uint64
|
||||
err := json.Unmarshal(uint64JSON, &i)
|
||||
maybePanic(err)
|
||||
assertUint64(t, i, "int json")
|
||||
|
||||
var ni Uint64
|
||||
err = json.Unmarshal(nullUint64JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertUint64(t, ni, "sql.NullUint64 json")
|
||||
|
||||
var zero Uint64
|
||||
err = json.Unmarshal(zeroU64JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, zero, "zero json")
|
||||
|
||||
var null Uint64
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, null, "null json")
|
||||
|
||||
var badType Uint64
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullUint64(t, badType, "wrong type json")
|
||||
|
||||
var invalid Uint64
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullUint64(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumberU64(t *testing.T) {
|
||||
var i Uint64
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalUint64(t *testing.T) {
|
||||
var i Uint64
|
||||
err := i.UnmarshalText([]byte("18446744073709551614"))
|
||||
maybePanic(err)
|
||||
assertUint64(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Uint64
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Uint64
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Uint64
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalUint64(t *testing.T) {
|
||||
i := Uint64From(18446744073709551614)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "18446744073709551614", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewUint64(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalUint64Text(t *testing.T) {
|
||||
i := Uint64From(18446744073709551614)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "18446744073709551614", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewUint64(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestUint64Pointer(t *testing.T) {
|
||||
i := Uint64From(18446744073709551614)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 18446744073709551614 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, uint64(18446744073709551614))
|
||||
}
|
||||
|
||||
null := NewUint64(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint64IsZero(t *testing.T) {
|
||||
i := Uint64From(18446744073709551614)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewUint64(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewUint64(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint64Scan(t *testing.T) {
|
||||
var i Uint64
|
||||
err := i.Scan(uint64(18446744073709551614))
|
||||
maybePanic(err)
|
||||
assertUint64(t, i, "scanned int")
|
||||
|
||||
var null Uint64
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullUint64(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestUint64SetValid(t *testing.T) {
|
||||
change := NewUint64(0, false)
|
||||
assertNullUint64(t, change, "SetValid()")
|
||||
change.SetValid(18446744073709551614)
|
||||
assertUint64(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertUint64(t *testing.T, i Uint64, from string) {
|
||||
if i.Uint64 != 18446744073709551614 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Uint64, uint64(18446744073709551614))
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullUint64(t *testing.T, i Uint64, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
146
zero/uint8.go
Normal file
146
zero/uint8.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pobri19/null-extended/convert"
|
||||
)
|
||||
|
||||
type NullUint8 struct {
|
||||
Uint8 uint8
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Uint8 is a nullable uint8.
|
||||
// JSON marshals to zero if null.
|
||||
// Considered null to SQL if zero.
|
||||
type Uint8 struct {
|
||||
NullUint8
|
||||
}
|
||||
|
||||
// NewUint8 creates a new Uint8
|
||||
func NewUint8(i uint8, valid bool) Uint8 {
|
||||
return Uint8{
|
||||
NullUint8: NullUint8{
|
||||
Uint8: i,
|
||||
Valid: valid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Uint8From creates a new Uint8 that will be null if zero.
|
||||
func Uint8From(i uint8) Uint8 {
|
||||
return NewUint8(i, i != 0)
|
||||
}
|
||||
|
||||
// Uint8FromPtr creates a new Uint8 that be null if i is nil.
|
||||
func Uint8FromPtr(i *uint8) Uint8 {
|
||||
if i == nil {
|
||||
return NewUint8(0, false)
|
||||
}
|
||||
n := NewUint8(*i, true)
|
||||
return n
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
// It supports number and null input.
|
||||
// 0 will be considered a null Uint8.
|
||||
// It also supports unmarshalling a sql.NullUint8.
|
||||
func (i *Uint8) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
var v interface{}
|
||||
if err = json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
// Unmarshal again, directly to uint8, to avoid intermediate float64
|
||||
err = json.Unmarshal(data, &i.Uint8)
|
||||
case map[string]interface{}:
|
||||
err = json.Unmarshal(data, &i.NullUint8)
|
||||
case nil:
|
||||
i.Valid = false
|
||||
return nil
|
||||
default:
|
||||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Uint8", reflect.TypeOf(v).Name())
|
||||
}
|
||||
i.Valid = (err == nil) && (i.Uint8 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
// It will unmarshal to a null Uint8 if the input is a blank, zero, or not an integer.
|
||||
// It will return an error if the input is not an integer, blank, or "null".
|
||||
func (i *Uint8) UnmarshalText(text []byte) error {
|
||||
str := string(text)
|
||||
if str == "" || str == "null" {
|
||||
i.Valid = false
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
res, err := strconv.ParseUint(string(text), 10, 8)
|
||||
i.Uint8 = uint8(res)
|
||||
i.Valid = (err == nil) && (i.Uint8 != 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
// It will encode 0 if this Uint8 is null.
|
||||
func (i Uint8) MarshalJSON() ([]byte, error) {
|
||||
n := i.Uint8
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
// It will encode a zero if this Uint8 is null.
|
||||
func (i Uint8) MarshalText() ([]byte, error) {
|
||||
n := i.Uint8
|
||||
if !i.Valid {
|
||||
n = 0
|
||||
}
|
||||
return []byte(strconv.FormatUint(uint64(n), 10)), nil
|
||||
}
|
||||
|
||||
// SetValid changes this Uint8's value and also sets it to be non-null.
|
||||
func (i *Uint8) SetValid(n uint8) {
|
||||
i.Uint8 = n
|
||||
i.Valid = true
|
||||
}
|
||||
|
||||
// Ptr returns a pointer to this Uint8's value, or a nil pointer if this Uint8 is null.
|
||||
func (i Uint8) Ptr() *uint8 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
return &i.Uint8
|
||||
}
|
||||
|
||||
// IsZero returns true for null or zero Uint8s, for future omitempty support (Go 1.4?)
|
||||
func (i Uint8) IsZero() bool {
|
||||
return !i.Valid || i.Uint8 == 0
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *NullUint8) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
n.Uint8, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
return convert.ConvertAssign(&n.Uint8, value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (n NullUint8) Value() (driver.Value, error) {
|
||||
if !n.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return n.Uint8, nil
|
||||
}
|
207
zero/uint8_test.go
Normal file
207
zero/uint8_test.go
Normal file
|
@ -0,0 +1,207 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
uint8JSON = []byte(`254`)
|
||||
nullUint8JSON = []byte(`{"Uint8":254,"Valid":true}`)
|
||||
zeroU8JSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestUint8From(t *testing.T) {
|
||||
i := Uint8From(254)
|
||||
assertUint8(t, i, "Uint8From()")
|
||||
|
||||
zero := Uint8From(0)
|
||||
if zero.Valid {
|
||||
t.Error("Uint8From(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8FromPtr(t *testing.T) {
|
||||
n := uint8(254)
|
||||
iptr := &n
|
||||
i := Uint8FromPtr(iptr)
|
||||
assertUint8(t, i, "Uint8FromPtr()")
|
||||
|
||||
null := Uint8FromPtr(nil)
|
||||
assertNullUint8(t, null, "Uint8FromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalUint8(t *testing.T) {
|
||||
var i Uint8
|
||||
err := json.Unmarshal(uint8JSON, &i)
|
||||
maybePanic(err)
|
||||
assertUint8(t, i, "int json")
|
||||
|
||||
var ni Uint8
|
||||
err = json.Unmarshal(nullUint8JSON, &ni)
|
||||
maybePanic(err)
|
||||
assertUint8(t, ni, "sql.NullUint8 json")
|
||||
|
||||
var zero Uint8
|
||||
err = json.Unmarshal(zeroU8JSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, zero, "zero json")
|
||||
|
||||
var null Uint8
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, null, "null json")
|
||||
|
||||
var badType Uint8
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullUint8(t, badType, "wrong type json")
|
||||
|
||||
var invalid Uint8
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullUint8(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonIntegerNumberU8(t *testing.T) {
|
||||
var i Uint8
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-integer number coerced to int")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalUint8Overflow(t *testing.T) {
|
||||
int64Overflow := uint64(math.MaxUint8)
|
||||
|
||||
// Max int64 should decode successfully
|
||||
var i Uint8
|
||||
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
maybePanic(err)
|
||||
|
||||
// Attempt to overflow
|
||||
int64Overflow++
|
||||
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||
if err == nil {
|
||||
panic("err should be present; decoded value overflows int64")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalUint8(t *testing.T) {
|
||||
var i Uint8
|
||||
err := i.UnmarshalText([]byte("254"))
|
||||
maybePanic(err)
|
||||
assertUint8(t, i, "UnmarshalText() int")
|
||||
|
||||
var zero Uint8
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, zero, "UnmarshalText() zero int")
|
||||
|
||||
var blank Uint8
|
||||
err = blank.UnmarshalText([]byte(""))
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, blank, "UnmarshalText() empty int")
|
||||
|
||||
var null Uint8
|
||||
err = null.UnmarshalText([]byte("null"))
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, null, `UnmarshalText() "null"`)
|
||||
}
|
||||
|
||||
func TestMarshalUint8(t *testing.T) {
|
||||
i := Uint8From(254)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "254", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewUint8(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalUint8Text(t *testing.T) {
|
||||
i := Uint8From(254)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "254", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewUint8(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestUint8Pointer(t *testing.T) {
|
||||
i := Uint8From(254)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 254 {
|
||||
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 254)
|
||||
}
|
||||
|
||||
null := NewUint8(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8IsZero(t *testing.T) {
|
||||
i := Uint8From(254)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewUint8(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewUint8(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8Scan(t *testing.T) {
|
||||
var i Uint8
|
||||
err := i.Scan(254)
|
||||
maybePanic(err)
|
||||
assertUint8(t, i, "scanned int")
|
||||
|
||||
var null Uint8
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullUint8(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestUint8SetValid(t *testing.T) {
|
||||
change := NewUint8(0, false)
|
||||
assertNullUint8(t, change, "SetValid()")
|
||||
change.SetValid(254)
|
||||
assertUint8(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertUint8(t *testing.T, i Uint8, from string) {
|
||||
if i.Uint8 != 254 {
|
||||
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Uint8, 254)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullUint8(t *testing.T, i Uint8, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
189
zero/uint_test.go
Normal file
189
zero/uint_test.go
Normal file
|
@ -0,0 +1,189 @@
|
|||
package zero
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
uintJSON = []byte(`12345`)
|
||||
nullUintJSON = []byte(`{"Uint":12345,"Valid":true}`)
|
||||
zeroUJSON = []byte(`0`)
|
||||
)
|
||||
|
||||
func TestUintFrom(t *testing.T) {
|
||||
i := UintFrom(12345)
|
||||
assertUint(t, i, "UintFrom()")
|
||||
|
||||
zero := UintFrom(0)
|
||||
if zero.Valid {
|
||||
t.Error("UintFrom(0)", "is valid, but should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintFromPtr(t *testing.T) {
|
||||
n := uint(12345)
|
||||
iptr := &n
|
||||
i := UintFromPtr(iptr)
|
||||
assertUint(t, i, "UintFromPtr()")
|
||||
|
||||
null := UintFromPtr(nil)
|
||||
assertNullUint(t, null, "UintFromPtr(nil)")
|
||||
}
|
||||
|
||||
func TestUnmarshalUint(t *testing.T) {
|
||||
var i Uint
|
||||
err := json.Unmarshal(uintJSON, &i)
|
||||
maybePanic(err)
|
||||
assertUint(t, i, "uint json")
|
||||
|
||||
var ni Uint
|
||||
err = json.Unmarshal(nullUintJSON, &ni)
|
||||
maybePanic(err)
|
||||
assertUint(t, ni, "sql.NullUint json")
|
||||
|
||||
var zero Uint
|
||||
err = json.Unmarshal(zeroUJSON, &zero)
|
||||
maybePanic(err)
|
||||
assertNullUint(t, zero, "zero json")
|
||||
|
||||
var null Uint
|
||||
err = json.Unmarshal(nullJSON, &null)
|
||||
maybePanic(err)
|
||||
assertNullUint(t, null, "null json")
|
||||
|
||||
var badType Uint
|
||||
err = json.Unmarshal(boolJSON, &badType)
|
||||
if err == nil {
|
||||
panic("err should not be nil")
|
||||
}
|
||||
assertNullUint(t, badType, "wrong type json")
|
||||
|
||||
var invalid Uint
|
||||
err = invalid.UnmarshalJSON(invalidJSON)
|
||||
if _, ok := err.(*json.SyntaxError); !ok {
|
||||
t.Errorf("expected json.SyntaxError, not %T", err)
|
||||
}
|
||||
assertNullUint(t, invalid, "invalid json")
|
||||
}
|
||||
|
||||
func TestUnmarshalNonUintegerNumber(t *testing.T) {
|
||||
var i Uint
|
||||
err := json.Unmarshal(floatJSON, &i)
|
||||
if err == nil {
|
||||
panic("err should be present; non-uinteger number coerced to uint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextUnmarshalUint(t *testing.T) {
|
||||
var i Uint
|
||||
err := i.UnmarshalText([]byte("12345"))
|
||||
maybePanic(err)
|
||||
assertUint(t, i, "UnmarshalText() uint")
|
||||
|
||||
var zero Uint
|
||||
err = zero.UnmarshalText([]byte("0"))
|
||||
maybePanic(err)
|
||||
assertNullUint(t, zero, "UnmarshalText() zero uint")
|
||||
|
||||
var blank Uint
|
||||
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) {
|
||||
i := UintFrom(12345)
|
||||
data, err := json.Marshal(i)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "12345", "non-empty json marshal")
|
||||
|
||||
// invalid values should be encoded as 0
|
||||
null := NewUint(0, false)
|
||||
data, err = json.Marshal(null)
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null json marshal")
|
||||
}
|
||||
|
||||
func TestMarshalUintText(t *testing.T) {
|
||||
i := UintFrom(12345)
|
||||
data, err := i.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "12345", "non-empty text marshal")
|
||||
|
||||
// invalid values should be encoded as zero
|
||||
null := NewUint(0, false)
|
||||
data, err = null.MarshalText()
|
||||
maybePanic(err)
|
||||
assertJSONEquals(t, data, "0", "null text marshal")
|
||||
}
|
||||
|
||||
func TestUintPouinter(t *testing.T) {
|
||||
i := UintFrom(12345)
|
||||
ptr := i.Ptr()
|
||||
if *ptr != 12345 {
|
||||
t.Errorf("bad %s uint: %#v ≠ %d\n", "pouinter", ptr, 12345)
|
||||
}
|
||||
|
||||
null := NewUint(0, false)
|
||||
ptr = null.Ptr()
|
||||
if ptr != nil {
|
||||
t.Errorf("bad %s uint: %#v ≠ %s\n", "nil pouinter", ptr, "nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintIsZero(t *testing.T) {
|
||||
i := UintFrom(12345)
|
||||
if i.IsZero() {
|
||||
t.Errorf("IsZero() should be false")
|
||||
}
|
||||
|
||||
null := NewUint(0, false)
|
||||
if !null.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
|
||||
zero := NewUint(0, true)
|
||||
if !zero.IsZero() {
|
||||
t.Errorf("IsZero() should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintScan(t *testing.T) {
|
||||
var i Uint
|
||||
err := i.Scan(12345)
|
||||
maybePanic(err)
|
||||
assertUint(t, i, "scanned uint")
|
||||
|
||||
var null Uint
|
||||
err = null.Scan(nil)
|
||||
maybePanic(err)
|
||||
assertNullUint(t, null, "scanned null")
|
||||
}
|
||||
|
||||
func TestUintSetValid(t *testing.T) {
|
||||
change := NewUint(0, false)
|
||||
assertNullUint(t, change, "SetValid()")
|
||||
change.SetValid(12345)
|
||||
assertUint(t, change, "SetValid()")
|
||||
}
|
||||
|
||||
func assertUint(t *testing.T, i Uint, from string) {
|
||||
if i.Uint != 12345 {
|
||||
t.Errorf("bad %s uint: %d ≠ %d\n", from, i.Uint, 12345)
|
||||
}
|
||||
if !i.Valid {
|
||||
t.Error(from, "is invalid, but should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullUint(t *testing.T, i Uint, from string) {
|
||||
if i.Valid {
|
||||
t.Error(from, "is valid, but should be invalid")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue