From 345c177dd59e157941ebd55ae81cae7fc8080ede Mon Sep 17 00:00:00 2001 From: wln32 Date: Mon, 13 Jan 2025 20:00:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96convertToSliceFunc=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/gdb/gdb_convert_config.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/database/gdb/gdb_convert_config.go b/database/gdb/gdb_convert_config.go index e311651778b..ffc2ec7d91a 100644 --- a/database/gdb/gdb_convert_config.go +++ b/database/gdb/gdb_convert_config.go @@ -36,10 +36,25 @@ func init() { convertConfig.RegisterInterfaceTypeConvertFunc(reflectTypeFor[sql.Scanner](), sqlScanner) } -func convertToSliceFunc(from any, to reflect.Value) error { - dst := to.Addr().Interface() - sv := from.(*gvar.Var).Bytes() - err := json.Unmarshal(sv, dst) +func convertToSliceFunc(from any, to reflect.Value) (err error) { + fromVal := from.(*gvar.Var).Val() + switch x := fromVal.(type) { + case []byte: + dst := to.Addr().Interface() + err = json.Unmarshal(x, dst) + case string: + dst := to.Addr().Interface() + err = json.Unmarshal([]byte(x), dst) + default: + sv := reflect.ValueOf(fromVal) + switch sv.Kind() { + case reflect.Slice: + dv := gconv.Convert(fromVal, to.Type().String()) + to.Set(reflect.ValueOf(dv)) + default: + err = fmt.Errorf("conversion from `%v(%T)` to `%v(%T)` is not supported", fromVal, fromVal, to, to) + } + } return err }