Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve slice handling #27

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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