Skip to content

Commit

Permalink
improve slice handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fschoell committed Nov 6, 2024
1 parent 3d753a0 commit 983b374
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions db/dialect_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,19 @@ func convertToType(value string, valueType reflect.Type) (any, error) {
case reflect.String:
return value, nil
case reflect.Slice:
// in case the slice values are wrapped in single quotes replace them to use json.Unmarshal which requires double quotes
value = strings.ReplaceAll(value, "'", "\"")
var anySlice []any
if err := json.Unmarshal([]byte(value), &anySlice); err != nil {
return "", fmt.Errorf("could not unmarshal slice value %q: %w", value, err)
if valueType.Elem().Kind() == reflect.Struct || valueType.Elem().Kind() == reflect.Ptr {
return nil, fmt.Errorf("%q is not supported as Clickhouse Array type", valueType.Elem().Name())
}

for i, v := range anySlice {
convertedType, err := convertToType(fmt.Sprintf("%v", v), valueType.Elem())
if err != nil {
return "", fmt.Errorf("converting value %q to type %q in column %q: %w", v, valueType.Elem(), i, err)
}
anySlice[i] = convertedType
// in case the slice values are wrapped in single quotes, replace them to use json.Unmarshal which requires double quotes
value = strings.ReplaceAll(value, "'", "\"")

res := reflect.New(reflect.SliceOf(valueType.Elem()))
if err := json.Unmarshal([]byte(value), res.Interface()); err != nil {
return "", fmt.Errorf("could not unmarshal slice value %q: %w", value, err)
}

return anySlice, nil
return res.Elem().Interface(), nil
case reflect.Bool:
return strconv.ParseBool(value)
case reflect.Int:
Expand Down

0 comments on commit 983b374

Please sign in to comment.