From 717ae359bc2dfd65e7629b670f7280b32b1b035b Mon Sep 17 00:00:00 2001 From: what Date: Sat, 22 Mar 2025 23:04:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=20string=20=E5=92=8C=20=20in?= =?UTF-8?q?t,=20converting=20NULL=20to=20string=20is=20unsupported?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exec/scanner.go | 10 ++++++++-- internal/util/reflect.go | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/exec/scanner.go b/exec/scanner.go index d70fbb3..8955aa3 100644 --- a/exec/scanner.go +++ b/exec/scanner.go @@ -70,9 +70,15 @@ func (s *scanner) ScanStruct(i interface{}) error { scans := make([]interface{}, 0, len(s.columns)) for _, col := range s.columns { data, ok := s.columnMap[col] - switch { - case !ok: + + if !ok { return unableToFindFieldError(col) + } + // 处理 converting NULL to string is unsupported + // 前面将 string 和 int 类型转为了 *string 和 *int + switch data.GoType.Kind() { + case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + scans = append(scans, reflect.New(reflect.PointerTo(data.GoType)).Interface()) default: scans = append(scans, reflect.New(data.GoType).Interface()) } diff --git a/internal/util/reflect.go b/internal/util/reflect.go index 6fd97d2..66e8c70 100644 --- a/internal/util/reflect.go +++ b/internal/util/reflect.go @@ -167,8 +167,15 @@ func SafeSetFieldByIndex(v reflect.Value, fieldIndex []int, src interface{}) (re return v case 1: f := v.FieldByIndex(fieldIndex) - srcVal := reflect.ValueOf(src) - f.Set(reflect.Indirect(srcVal)) + srcVal := reflect.ValueOf(src).Elem() + // 处理 converting NULL to string is unsupported + // 前面将 string 和 int 类型转为了 *string 和 *int + // 这里做还原 + if f.Type().ConvertibleTo(srcVal.Type().Elem()) { + f.Set(srcVal.Elem()) + } else { + f.Set(srcVal) + } default: f := v.Field(fieldIndex[0]) switch f.Kind() {