From a7a2dc32706b5784632d616b7eea209c42354688 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sat, 7 May 2022 15:02:06 -0700 Subject: [PATCH] Use Convert() for arrays in reflectutil.ConvertSlice() --- internal/reflectutil/utils.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/reflectutil/utils.go b/internal/reflectutil/utils.go index 0c7a6c1..61f478f 100644 --- a/internal/reflectutil/utils.go +++ b/internal/reflectutil/utils.go @@ -126,11 +126,11 @@ func ConvertSlice(in []any, to reflect.Type) any { // Create new value for output out := reflect.New(to).Elem() + // Get type of slice elements + outType := out.Type().Elem() + // If output value is a slice if out.Kind() == reflect.Slice { - // Get type of slice elements - outType := out.Type().Elem() - // For every value provided for i := 0; i < len(in); i++ { // Get value of input type @@ -170,13 +170,12 @@ func ConvertSlice(in []any, to reflect.Type) any { // Set output value to input value outVal.Set(inVal) } else { - // If input value can be converted to output type - if inVal.CanConvert(outVal.Type()) { - // Convert and set output value to input value - outVal.Set(inVal.Convert(outVal.Type())) - } else { + newVal, err := Convert(inVal, outType) + if err != nil { // Set output value to its zero value outVal.Set(reflect.Zero(outVal.Type())) + } else { + outVal.Set(newVal) } } }