Skip to content

Commit

Permalink
Fixed issue with collection types in udt. empty type colums skiped. A…
Browse files Browse the repository at this point in the history
…dded test for udt.
  • Loading branch information
pavle995 authored and mmatczuk committed Jun 30, 2022
1 parent a62ba24 commit fc92258
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
6 changes: 4 additions & 2 deletions cmd/schemagen/keyspace.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var (
{{range .}}
{{- $type_name := .Name | camelize}}
{{- $field_types := .FieldTypes}}
type {{$type_name}}Type struct {
type {{$type_name}}UserType struct {
{{- range $index, $element := .FieldNames}}
{{- $type := index $field_types $index}}
{{. | camelize}} {{getNativeTypeSting $type | mapScyllaToGoType}}
{{. | camelize}} {{typeToString $type | mapScyllaToGoType}}
{{- end}}
}
{{- end}}
Expand All @@ -54,7 +54,9 @@ type {{$type_name}}Type struct {
{{- $model_name := .Name | camelize}}
type {{$model_name}}Struct struct {
{{- range .Columns}}
{{- if not (eq .Validator "empty") }}
{{.Name | camelize}} {{.Validator | mapScyllaToGoType}}
{{- end}}
{{- end}}
}
{{- end}}
Expand Down
26 changes: 22 additions & 4 deletions cmd/schemagen/map_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -33,11 +34,17 @@ var types = map[string]string{
}

func mapScyllaToGoType(s string) string {
frozenRegex := regexp.MustCompile(`frozen<([a-z]*)>`)
match := frozenRegex.FindAllStringSubmatch(s, -1)
if match != nil {
s = match[0][1]
}

mapRegex := regexp.MustCompile(`map<([a-z]*), ([a-z]*)>`)
setRegex := regexp.MustCompile(`set<([a-z]*)>`)
listRegex := regexp.MustCompile(`list<([a-z]*)>`)
tupleRegex := regexp.MustCompile(`tuple<(?:([a-z]*),? ?)*>`)
match := mapRegex.FindAllStringSubmatch(s, -1)
match = mapRegex.FindAllStringSubmatch(s, -1)
if match != nil {
key := match[0][1]
value := match[0][2]
Expand Down Expand Up @@ -79,9 +86,20 @@ func mapScyllaToGoType(s string) string {
return t
}

return camelize(s) + "Type"
return camelize(s) + "UserType"
}

func getNativeTypeSting(t gocql.NativeType) string {
return t.String()
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))
}
}
2 changes: 1 addition & 1 deletion cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}).
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
Funcs(template.FuncMap{"getNativeTypeSting": getNativeTypeSting}).
Funcs(template.FuncMap{"typeToString": typeToString}).
Parse(keyspaceTmpl)

if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/schemagen/schemagen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ func createTestSchema(t *testing.T) {
t.Fatal("create table:", err)
}

err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS schemagen.album (
name text,
songwriters set<text>,)`)
if err != nil {
t.Fatal("create type:", err)
}

err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
id uuid,
title text,
album text,
album frozen<album>,
artist text,
song_id uuid,
PRIMARY KEY (id, title, album, artist))`)
Expand Down
7 changes: 6 additions & 1 deletion cmd/schemagen/testdata/models.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ var (
})
)

type AlbumUserType struct {
Name string
Songwriters []string
}

type PlaylistsStruct struct {
Album string
Album AlbumUserType
Artist string
Id [16]byte
SongId [16]byte
Expand Down

0 comments on commit fc92258

Please sign in to comment.