From 281274eecfc6d7a3d7bf74e56d981dc58195d166 Mon Sep 17 00:00:00 2001 From: j2gg0s Date: Wed, 18 Dec 2024 10:55:21 +0800 Subject: [PATCH] fix: support scan float32 to float32 Close #1087 --- schema/scan.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/schema/scan.go b/schema/scan.go index 1b7f05b7..86a30b2f 100644 --- a/schema/scan.go +++ b/schema/scan.go @@ -38,7 +38,7 @@ func init() { reflect.Uint32: scanUint64, reflect.Uint64: scanUint64, reflect.Uintptr: scanUint64, - reflect.Float32: scanFloat64, + reflect.Float32: scanFloat32, reflect.Float64: scanFloat64, reflect.Complex64: nil, reflect.Complex128: nil, @@ -214,6 +214,36 @@ func scanUint64(dest reflect.Value, src interface{}) error { } } +func scanFloat32(dest reflect.Value, src interface{}) error { + switch src := src.(type) { + case nil: + dest.SetFloat(0) + return nil + case float32: + dest.SetFloat(float64(src)) + return nil + case float64: + dest.SetFloat(src) + return nil + case []byte: + f, err := strconv.ParseFloat(internal.String(src), 64) + if err != nil { + return err + } + 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) + } +} + func scanFloat64(dest reflect.Value, src interface{}) error { switch src := src.(type) { case nil: