Skip to content

Commit

Permalink
Move from gocql/gocql to scylladb/gocql
Browse files Browse the repository at this point in the history
  • Loading branch information
dkropachev committed Jun 16, 2024
1 parent 624fc1d commit 92a1078
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 38 deletions.
7 changes: 3 additions & 4 deletions cmd/schemagen/keyspace.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}
}
Expand Down
18 changes: 0 additions & 18 deletions cmd/schemagen/map_types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/gocql/gocql"
)

var types = map[string]string{
Expand Down Expand Up @@ -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.Replace(collectionType, "(", "<", -1)
collectionType = strings.Replace(collectionType, ")", ">", -1)
return collectionType
default:
panic(fmt.Sprintf("Did not expect %v type in user defined type", tType))
}
}
21 changes: 12 additions & 9 deletions cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path"
"regexp"
"sort"
"strings"

"github.com/gocql/gocql"
Expand Down Expand Up @@ -78,7 +79,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 {
Expand All @@ -101,25 +101,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")
}
}
Expand All @@ -129,7 +132,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,
}

Expand Down Expand Up @@ -175,10 +178,10 @@ var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)(?:>|,)`) // match all types co
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
Expand Down
4 changes: 2 additions & 2 deletions cmd/schemagen/schemagen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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) {
Expand All @@ -108,7 +108,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)
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCS
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537 h1:NaMut1fdw76YYX/TPinSAbai4DShF5tPort3bHpET6g=
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 h1:px9qUCy/RNJNsfCam4m2IxWGxNuimkrioEF0vrrbPsg=
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down

0 comments on commit 92a1078

Please sign in to comment.