Move from gocql/gocql to scylladb/gocql

This commit is contained in:
Dmitry Kropachev
2024-06-16 08:40:32 -04:00
committed by Sylwia Szunejko
parent ab80d70106
commit 207ba8723e
6 changed files with 41 additions and 42 deletions

View File

@@ -42,8 +42,7 @@ var (
{{- $field_types := .FieldTypes}}
type {{$type_name}}UserType struct {
{{- range $index, $element := .FieldNames}}
{{- $type := index $field_types $index}}
{{. | camelize}} {{typeToString $type | mapScyllaToGoType}}
{{. | camelize}} {{(index $field_types $index) | mapScyllaToGoType}}
{{- end}}
}
{{- end}}
@@ -54,8 +53,8 @@ type {{$type_name}}UserType struct {
{{- $model_name := .Name | camelize}}
type {{$model_name}}Struct struct {
{{- range .Columns}}
{{- if not (eq .Validator "empty") }}
{{.Name | camelize}} {{.Validator | mapScyllaToGoType}}
{{- if not (eq .Type "empty") }}
{{.Name | camelize}} {{.Type | mapScyllaToGoType}}
{{- end}}
{{- end}}
}

View File

@@ -1,12 +1,9 @@
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/gocql/gocql"
)
var types = map[string]string{
@@ -88,18 +85,3 @@ func mapScyllaToGoType(s string) string {
return camelize(s) + "UserType"
}
func typeToString(t interface{}) string {
tType := fmt.Sprintf("%T", t)
switch tType {
case "gocql.NativeType":
return t.(gocql.NativeType).String()
case "gocql.CollectionType":
collectionType := t.(gocql.CollectionType).String()
collectionType = strings.ReplaceAll(collectionType, "(", "<")
collectionType = strings.ReplaceAll(collectionType, ")", ">")
return collectionType
default:
panic(fmt.Sprintf("Did not expect %v type in user defined type", tType))
}
}

View File

@@ -12,6 +12,7 @@ import (
"os"
"path"
"regexp"
"sort"
"strings"
"github.com/gocql/gocql"
@@ -77,7 +78,6 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}).
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
Funcs(template.FuncMap{"typeToString": typeToString}).
Parse(keyspaceTmpl)
if err != nil {
log.Fatalln("unable to parse models template:", err)
@@ -99,25 +99,28 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
}
orphanedTypes := make(map[string]struct{})
for userTypeName := range md.UserTypes {
for userTypeName := range md.Types {
if !usedInTables(userTypeName, md.Tables) {
orphanedTypes[userTypeName] = struct{}{}
}
}
for typeName := range orphanedTypes {
delete(md.UserTypes, typeName)
delete(md.Types, typeName)
}
imports := make([]string, 0)
for _, t := range md.Tables {
// Ensure ordered columns are sorted alphabetically
sort.Strings(t.OrderedColumns)
for _, c := range t.Columns {
if (c.Validator == "timestamp" || c.Validator == "date" || c.Validator == "duration" || c.Validator == "time") && !existsInSlice(imports, "time") {
if (c.Type == "timestamp" || c.Type == "date" || c.Type == "duration" || c.Type == "time") && !existsInSlice(imports, "time") {
imports = append(imports, "time")
}
if c.Validator == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
if c.Type == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
imports = append(imports, "gopkg.in/inf.v0")
}
if c.Validator == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
if c.Type == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
imports = append(imports, "github.com/gocql/gocql")
}
}
@@ -127,7 +130,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
data := map[string]interface{}{
"PackageName": *flagPkgname,
"Tables": md.Tables,
"UserTypes": md.UserTypes,
"UserTypes": md.Types,
"Imports": imports,
}
@@ -173,10 +176,10 @@ var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)[>,]`) // match all types conta
func usedInTables(typeName string, tables map[string]*gocql.TableMetadata) bool {
for _, table := range tables {
for _, column := range table.Columns {
if typeName == column.Validator {
if typeName == column.Type {
return true
}
matches := userTypes.FindAllStringSubmatch(column.Validator, -1)
matches := userTypes.FindAllStringSubmatch(column.Type, -1)
for _, s := range matches {
if s[1] == typeName {
return true

View File

@@ -97,7 +97,7 @@ func Test_usedInTables(t *testing.T) {
t.Run(name, func(t *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Validator: tt.columnValidator},
"column": {Type: tt.columnValidator},
}},
}
if !usedInTables(tt.typeName, tables) {
@@ -109,7 +109,7 @@ func Test_usedInTables(t *testing.T) {
t.Run("doesn't panic with empty type name", func(t *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Validator: "map<text, album>"},
"column": {Type: "map<text, album>"},
}},
}
usedInTables("", tables)