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

Switch to scylladb/gocql #274

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
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
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.ReplaceAll(collectionType, "(", "<")
collectionType = strings.ReplaceAll(collectionType, ")", ">")
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 @@ -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)
Expand All @@ -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")
}
}
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
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 @@ -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) {
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ go 1.17

require (
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1
github.com/google/go-cmp v0.5.4
github.com/google/go-cmp v0.6.0
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef
github.com/scylladb/go-reflectx v1.0.1
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sync v0.7.0
gopkg.in/inf.v0 v0.9.1
)

require (
github.com/golang/snappy v0.0.4 // indirect
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
)

replace github.com/gocql/gocql => github.com/scylladb/gocql v1.14.0
26 changes: 20 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ 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-20211015133455-b225f9b53fa1 h1:px9qUCy/RNJNsfCam4m2IxWGxNuimkrioEF0vrrbPsg=
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -22,11 +23,24 @@ github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef h1:NKxTG6GVGbfMXc2m
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef/go.mod h1:tcaRap0jS3eifrEEllL6ZMd9dg8IlDpi2S1oARrQ+NI=
github.com/scylladb/go-reflectx v1.0.1 h1:b917wZM7189pZdlND9PbIJ6NQxfDPfBvUaQ7cjj1iZQ=
github.com/scylladb/go-reflectx v1.0.1/go.mod h1:rWnOfDIRWBGN0miMLIcoPt/Dhi2doCMZqwMCJ3KupFc=
github.com/scylladb/gocql v1.14.0 h1:MuQ2sEOHxqTmCWS8zoH34SD5bpm3NbXi7CQbIVxSsZY=
github.com/scylladb/gocql v1.14.0/go.mod h1:ZLEJ0EVE5JhmtxIW2stgHq/v1P4fWap0qyyXSKyV8K0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
Loading