Skip to content

Commit ef09592

Browse files
committed
Correct lint errors
- Add a few missing/broken tests exposed by linting
1 parent 628b779 commit ef09592

22 files changed

Lines changed: 156 additions & 46 deletions

bool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
7070
// UnmarshalText implements encoding.TextUnmarshaler.
7171
func (b *Bool) UnmarshalText(text []byte) error {
7272
b.Set = true
73-
if text == nil || len(text) == 0 {
73+
if len(text) == 0 {
7474
b.Valid = false
7575
return nil
7676
}

byte.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (b *Byte) UnmarshalJSON(data []byte) error {
7474
// UnmarshalText implements encoding.TextUnmarshaler.
7575
func (b *Byte) UnmarshalText(text []byte) error {
7676
b.Set = true
77-
if text == nil || len(text) == 0 {
77+
if len(text) == 0 {
7878
b.Valid = false
7979
return nil
8080
}

bytes_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestUnmarshalBytes(t *testing.T) {
5151

5252
var null Bytes
5353
err = null.UnmarshalJSON(NullBytes)
54+
if err != nil {
55+
t.Error(err)
56+
}
5457
if null.Valid {
5558
t.Errorf("expected Valid to be false, got true")
5659
}

convert/convert.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ func ConvertAssign(dest, src interface{}) error {
170170
if src == nil {
171171
dv.Set(reflect.Zero(dv.Type()))
172172
return nil
173-
} else {
174-
dv.Set(reflect.New(dv.Type().Elem()))
175-
return ConvertAssign(dv.Interface(), src)
176173
}
174+
dv.Set(reflect.New(dv.Type().Elem()))
175+
return ConvertAssign(dv.Interface(), src)
177176
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
178177
s := asString(src)
179178
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
@@ -216,11 +215,10 @@ func strconvErr(err error) error {
216215
func cloneBytes(b []byte) []byte {
217216
if b == nil {
218217
return nil
219-
} else {
220-
c := make([]byte, len(b))
221-
copy(c, b)
222-
return c
223218
}
219+
c := make([]byte, len(b))
220+
copy(c, b)
221+
return c
224222
}
225223

226224
func asString(src interface{}) string {

convert/convert_test.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,23 @@ var conversionTests = []conversionTest{
114114
{s: 1.5, d: &scanraw, wantraw: sql.RawBytes("1.5")},
115115

116116
// Strings to integers
117+
{s: "127", d: &scanint8, wantint: 127},
118+
{s: "128", d: &scanint8, wanterr: `converting driver.Value type string ("128") to a int8: value out of range`},
119+
{s: "32767", d: &scanint16, wantint: 32767},
120+
{s: "32768", d: &scanint16, wanterr: `converting driver.Value type string ("32768") to a int16: value out of range`},
121+
{s: "2147483647", d: &scanint32, wantint: 2147483647},
122+
{s: "2147483648", d: &scanint32, wanterr: `converting driver.Value type string ("2147483648") to a int32: value out of range`},
117123
{s: "255", d: &scanuint8, wantuint: 255},
118-
{s: "256", d: &scanuint8, wanterr: "converting driver.Value type string (\"256\") to a uint8: value out of range"},
124+
{s: "256", d: &scanuint8, wanterr: `converting driver.Value type string ("256") to a uint8: value out of range`},
119125
{s: "256", d: &scanuint16, wantuint: 256},
120126
{s: "-1", d: &scanint, wantint: -1},
121-
{s: "foo", d: &scanint, wanterr: "converting driver.Value type string (\"foo\") to a int: invalid syntax"},
127+
{s: "foo", d: &scanint, wanterr: `converting driver.Value type string ("foo") to a int: invalid syntax`},
122128

123129
// int64 to smaller integers
124130
{s: int64(5), d: &scanuint8, wantuint: 5},
125-
{s: int64(256), d: &scanuint8, wanterr: "converting driver.Value type int64 (\"256\") to a uint8: value out of range"},
131+
{s: int64(256), d: &scanuint8, wanterr: `converting driver.Value type int64 ("256") to a uint8: value out of range`},
126132
{s: int64(256), d: &scanuint16, wantuint: 256},
127-
{s: int64(65536), d: &scanuint16, wanterr: "converting driver.Value type int64 (\"65536\") to a uint16: value out of range"},
133+
{s: int64(65536), d: &scanuint16, wanterr: `converting driver.Value type int64 ("65536") to a uint16: value out of range`},
128134

129135
// True bools
130136
{s: true, d: &scanbool, wantbool: true},
@@ -247,6 +253,27 @@ func TestConversions(t *testing.T) {
247253
errf("want pointer to %v, got %v", *ct.wantptr, intPtrValue(ct.d))
248254
}
249255
}
256+
if len(ct.wantraw) != 0 {
257+
s := fmt.Sprintf("%v", ct.s)
258+
if _, ok := ct.s.([]byte); ok {
259+
s = fmt.Sprintf("%s", ct.s)
260+
}
261+
if s != string(ct.wantraw) {
262+
errf("want %q, got: %s", string(ct.wantraw), s)
263+
}
264+
}
265+
if len(ct.wantbytes) != 0 {
266+
s := fmt.Sprintf("%v", ct.s)
267+
if _, ok := ct.s.([]byte); ok {
268+
s = fmt.Sprintf("%s", ct.s)
269+
}
270+
if timeVal, ok := ct.s.(time.Time); ok {
271+
s = timeVal.Format(time.RFC3339Nano)
272+
}
273+
if s != string(ct.wantbytes) {
274+
errf("want %q, got: %s", string(ct.wantbytes), s)
275+
}
276+
}
250277
if ifptr, ok := ct.d.(*interface{}); ok {
251278
if !reflect.DeepEqual(ct.wantiface, scaniface) {
252279
errf("want interface %#v, got %#v", ct.wantiface, scaniface)
@@ -267,14 +294,18 @@ func TestConversions(t *testing.T) {
267294

268295
func TestNullString(t *testing.T) {
269296
var ns sql.NullString
270-
ConvertAssign(&ns, []byte("foo"))
297+
if err := ConvertAssign(&ns, []byte("foo")); err != nil {
298+
t.Error(err)
299+
}
271300
if !ns.Valid {
272301
t.Errorf("expecting not null")
273302
}
274303
if ns.String != "foo" {
275304
t.Errorf("expecting foo; got %q", ns.String)
276305
}
277-
ConvertAssign(&ns, nil)
306+
if err := ConvertAssign(&ns, nil); err != nil {
307+
t.Error(err)
308+
}
278309
if ns.Valid {
279310
t.Errorf("expecting null on nil")
280311
}
@@ -290,8 +321,8 @@ type valueConverterTest struct {
290321
}
291322

292323
var valueConverterTests = []valueConverterTest{
293-
{driver.DefaultParameterConverter, sql.NullString{"hi", true}, "hi", ""},
294-
{driver.DefaultParameterConverter, sql.NullString{"", false}, nil, ""},
324+
{driver.DefaultParameterConverter, sql.NullString{String: "hi", Valid: true}, "hi", ""},
325+
{driver.DefaultParameterConverter, sql.NullString{String: "", Valid: false}, nil, ""},
295326
}
296327

297328
func TestValueConverters(t *testing.T) {

float32.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (f *Float32) UnmarshalJSON(data []byte) error {
7171
// UnmarshalText implements encoding.TextUnmarshaler.
7272
func (f *Float32) UnmarshalText(text []byte) error {
7373
f.Set = true
74-
if text == nil || len(text) == 0 {
74+
if len(text) == 0 {
7575
f.Valid = false
7676
return nil
7777
}

float64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (f *Float64) UnmarshalJSON(data []byte) error {
6969
// UnmarshalText implements encoding.TextUnmarshaler.
7070
func (f *Float64) UnmarshalText(text []byte) error {
7171
f.Set = true
72-
if text == nil || len(text) == 0 {
72+
if len(text) == 0 {
7373
f.Valid = false
7474
return nil
7575
}

int.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (i *Int) UnmarshalJSON(data []byte) error {
7373
// UnmarshalText implements encoding.TextUnmarshaler.
7474
func (i *Int) UnmarshalText(text []byte) error {
7575
i.Set = true
76-
if text == nil || len(text) == 0 {
76+
if len(text) == 0 {
7777
i.Valid = false
7878
return nil
7979
}

int16.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (i *Int16) UnmarshalJSON(data []byte) error {
7575
// UnmarshalText implements encoding.TextUnmarshaler.
7676
func (i *Int16) UnmarshalText(text []byte) error {
7777
i.Set = true
78-
if text == nil || len(text) == 0 {
78+
if len(text) == 0 {
7979
i.Valid = false
8080
return nil
8181
}

int32.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (i *Int32) UnmarshalJSON(data []byte) error {
7878
// UnmarshalText implements encoding.TextUnmarshaler.
7979
func (i *Int32) UnmarshalText(text []byte) error {
8080
i.Set = true
81-
if text == nil || len(text) == 0 {
81+
if len(text) == 0 {
8282
i.Valid = false
8383
return nil
8484
}

0 commit comments

Comments
 (0)