Added tests, example and updated a few types

This commit is contained in:
Pavle Kosutic
2021-12-12 09:34:17 +01:00
committed by Michal Jan Matczuk
parent 2942397ab6
commit a62ba24cf9
5 changed files with 250 additions and 12 deletions

View File

@@ -14,20 +14,20 @@ var types = map[string]string{
"blob": "[]byte",
"boolean": "bool",
"counter": "int",
"date": "string",
"decimal": "float32",
"date": "time.Time",
"decimal": "inf.Dec",
"double": "float64",
"duration": "unit32",
"duration": "gocql.Duration",
"float": "float32",
"inet": "string",
"int": "int32",
"smallint": "int16",
"text": "string",
"time": "uint32",
"timestamp": "uint32",
"timeuuid": "string",
"time": "time.Duration",
"timestamp": "time.Time",
"timeuuid": "[16]byte",
"tinyint": "int8",
"uuid": "gocql.UUID",
"uuid": "[16]byte",
"varchar": "string",
"varint": "int64",
}
@@ -67,7 +67,7 @@ func mapScyllaToGoType(s string) string {
typeStr := "struct {\n"
for i, t := range types {
typeStr = typeStr + "\t\tFiled" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
typeStr = typeStr + "\t\tField" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
}
typeStr = typeStr + "\t}"

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
package main
import (
"testing"
)
func TestMapScyllaToGoType(t *testing.T) {
tests := []struct {
input string
want string
}{
{"ascii", "string"},
{"bigint", "int64"},
{"blob", "[]byte"},
{"boolean", "bool"},
{"counter", "int"},
{"date", "time.Time"},
{"decimal", "inf.Dec"},
{"double", "float64"},
{"duration", "gocql.Duration"},
{"float", "float32"},
{"inet", "string"},
{"int", "int32"},
{"smallint", "int16"},
{"text", "string"},
{"time", "time.Duration"},
{"timestamp", "time.Time"},
{"timeuuid", "[16]byte"},
{"tinyint", "int8"},
{"uuid", "[16]byte"},
{"varchar", "string"},
{"varint", "int64"},
{"map<int, text>", "map[int32]string"},
{"list<int>", "[]int32"},
{"set<int>", "[]int32"},
{"tuple<boolean, int, smallint>", "struct {\n\t\tField1 bool\n\t\tField2 int32\n\t\tField3 int16\n\t}"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := mapScyllaToGoType(tt.input); got != tt.want {
t.Errorf("mapScyllaToGoType() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -5,6 +5,7 @@ import (
_ "embed"
"flag"
"fmt"
"go/format"
"html/template"
"io/ioutil"
"log"
@@ -84,7 +85,13 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
imports := make([]string, 0)
for _, t := range md.Tables {
for _, c := range t.Columns {
if c.Validator == "uuid" && !existsInSlice(imports, "github.com/gocql/gocql") {
if (c.Validator == "timestamp" || c.Validator == "date" || c.Validator == "duration" || c.Validator == "time") && !existsInSlice(imports, "time") {
imports = append(imports, "time")
}
if c.Validator == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
imports = append(imports, "gopkg.in/inf.v0")
}
if c.Validator == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
imports = append(imports, "github.com/gocql/gocql")
}
}
@@ -101,8 +108,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
if err = t.Execute(buf, data); err != nil {
return nil, fmt.Errorf("template: %w", err)
}
//return format.Source(buf.Bytes())
return buf.Bytes(), nil
return format.Source(buf.Bytes())
}
func createSession() (gocqlx.Session, error) {

View File

@@ -2,7 +2,9 @@
package foobar
import "github.com/scylladb/gocqlx/v2/table"
import (
"github.com/scylladb/gocqlx/v2/table"
)
// Table models.
var (
@@ -41,3 +43,19 @@ var (
SortKey: []string{},
})
)
type PlaylistsStruct struct {
Album string
Artist string
Id [16]byte
SongId [16]byte
Title string
}
type SongsStruct struct {
Album string
Artist string
Data []byte
Id [16]byte
Tags []string
Title string
}