From f920f8d434cc226464abe24e8389e6466b561ecf Mon Sep 17 00:00:00 2001 From: IKEDA Sho Date: Thu, 13 Aug 2020 13:40:09 +0900 Subject: [PATCH] Support returning uint64 from Valuer in ConvertValue (#1143) https://golang.org/pkg/database/sql/driver/#Value says: > Value is a value that drivers must be able to handle. It is either nil, a type handled by a database driver's NamedValueChecker interface, or an instance of one of these types: --- AUTHORS | 1 + statement.go | 12 +++++++++--- statement_test.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 8ba3db6b3..ccf2f466c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -79,6 +79,7 @@ Reed Allman Richard Wilkes Robert Russell Runrioter Wung +Sho Ikeda Shuode Li Simon J Mudd Soroush Pour diff --git a/statement.go b/statement.go index d3e68112f..18a3ae498 100644 --- a/statement.go +++ b/statement.go @@ -154,10 +154,16 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) { if err != nil { return nil, err } - if !driver.IsValue(sv) { - return nil, fmt.Errorf("non-Value type %T returned from Value", sv) + if driver.IsValue(sv) { + return sv, nil } - return sv, nil + // A value returend from the Valuer interface can be "a type handled by + // a database driver's NamedValueChecker interface" so we should accept + // uint64 here as well. + if u, ok := sv.(uint64); ok { + return u, nil + } + return nil, fmt.Errorf("non-Value type %T returned from Value", sv) } rv := reflect.ValueOf(v) switch rv.Kind() { diff --git a/statement_test.go b/statement_test.go index 2cc022bf5..ac6b92de9 100644 --- a/statement_test.go +++ b/statement_test.go @@ -10,6 +10,7 @@ package mysql import ( "bytes" + "database/sql/driver" "encoding/json" "testing" ) @@ -96,6 +97,14 @@ func TestConvertSignedIntegers(t *testing.T) { } } +type myUint64 struct { + value uint64 +} + +func (u myUint64) Value() (driver.Value, error) { + return u.value, nil +} + func TestConvertUnsignedIntegers(t *testing.T) { values := []interface{}{ uint8(42), @@ -103,6 +112,7 @@ func TestConvertUnsignedIntegers(t *testing.T) { uint32(42), uint64(42), uint(42), + myUint64{uint64(42)}, } for _, value := range values {