Skip to content

Commit 570c44d

Browse files
committed
fix(bool): fixed driver.Value issue on bool
1 parent 13b5269 commit 570c44d

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

bool.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ type Bool bool
1111
// Value implements the driver.Valuer interface,
1212
// and turns the BitBool into a bit field (BIT(1)) for MySQL storage.
1313
func (b Bool) Value() (driver.Value, error) { // skipcq: GO-W1029
14-
if b {
15-
return []byte{1}, nil
16-
} else {
17-
return []byte{0}, nil
18-
}
14+
return bool(b), nil
1915
}
2016

2117
// Scan implements the sql.Scanner interface,
@@ -30,6 +26,14 @@ func (b *Bool) Scan(src interface{}) error { // skipcq: GO-W1029
3026
*b = v[0] == 1
3127
case int64:
3228
*b = v == 1
29+
case bool:
30+
*b = Bool(v)
31+
case string:
32+
if v == "1" || v == "t" || v == "T" || v == "true" || v == "TRUE" || v == "True" {
33+
*b = true
34+
} else {
35+
*b = false
36+
}
3337
default:
3438
return errors.New("bad []byte type assertion")
3539
}

bool_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func TestBool(t *testing.T) {
4848

4949
require.EqualValues(t, true, b1)
5050

51+
err = d.QueryRow("SELECT `status` FROM `users` WHERE id=? AND status=1", 10).Scan(&b1)
52+
require.NoError(t, err)
53+
54+
require.EqualValues(t, true, b1)
55+
5156
var b2 Bool
5257
err = d.QueryRow("SELECT `status` FROM `users` WHERE id=?", 11).Scan(&b2)
5358
require.NoError(t, err)

0 commit comments

Comments
 (0)