Commit 9d52fe22 authored by David Symonds's avatar David Symonds

exp/sql: simplify some string conversions.

R=bradfitz
CC=golang-dev
https://golang.org/cl/5451112
parent 5e98505b
...@@ -95,35 +95,26 @@ func convertAssign(dest, src interface{}) error { ...@@ -95,35 +95,26 @@ func convertAssign(dest, src interface{}) error {
switch dv.Kind() { switch dv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := asString(src) s := asString(src)
i64, err := strconv.ParseInt(s, 10, 64) i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil { if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) 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) dv.SetInt(i64)
return nil return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s := asString(src) s := asString(src)
u64, err := strconv.ParseUint(s, 10, 64) u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil { if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) 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) dv.SetUint(u64)
return nil return nil
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
s := asString(src) s := asString(src)
f64, err := strconv.ParseFloat(s, 64) f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil { if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err) 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) dv.SetFloat(f64)
return nil return nil
} }
......
...@@ -55,7 +55,7 @@ var conversionTests = []conversionTest{ ...@@ -55,7 +55,7 @@ var conversionTests = []conversionTest{
// Strings to integers // Strings to integers
{s: "255", d: &scanuint8, wantuint: 255}, {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: "256", d: &scanuint16, wantuint: 256},
{s: "-1", d: &scanint, wantint: -1}, {s: "-1", d: &scanint, wantint: -1},
{s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: parsing "foo": invalid syntax`}, {s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: parsing "foo": invalid syntax`},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment