Use Convert() for arrays in reflectutil.ConvertSlice()

This commit is contained in:
Elara 2022-05-07 15:02:06 -07:00
parent f609d5a97f
commit a7a2dc3270

View File

@ -126,11 +126,11 @@ func ConvertSlice(in []any, to reflect.Type) any {
// Create new value for output // Create new value for output
out := reflect.New(to).Elem() out := reflect.New(to).Elem()
// Get type of slice elements
outType := out.Type().Elem()
// If output value is a slice // If output value is a slice
if out.Kind() == reflect.Slice { if out.Kind() == reflect.Slice {
// Get type of slice elements
outType := out.Type().Elem()
// For every value provided // For every value provided
for i := 0; i < len(in); i++ { for i := 0; i < len(in); i++ {
// Get value of input type // Get value of input type
@ -170,13 +170,12 @@ func ConvertSlice(in []any, to reflect.Type) any {
// Set output value to input value // Set output value to input value
outVal.Set(inVal) outVal.Set(inVal)
} else { } else {
// If input value can be converted to output type newVal, err := Convert(inVal, outType)
if inVal.CanConvert(outVal.Type()) { if err != nil {
// Convert and set output value to input value
outVal.Set(inVal.Convert(outVal.Type()))
} else {
// Set output value to its zero value // Set output value to its zero value
outVal.Set(reflect.Zero(outVal.Type())) outVal.Set(reflect.Zero(outVal.Type()))
} else {
outVal.Set(newVal)
} }
} }
} }