Skip to content

Commit 2942397

Browse files
pavle995mmatczuk
authored andcommitted
Code generation for structs for tables
1 parent 3e15114 commit 2942397

File tree

5 files changed

+149
-5
lines changed

5 files changed

+149
-5
lines changed

cmd/schemagen/keyspace.tmpl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
package {{.PackageName}}
44

5-
import "github.com/scylladb/gocqlx/v2/table"
5+
import (
6+
"github.com/scylladb/gocqlx/v2/table"
7+
{{- range .Imports}}
8+
"{{.}}"
9+
{{- end}}
10+
)
611

712
// Table models.
813
var (
@@ -30,3 +35,27 @@ var (
3035
{{end}}
3136
{{end}}
3237
)
38+
39+
{{with .UserTypes}}
40+
{{range .}}
41+
{{- $type_name := .Name | camelize}}
42+
{{- $field_types := .FieldTypes}}
43+
type {{$type_name}}Type struct {
44+
{{- range $index, $element := .FieldNames}}
45+
{{- $type := index $field_types $index}}
46+
{{. | camelize}} {{getNativeTypeSting $type | mapScyllaToGoType}}
47+
{{- end}}
48+
}
49+
{{- end}}
50+
{{- end}}
51+
52+
{{with .Tables}}
53+
{{range .}}
54+
{{- $model_name := .Name | camelize}}
55+
type {{$model_name}}Struct struct {
56+
{{- range .Columns}}
57+
{{.Name | camelize}} {{.Validator | mapScyllaToGoType}}
58+
{{- end}}
59+
}
60+
{{- end}}
61+
{{- end}}

cmd/schemagen/map_types.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/gocql/gocql"
9+
)
10+
11+
var types = map[string]string{
12+
"ascii": "string",
13+
"bigint": "int64",
14+
"blob": "[]byte",
15+
"boolean": "bool",
16+
"counter": "int",
17+
"date": "string",
18+
"decimal": "float32",
19+
"double": "float64",
20+
"duration": "unit32",
21+
"float": "float32",
22+
"inet": "string",
23+
"int": "int32",
24+
"smallint": "int16",
25+
"text": "string",
26+
"time": "uint32",
27+
"timestamp": "uint32",
28+
"timeuuid": "string",
29+
"tinyint": "int8",
30+
"uuid": "gocql.UUID",
31+
"varchar": "string",
32+
"varint": "int64",
33+
}
34+
35+
func mapScyllaToGoType(s string) string {
36+
mapRegex := regexp.MustCompile(`map<([a-z]*), ([a-z]*)>`)
37+
setRegex := regexp.MustCompile(`set<([a-z]*)>`)
38+
listRegex := regexp.MustCompile(`list<([a-z]*)>`)
39+
tupleRegex := regexp.MustCompile(`tuple<(?:([a-z]*),? ?)*>`)
40+
match := mapRegex.FindAllStringSubmatch(s, -1)
41+
if match != nil {
42+
key := match[0][1]
43+
value := match[0][2]
44+
45+
return "map[" + types[key] + "]" + types[value]
46+
}
47+
48+
match = setRegex.FindAllStringSubmatch(s, -1)
49+
if match != nil {
50+
key := match[0][1]
51+
52+
return "[]" + types[key]
53+
}
54+
55+
match = listRegex.FindAllStringSubmatch(s, -1)
56+
if match != nil {
57+
key := match[0][1]
58+
59+
return "[]" + types[key]
60+
}
61+
62+
match = tupleRegex.FindAllStringSubmatch(s, -1)
63+
if match != nil {
64+
tuple := match[0][0]
65+
subStr := tuple[6 : len(tuple)-1]
66+
types := strings.Split(subStr, ", ")
67+
68+
typeStr := "struct {\n"
69+
for i, t := range types {
70+
typeStr = typeStr + "\t\tFiled" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
71+
}
72+
typeStr = typeStr + "\t}"
73+
74+
return typeStr
75+
}
76+
77+
t, exists := types[s]
78+
if exists {
79+
return t
80+
}
81+
82+
return camelize(s) + "Type"
83+
}
84+
85+
func getNativeTypeSting(t gocql.NativeType) string {
86+
return t.String()
87+
}

cmd/schemagen/schemagen.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
_ "embed"
66
"flag"
77
"fmt"
8-
"go/format"
98
"html/template"
109
"io/ioutil"
1110
"log"
@@ -74,22 +73,36 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
7473
t, err := template.
7574
New("keyspace.tmpl").
7675
Funcs(template.FuncMap{"camelize": camelize}).
76+
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
77+
Funcs(template.FuncMap{"getNativeTypeSting": getNativeTypeSting}).
7778
Parse(keyspaceTmpl)
7879

7980
if err != nil {
8081
log.Fatalln("unable to parse models template:", err)
8182
}
8283

84+
imports := make([]string, 0)
85+
for _, t := range md.Tables {
86+
for _, c := range t.Columns {
87+
if c.Validator == "uuid" && !existsInSlice(imports, "github.com/gocql/gocql") {
88+
imports = append(imports, "github.com/gocql/gocql")
89+
}
90+
}
91+
}
92+
8393
buf := &bytes.Buffer{}
8494
data := map[string]interface{}{
8595
"PackageName": *flagPkgname,
8696
"Tables": md.Tables,
97+
"UserTypes": md.UserTypes,
98+
"Imports": imports,
8799
}
88100

89101
if err = t.Execute(buf, data); err != nil {
90102
return nil, fmt.Errorf("template: %w", err)
91103
}
92-
return format.Source(buf.Bytes())
104+
//return format.Source(buf.Bytes())
105+
return buf.Bytes(), nil
93106
}
94107

95108
func createSession() (gocqlx.Session, error) {
@@ -106,3 +119,13 @@ func createSession() (gocqlx.Session, error) {
106119
func clusterHosts() []string {
107120
return strings.Split(*flagCluster, ",")
108121
}
122+
123+
func existsInSlice(s []string, v string) bool {
124+
for _, i := range s {
125+
if v == i {
126+
return true
127+
}
128+
}
129+
130+
return false
131+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/scylladb/gocqlx/v2
33
go 1.17
44

55
require (
6-
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537
6+
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1
77
github.com/google/go-cmp v0.5.4
88
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef
99
github.com/scylladb/go-reflectx v1.0.1
@@ -12,7 +12,7 @@ require (
1212
)
1313

1414
require (
15-
github.com/golang/snappy v0.0.1 // indirect
15+
github.com/golang/snappy v0.0.4 // indirect
1616
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
1717
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
1818
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537 h1:NaMut1fdw76YYX/TPinSAbai4DShF5tPort3bHpET6g=
77
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
8+
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 h1:px9qUCy/RNJNsfCam4m2IxWGxNuimkrioEF0vrrbPsg=
9+
github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
810
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
911
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
1012
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
13+
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
14+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
15+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
1116
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
1217
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1318
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=

0 commit comments

Comments
 (0)