Skip to content

Commit 6d8d88a

Browse files
ianlancetaylorgopherbot
authored andcommitted
database/sql: use reflect.TypeFor for known types
For #60088 Change-Id: Ib05ba3d456b22f7370459037b3d263c4b3ebe3b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/514975 Reviewed-by: Daniel Theophanes <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 0c13bd6 commit 6d8d88a

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/database/sql/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
529529
return
530530
}
531531

532-
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
532+
var valuerReflectType = reflect.TypeFor[driver.Valuer]()
533533

534534
// callValuerValue returns vr.Value(), with one exception:
535535
// If vr.Value is an auto-generated method on a pointer type and the

src/database/sql/driver/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ type defaultConverter struct{}
211211

212212
var _ ValueConverter = defaultConverter{}
213213

214-
var valuerReflectType = reflect.TypeOf((*Valuer)(nil)).Elem()
214+
var valuerReflectType = reflect.TypeFor[Valuer]()
215215

216216
// callValuerValue returns vr.Value(), with one exception:
217217
// If vr.Value is an auto-generated method on a pointer type and the

src/database/sql/fakedb_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,33 +1251,33 @@ func converterForType(typ string) driver.ValueConverter {
12511251
func colTypeToReflectType(typ string) reflect.Type {
12521252
switch typ {
12531253
case "bool":
1254-
return reflect.TypeOf(false)
1254+
return reflect.TypeFor[bool]()
12551255
case "nullbool":
1256-
return reflect.TypeOf(NullBool{})
1256+
return reflect.TypeFor[NullBool]()
12571257
case "int16":
1258-
return reflect.TypeOf(int16(0))
1258+
return reflect.TypeFor[int16]()
12591259
case "nullint16":
1260-
return reflect.TypeOf(NullInt16{})
1260+
return reflect.TypeFor[NullInt16]()
12611261
case "int32":
1262-
return reflect.TypeOf(int32(0))
1262+
return reflect.TypeFor[int32]()
12631263
case "nullint32":
1264-
return reflect.TypeOf(NullInt32{})
1264+
return reflect.TypeFor[NullInt32]()
12651265
case "string":
1266-
return reflect.TypeOf("")
1266+
return reflect.TypeFor[string]()
12671267
case "nullstring":
1268-
return reflect.TypeOf(NullString{})
1268+
return reflect.TypeFor[NullString]()
12691269
case "int64":
1270-
return reflect.TypeOf(int64(0))
1270+
return reflect.TypeFor[int64]()
12711271
case "nullint64":
1272-
return reflect.TypeOf(NullInt64{})
1272+
return reflect.TypeFor[NullInt64]()
12731273
case "float64":
1274-
return reflect.TypeOf(float64(0))
1274+
return reflect.TypeFor[float64]()
12751275
case "nullfloat64":
1276-
return reflect.TypeOf(NullFloat64{})
1276+
return reflect.TypeFor[NullFloat64]()
12771277
case "datetime":
1278-
return reflect.TypeOf(time.Time{})
1278+
return reflect.TypeFor[time.Time]()
12791279
case "any":
1280-
return reflect.TypeOf(new(any)).Elem()
1280+
return reflect.TypeFor[any]()
12811281
}
12821282
panic("invalid fakedb column type of " + typ)
12831283
}

src/database/sql/sql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3244,7 +3244,7 @@ func rowsColumnInfoSetupConnLocked(rowsi driver.Rows) []*ColumnType {
32443244
if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok {
32453245
ci.scanType = prop.ColumnTypeScanType(i)
32463246
} else {
3247-
ci.scanType = reflect.TypeOf(new(any)).Elem()
3247+
ci.scanType = reflect.TypeFor[any]()
32483248
}
32493249
if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok {
32503250
ci.databaseType = prop.ColumnTypeDatabaseTypeName(i)

0 commit comments

Comments
 (0)