summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/schema/scan.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/schema/scan.go')
-rw-r--r--vendor/github.com/uptrace/bun/schema/scan.go69
1 files changed, 57 insertions, 12 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/scan.go b/vendor/github.com/uptrace/bun/schema/scan.go
index 30abcfc35..069b14e44 100644
--- a/vendor/github.com/uptrace/bun/schema/scan.go
+++ b/vendor/github.com/uptrace/bun/schema/scan.go
@@ -94,6 +94,8 @@ func scanner(typ reflect.Type) ScannerFunc {
}
switch typ {
+ case bytesType:
+ return scanBytes
case timeType:
return scanTime
case ipType:
@@ -134,12 +136,22 @@ func scanBool(dest reflect.Value, src interface{}) error {
dest.SetBool(src != 0)
return nil
case []byte:
- if len(src) == 1 {
- dest.SetBool(src[0] != '0')
- return nil
+ f, err := strconv.ParseBool(internal.String(src))
+ if err != nil {
+ return err
+ }
+ dest.SetBool(f)
+ return nil
+ case string:
+ f, err := strconv.ParseBool(src)
+ if err != nil {
+ return err
}
+ dest.SetBool(f)
+ return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanInt64(dest reflect.Value, src interface{}) error {
@@ -167,8 +179,9 @@ func scanInt64(dest reflect.Value, src interface{}) error {
}
dest.SetInt(n)
return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanUint64(dest reflect.Value, src interface{}) error {
@@ -189,8 +202,16 @@ func scanUint64(dest reflect.Value, src interface{}) error {
}
dest.SetUint(n)
return nil
+ case string:
+ n, err := strconv.ParseUint(src, 10, 64)
+ if err != nil {
+ return err
+ }
+ dest.SetUint(n)
+ return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanFloat64(dest reflect.Value, src interface{}) error {
@@ -208,8 +229,16 @@ func scanFloat64(dest reflect.Value, src interface{}) error {
}
dest.SetFloat(f)
return nil
+ case string:
+ f, err := strconv.ParseFloat(src, 64)
+ if err != nil {
+ return err
+ }
+ dest.SetFloat(f)
+ return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanString(dest reflect.Value, src interface{}) error {
@@ -226,8 +255,18 @@ func scanString(dest reflect.Value, src interface{}) error {
case time.Time:
dest.SetString(src.Format(time.RFC3339Nano))
return nil
+ case int64:
+ dest.SetString(strconv.FormatInt(src, 10))
+ return nil
+ case uint64:
+ dest.SetString(strconv.FormatUint(src, 10))
+ return nil
+ case float64:
+ dest.SetString(strconv.FormatFloat(src, 'G', -1, 64))
+ return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanBytes(dest reflect.Value, src interface{}) error {
@@ -244,8 +283,9 @@ func scanBytes(dest reflect.Value, src interface{}) error {
dest.SetBytes(clone)
return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanTime(dest reflect.Value, src interface{}) error {
@@ -274,8 +314,9 @@ func scanTime(dest reflect.Value, src interface{}) error {
destTime := dest.Addr().Interface().(*time.Time)
*destTime = srcTime
return nil
+ default:
+ return scanError(dest.Type(), src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
}
func scanScanner(dest reflect.Value, src interface{}) error {
@@ -438,7 +479,7 @@ func scanJSONIntoInterface(dest reflect.Value, src interface{}) error {
if fn := Scanner(dest.Type()); fn != nil {
return fn(dest, src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
+ return scanError(dest.Type(), src)
}
func scanInterface(dest reflect.Value, src interface{}) error {
@@ -454,7 +495,7 @@ func scanInterface(dest reflect.Value, src interface{}) error {
if fn := Scanner(dest.Type()); fn != nil {
return fn(dest, src)
}
- return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
+ return scanError(dest.Type(), src)
}
func nilable(kind reflect.Kind) bool {
@@ -464,3 +505,7 @@ func nilable(kind reflect.Kind) bool {
}
return false
}
+
+func scanError(dest reflect.Type, src interface{}) error {
+ return fmt.Errorf("bun: can't scan %#v (%T) into %s", src, src, dest.String())
+}