From 073ebbd8ba1e33b3aee45c2dfbfbca0d47cbb363 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sun, 1 May 2022 03:25:32 -0700 Subject: [PATCH] Call Convert() in ConvertSlice() to ensure proper conversion of maps and nested slices --- internal/reflectutil/utils.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/reflectutil/utils.go b/internal/reflectutil/utils.go index 7a8a1a0..a7b76c7 100644 --- a/internal/reflectutil/utils.go +++ b/internal/reflectutil/utils.go @@ -72,10 +72,10 @@ func Convert(in reflect.Value, toType reflect.Type) (reflect.Value, error) { if in.Type() == reflect.TypeOf([]any{}) && to.Kind() == reflect.Slice || to.Kind() == reflect.Array { // Use ConvertSlice to convert value - to.Set(reflect.ValueOf(ConvertSlice( + return reflect.ValueOf(ConvertSlice( in.Interface().([]any), toType, - ))) + )), nil } return to, fmt.Errorf("cannot convert %s to %s", inType, toType) @@ -104,13 +104,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(outType) { - // Convert and set output value to input value - outVal.Set(inVal.Convert(outType)) - } 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) } }