Fixed issue with collection types in udt. empty type colums skiped. Added test for udt.

This commit is contained in:
pavle995
2022-05-08 15:08:09 +02:00
committed by Michal Jan Matczuk
parent a62ba24cf9
commit fc92258512
5 changed files with 41 additions and 9 deletions

View File

@@ -40,10 +40,10 @@ var (
{{range .}} {{range .}}
{{- $type_name := .Name | camelize}} {{- $type_name := .Name | camelize}}
{{- $field_types := .FieldTypes}} {{- $field_types := .FieldTypes}}
type {{$type_name}}Type struct { type {{$type_name}}UserType struct {
{{- range $index, $element := .FieldNames}} {{- range $index, $element := .FieldNames}}
{{- $type := index $field_types $index}} {{- $type := index $field_types $index}}
{{. | camelize}} {{getNativeTypeSting $type | mapScyllaToGoType}} {{. | camelize}} {{typeToString $type | mapScyllaToGoType}}
{{- end}} {{- end}}
} }
{{- end}} {{- end}}
@@ -54,8 +54,10 @@ type {{$type_name}}Type struct {
{{- $model_name := .Name | camelize}} {{- $model_name := .Name | camelize}}
type {{$model_name}}Struct struct { type {{$model_name}}Struct struct {
{{- range .Columns}} {{- range .Columns}}
{{- if not (eq .Validator "empty") }}
{{.Name | camelize}} {{.Validator | mapScyllaToGoType}} {{.Name | camelize}} {{.Validator | mapScyllaToGoType}}
{{- end}} {{- end}}
{{- end}}
} }
{{- end}} {{- end}}
{{- end}} {{- end}}

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -33,11 +34,17 @@ var types = map[string]string{
} }
func mapScyllaToGoType(s 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]*)>`) mapRegex := regexp.MustCompile(`map<([a-z]*), ([a-z]*)>`)
setRegex := regexp.MustCompile(`set<([a-z]*)>`) setRegex := regexp.MustCompile(`set<([a-z]*)>`)
listRegex := regexp.MustCompile(`list<([a-z]*)>`) listRegex := regexp.MustCompile(`list<([a-z]*)>`)
tupleRegex := regexp.MustCompile(`tuple<(?:([a-z]*),? ?)*>`) tupleRegex := regexp.MustCompile(`tuple<(?:([a-z]*),? ?)*>`)
match := mapRegex.FindAllStringSubmatch(s, -1) match = mapRegex.FindAllStringSubmatch(s, -1)
if match != nil { if match != nil {
key := match[0][1] key := match[0][1]
value := match[0][2] value := match[0][2]
@@ -79,9 +86,20 @@ func mapScyllaToGoType(s string) string {
return t return t
} }
return camelize(s) + "Type" return camelize(s) + "UserType"
} }
func getNativeTypeSting(t gocql.NativeType) string { func typeToString(t interface{}) string {
return t.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))
}
} }

View File

@@ -75,7 +75,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
New("keyspace.tmpl"). New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}). Funcs(template.FuncMap{"camelize": camelize}).
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}). Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
Funcs(template.FuncMap{"getNativeTypeSting": getNativeTypeSting}). Funcs(template.FuncMap{"typeToString": typeToString}).
Parse(keyspaceTmpl) Parse(keyspaceTmpl)
if err != nil { if err != nil {

View File

@@ -56,10 +56,17 @@ func createTestSchema(t *testing.T) {
t.Fatal("create table:", err) 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 ( err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
id uuid, id uuid,
title text, title text,
album text, album frozen<album>,
artist text, artist text,
song_id uuid, song_id uuid,
PRIMARY KEY (id, title, album, artist))`) PRIMARY KEY (id, title, album, artist))`)

View File

@@ -44,8 +44,13 @@ var (
}) })
) )
type AlbumUserType struct {
Name string
Songwriters []string
}
type PlaylistsStruct struct { type PlaylistsStruct struct {
Album string Album AlbumUserType
Artist string Artist string
Id [16]byte Id [16]byte
SongId [16]byte SongId [16]byte