From 1269203c087bad41eed6cae67ea8c6d2fd9b16ae Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Tue, 3 May 2022 18:47:49 -0700 Subject: [PATCH] Handle pointer types in reflectutil.Convert() --- internal/reflectutil/utils.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/reflectutil/utils.go b/internal/reflectutil/utils.go index f5b90c8..20a7a0a 100644 --- a/internal/reflectutil/utils.go +++ b/internal/reflectutil/utils.go @@ -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