mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:18:31 +02:00
refactor(serialization): simplify string conversion logic in Convert function
This commit is contained in:
@@ -441,28 +441,8 @@ func Convert(src reflect.Value, dst reflect.Value, checkValidateTag bool) gperr.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case dstT.Kind() == reflect.String:
|
case dstT.Kind() == reflect.String:
|
||||||
if src.Kind() == reflect.Bool { // bool to string
|
dst.SetString(gi.ReflectToStr(src))
|
||||||
if !dst.CanSet() {
|
return nil
|
||||||
return ErrUnsettable.Subject(dstT.String())
|
|
||||||
}
|
|
||||||
dst.SetString(strconv.FormatBool(src.Bool()))
|
|
||||||
return nil
|
|
||||||
} else if gi.ReflectIsNumeric(src) { // numeric to string
|
|
||||||
if !dst.CanSet() {
|
|
||||||
return ErrUnsettable.Subject(dstT.String())
|
|
||||||
}
|
|
||||||
var strV string
|
|
||||||
switch {
|
|
||||||
case src.CanInt():
|
|
||||||
strV = strconv.FormatInt(src.Int(), 10)
|
|
||||||
case src.CanUint():
|
|
||||||
strV = strconv.FormatUint(src.Uint(), 10)
|
|
||||||
case src.CanFloat():
|
|
||||||
strV = strconv.FormatFloat(src.Float(), 'f', -1, 64)
|
|
||||||
}
|
|
||||||
dst.SetString(strV)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
case srcKind == reflect.Map: // map to map
|
case srcKind == reflect.Map: // map to map
|
||||||
if src.Len() == 0 {
|
if src.Len() == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user