Skip to content

Commit 6ee82df

Browse files
authored
Merge pull request #12 from maku693/fix-uint64-scan
Fix Uint64.Scan() to convert negative int64 to uint64
2 parents c1106c4 + 057ceb6 commit 6ee82df

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
github.com/friendsofgo/errors v0.9.2 h1:X6NYxef4efCBdwI7BgS820zFaN7Cphrmb+Pljdzjtgk=
12
github.com/friendsofgo/errors v0.9.2/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI=
3+
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
24
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
35
github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwvE8KsU=
46
github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=

uint64.go

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ func (u *Uint64) Scan(value interface{}) error {
109109
return nil
110110
}
111111
u.Valid = true
112+
113+
// If value is negative int64, convert it to uint64
114+
if i, ok := value.(int64); ok && i < 0 {
115+
return convert.ConvertAssign(&u.Uint64, uint64(i))
116+
}
117+
112118
return convert.ConvertAssign(&u.Uint64, value)
113119
}
114120

@@ -118,6 +124,7 @@ func (u Uint64) Value() (driver.Value, error) {
118124
return nil, nil
119125
}
120126

127+
// If u.Uint64 overflows the range of int64, convert it to string
121128
if u.Uint64 >= 1<<63 {
122129
return strconv.FormatUint(u.Uint64, 10), nil
123130
}

uint64_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ func TestUint64Scan(t *testing.T) {
145145
maybePanic(err)
146146
assertUint64(t, i, "scanned uint64")
147147

148-
var null Uint64
149-
err = null.Scan(nil)
148+
err = i.Scan(int64(-2))
149+
maybePanic(err)
150+
assertUint64(t, i, "scanned int64")
151+
152+
err = i.Scan(nil)
150153
maybePanic(err)
151-
assertNullUint64(t, null, "scanned null")
154+
assertNullUint64(t, i, "scanned null")
152155
}
153156

154157
func assertUint64(t *testing.T, i Uint64, from string) {

0 commit comments

Comments
 (0)