Skip to content

Commit fc92258

Browse files
pavle995mmatczuk
authored andcommitted
Fixed issue with collection types in udt. empty type colums skiped. Added test for udt.
1 parent a62ba24 commit fc92258

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

Diff for: cmd/schemagen/keyspace.tmpl

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ var (
4040
{{range .}}
4141
{{- $type_name := .Name | camelize}}
4242
{{- $field_types := .FieldTypes}}
43-
type {{$type_name}}Type struct {
43+
type {{$type_name}}UserType struct {
4444
{{- range $index, $element := .FieldNames}}
4545
{{- $type := index $field_types $index}}
46-
{{. | camelize}} {{getNativeTypeSting $type | mapScyllaToGoType}}
46+
{{. | camelize}} {{typeToString $type | mapScyllaToGoType}}
4747
{{- end}}
4848
}
4949
{{- end}}
@@ -54,7 +54,9 @@ type {{$type_name}}Type struct {
5454
{{- $model_name := .Name | camelize}}
5555
type {{$model_name}}Struct struct {
5656
{{- range .Columns}}
57+
{{- if not (eq .Validator "empty") }}
5758
{{.Name | camelize}} {{.Validator | mapScyllaToGoType}}
59+
{{- end}}
5860
{{- end}}
5961
}
6062
{{- end}}

Diff for: cmd/schemagen/map_types.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"regexp"
56
"strconv"
67
"strings"
@@ -33,11 +34,17 @@ var types = map[string]string{
3334
}
3435

3536
func mapScyllaToGoType(s string) string {
37+
frozenRegex := regexp.MustCompile(`frozen<([a-z]*)>`)
38+
match := frozenRegex.FindAllStringSubmatch(s, -1)
39+
if match != nil {
40+
s = match[0][1]
41+
}
42+
3643
mapRegex := regexp.MustCompile(`map<([a-z]*), ([a-z]*)>`)
3744
setRegex := regexp.MustCompile(`set<([a-z]*)>`)
3845
listRegex := regexp.MustCompile(`list<([a-z]*)>`)
3946
tupleRegex := regexp.MustCompile(`tuple<(?:([a-z]*),? ?)*>`)
40-
match := mapRegex.FindAllStringSubmatch(s, -1)
47+
match = mapRegex.FindAllStringSubmatch(s, -1)
4148
if match != nil {
4249
key := match[0][1]
4350
value := match[0][2]
@@ -79,9 +86,20 @@ func mapScyllaToGoType(s string) string {
7986
return t
8087
}
8188

82-
return camelize(s) + "Type"
89+
return camelize(s) + "UserType"
8390
}
8491

85-
func getNativeTypeSting(t gocql.NativeType) string {
86-
return t.String()
92+
func typeToString(t interface{}) string {
93+
tType := fmt.Sprintf("%T", t)
94+
switch tType {
95+
case "gocql.NativeType":
96+
return t.(gocql.NativeType).String()
97+
case "gocql.CollectionType":
98+
collectionType := t.(gocql.CollectionType).String()
99+
collectionType = strings.Replace(collectionType, "(", "<", -1)
100+
collectionType = strings.Replace(collectionType, ")", ">", -1)
101+
return collectionType
102+
default:
103+
panic(fmt.Sprintf("Did not expect %v type in user defined type", tType))
104+
}
87105
}

Diff for: cmd/schemagen/schemagen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
7575
New("keyspace.tmpl").
7676
Funcs(template.FuncMap{"camelize": camelize}).
7777
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
78-
Funcs(template.FuncMap{"getNativeTypeSting": getNativeTypeSting}).
78+
Funcs(template.FuncMap{"typeToString": typeToString}).
7979
Parse(keyspaceTmpl)
8080

8181
if err != nil {

Diff for: cmd/schemagen/schemagen_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ func createTestSchema(t *testing.T) {
5656
t.Fatal("create table:", err)
5757
}
5858

59+
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS schemagen.album (
60+
name text,
61+
songwriters set<text>,)`)
62+
if err != nil {
63+
t.Fatal("create type:", err)
64+
}
65+
5966
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
6067
id uuid,
6168
title text,
62-
album text,
69+
album frozen<album>,
6370
artist text,
6471
song_id uuid,
6572
PRIMARY KEY (id, title, album, artist))`)

Diff for: cmd/schemagen/testdata/models.go.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ var (
4444
})
4545
)
4646

47+
type AlbumUserType struct {
48+
Name string
49+
Songwriters []string
50+
}
51+
4752
type PlaylistsStruct struct {
48-
Album string
53+
Album AlbumUserType
4954
Artist string
5055
Id [16]byte
5156
SongId [16]byte

0 commit comments

Comments
 (0)