Skip to content

Commit d7c0de9

Browse files
Scott Bellianlancetaylor
Scott Bell
authored andcommitted
database/sql: additional underlying types in DefaultValueConverter
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent 0df762e commit d7c0de9

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/database/sql/driver/types.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ func IsScanValue(v interface{}) bool {
198198
// Value method is used to return a Value. As a fallback, the provided
199199
// argument's underlying type is used to convert it to a Value:
200200
// underlying integer types are converted to int64, floats to float64,
201-
// and strings to []byte. If the argument is a nil pointer,
202-
// ConvertValue returns a nil Value. If the argument is a non-nil
203-
// pointer, it is dereferenced and ConvertValue is called
201+
// bool, string, and []byte to themselves. If the argument is a nil
202+
// pointer, ConvertValue returns a nil Value. If the argument is a
203+
// non-nil pointer, it is dereferenced and ConvertValue is called
204204
// recursively. Other types are an error.
205205
var DefaultParameterConverter defaultConverter
206206

@@ -267,6 +267,16 @@ func (defaultConverter) ConvertValue(v interface{}) (Value, error) {
267267
return int64(u64), nil
268268
case reflect.Float32, reflect.Float64:
269269
return rv.Float(), nil
270+
case reflect.Bool:
271+
return rv.Bool(), nil
272+
case reflect.Slice:
273+
ek := rv.Type().Elem().Kind()
274+
if ek == reflect.Uint8 {
275+
return rv.Bytes(), nil
276+
}
277+
return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
278+
case reflect.String:
279+
return rv.String(), nil
270280
}
271281
return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
272282
}

src/database/sql/driver/types_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ type valueConverterTest struct {
2020
var now = time.Now()
2121
var answer int64 = 42
2222

23+
type (
24+
i int64
25+
f float64
26+
b bool
27+
bs []byte
28+
s string
29+
t time.Time
30+
is []int
31+
)
32+
2333
var valueConverterTests = []valueConverterTest{
2434
{Bool, "true", true, ""},
2535
{Bool, "True", true, ""},
@@ -41,6 +51,12 @@ var valueConverterTests = []valueConverterTest{
4151
{DefaultParameterConverter, (*int64)(nil), nil, ""},
4252
{DefaultParameterConverter, &answer, answer, ""},
4353
{DefaultParameterConverter, &now, now, ""},
54+
{DefaultParameterConverter, i(9), int64(9), ""},
55+
{DefaultParameterConverter, f(0.1), float64(0.1), ""},
56+
{DefaultParameterConverter, b(true), true, ""},
57+
{DefaultParameterConverter, bs{1}, []byte{1}, ""},
58+
{DefaultParameterConverter, s("a"), "a", ""},
59+
{DefaultParameterConverter, is{1}, nil, "unsupported type driver.is, a slice of int"},
4460
}
4561

4662
func TestValueConverters(t *testing.T) {

0 commit comments

Comments
 (0)