Handle pointer types in reflectutil.Convert()

This commit is contained in:
Elara 2022-05-03 18:47:49 -07:00
parent 5e99a66007
commit 1269203c08
1 changed files with 12 additions and 0 deletions

View File

@ -36,6 +36,18 @@ func Convert(in reflect.Value, toType reflect.Type) (reflect.Value, error) {
return in, nil
}
// If the output type is a pointer to the input type
if reflect.PtrTo(inType) == toType {
// Return pointer to input
return in.Addr(), nil
}
// If input is a pointer pointing to the output type
if inType.Kind() == reflect.Ptr && inType.Elem() == toType {
// Return value being pointed at by input
return reflect.Indirect(in), nil
}
// If input can be converted to desired type, convert and return
if in.CanConvert(toType) {
return in.Convert(toType), nil