Skip to content

Commit

Permalink
优化convertToSliceFunc逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 committed Jan 13, 2025
1 parent 9863133 commit 345c177
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions database/gdb/gdb_convert_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 345c177

Please sign in to comment.