]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: additional underlying types in DefaultValueConverter
authorScott Bell <scott@sctsm.com>
Wed, 18 May 2016 16:56:51 +0000 (09:56 -0700)
committerIan Lance Taylor <iant@golang.org>
Thu, 17 Nov 2016 00:09:27 +0000 (00:09 +0000)
The previous documentation purported to convert underlying strings to
[]byte, which it did not do. This adds support for underlying bool,
string, and []byte, which convert directly to their underlying type.

Fixes #15174.

Change-Id: I7fc4e2520577f097a48f39c9ff6c8160fdfb7be4
Reviewed-on: https://go-review.googlesource.com/27812
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>

src/database/sql/driver/types.go
src/database/sql/driver/types_test.go

index c93c97a39255b568a246924c94a8ad2f11c6e6f6..8b3cb6c8f6115a4378fafef45ef4f759626f1f73 100644 (file)
@@ -198,9 +198,9 @@ func IsScanValue(v interface{}) bool {
 // Value method is used to return a Value. As a fallback, the provided
 // argument's underlying type is used to convert it to a Value:
 // underlying integer types are converted to int64, floats to float64,
-// and strings to []byte. If the argument is a nil pointer,
-// ConvertValue returns a nil Value. If the argument is a non-nil
-// pointer, it is dereferenced and ConvertValue is called
+// bool, string, and []byte to themselves. If the argument is a nil
+// pointer, ConvertValue returns a nil Value. If the argument is a
+// non-nil pointer, it is dereferenced and ConvertValue is called
 // recursively. Other types are an error.
 var DefaultParameterConverter defaultConverter
 
@@ -267,6 +267,16 @@ func (defaultConverter) ConvertValue(v interface{}) (Value, error) {
                return int64(u64), nil
        case reflect.Float32, reflect.Float64:
                return rv.Float(), nil
+       case reflect.Bool:
+               return rv.Bool(), nil
+       case reflect.Slice:
+               ek := rv.Type().Elem().Kind()
+               if ek == reflect.Uint8 {
+                       return rv.Bytes(), nil
+               }
+               return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
+       case reflect.String:
+               return rv.String(), nil
        }
        return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
 }
index 1ce0ff06541844a5d4155f6d9d779eb252674fe7..0379bf8892f54d304ba1c2e83fb885682351b8de 100644 (file)
@@ -20,6 +20,16 @@ type valueConverterTest struct {
 var now = time.Now()
 var answer int64 = 42
 
+type (
+       i  int64
+       f  float64
+       b  bool
+       bs []byte
+       s  string
+       t  time.Time
+       is []int
+)
+
 var valueConverterTests = []valueConverterTest{
        {Bool, "true", true, ""},
        {Bool, "True", true, ""},
@@ -41,6 +51,12 @@ var valueConverterTests = []valueConverterTest{
        {DefaultParameterConverter, (*int64)(nil), nil, ""},
        {DefaultParameterConverter, &answer, answer, ""},
        {DefaultParameterConverter, &now, now, ""},
+       {DefaultParameterConverter, i(9), int64(9), ""},
+       {DefaultParameterConverter, f(0.1), float64(0.1), ""},
+       {DefaultParameterConverter, b(true), true, ""},
+       {DefaultParameterConverter, bs{1}, []byte{1}, ""},
+       {DefaultParameterConverter, s("a"), "a", ""},
+       {DefaultParameterConverter, is{1}, nil, "unsupported type driver.is, a slice of int"},
 }
 
 func TestValueConverters(t *testing.T) {