mirror of
https://github.com/golang/go
synced 2024-11-12 06:20:22 -07:00
exp/sql: simplify some string conversions.
R=bradfitz CC=golang-dev https://golang.org/cl/5451112
This commit is contained in:
parent
5e98505ba7
commit
9d52fe22b4
@ -95,35 +95,26 @@ func convertAssign(dest, src interface{}) error {
|
||||
switch dv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
s := asString(src)
|
||||
i64, err := strconv.ParseInt(s, 10, 64)
|
||||
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
|
||||
}
|
||||
if dv.OverflowInt(i64) {
|
||||
return fmt.Errorf("string %q overflows %s", s, dv.Kind())
|
||||
}
|
||||
dv.SetInt(i64)
|
||||
return nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
s := asString(src)
|
||||
u64, err := strconv.ParseUint(s, 10, 64)
|
||||
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
|
||||
}
|
||||
if dv.OverflowUint(u64) {
|
||||
return fmt.Errorf("string %q overflows %s", s, dv.Kind())
|
||||
}
|
||||
dv.SetUint(u64)
|
||||
return nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
s := asString(src)
|
||||
f64, err := strconv.ParseFloat(s, 64)
|
||||
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
|
||||
}
|
||||
if dv.OverflowFloat(f64) {
|
||||
return fmt.Errorf("value %q overflows %s", s, dv.Kind())
|
||||
}
|
||||
dv.SetFloat(f64)
|
||||
return nil
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ var conversionTests = []conversionTest{
|
||||
|
||||
// Strings to integers
|
||||
{s: "255", d: &scanuint8, wantuint: 255},
|
||||
{s: "256", d: &scanuint8, wanterr: `string "256" overflows uint8`},
|
||||
{s: "256", d: &scanuint8, wanterr: `converting string "256" to a uint8: parsing "256": value out of range`},
|
||||
{s: "256", d: &scanuint16, wantuint: 256},
|
||||
{s: "-1", d: &scanint, wantint: -1},
|
||||
{s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: parsing "foo": invalid syntax`},
|
||||
|
Loading…
Reference in New Issue
Block a user