From 8a4b9de8540bf0b0ed7eb5546fcf6ae06e347821 Mon Sep 17 00:00:00 2001 From: maku693 Date: Sun, 14 Feb 2021 00:49:57 +0900 Subject: [PATCH 1/3] Fix Uint64.Scan() to convert received value to uint64 when it's negative --- uint64.go | 6 ++++++ uint64_test.go | 9 ++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/uint64.go b/uint64.go index 9326148..c3ac50b 100644 --- a/uint64.go +++ b/uint64.go @@ -109,6 +109,12 @@ func (u *Uint64) Scan(value interface{}) error { return nil } u.Valid = true + + // If value is negative int64, convert it to uint64 + if i, ok := value.(int64); ok && i < 0 { + return convert.ConvertAssign(&u.Uint64, uint64(i)) + } + return convert.ConvertAssign(&u.Uint64, value) } diff --git a/uint64_test.go b/uint64_test.go index c0abc14..0cee3fb 100644 --- a/uint64_test.go +++ b/uint64_test.go @@ -145,10 +145,13 @@ func TestUint64Scan(t *testing.T) { maybePanic(err) assertUint64(t, i, "scanned uint64") - var null Uint64 - err = null.Scan(nil) + err = i.Scan(int64(-2)) + maybePanic(err) + assertUint64(t, i, "scanned int64") + + err = i.Scan(nil) maybePanic(err) - assertNullUint64(t, null, "scanned null") + assertNullUint64(t, i, "scanned null") } func assertUint64(t *testing.T, i Uint64, from string) { From 6b3346c28dc90239117dc16214c43a8dd40a4345 Mon Sep 17 00:00:00 2001 From: maku693 Date: Sun, 14 Feb 2021 00:55:13 +0900 Subject: [PATCH 2/3] Add a comment to the special handling of large uint64 in Uint64.Scan() --- uint64.go | 1 + 1 file changed, 1 insertion(+) diff --git a/uint64.go b/uint64.go index c3ac50b..a9ff8cf 100644 --- a/uint64.go +++ b/uint64.go @@ -124,6 +124,7 @@ func (u Uint64) Value() (driver.Value, error) { return nil, nil } + // If u.Uint64 overflows the range of int64, convert it to string if u.Uint64 >= 1<<63 { return strconv.FormatUint(u.Uint64, 10), nil } From 057ceb611ec130d0c38288861dcb0cd0bf75d88c Mon Sep 17 00:00:00 2001 From: maku693 Date: Sun, 14 Feb 2021 00:58:39 +0900 Subject: [PATCH 3/3] Update go.sum --- go.sum | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.sum b/go.sum index 42f5594..2456d75 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ +github.com/friendsofgo/errors v0.9.2 h1:X6NYxef4efCBdwI7BgS820zFaN7Cphrmb+Pljdzjtgk= github.com/friendsofgo/errors v0.9.2/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI= +github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwvE8KsU= github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=