Skip to content

Commit a62ba24

Browse files
pavle995mmatczuk
authored andcommitted
Added tests, example and updated a few types
1 parent 2942397 commit a62ba24

File tree

5 files changed

+250
-12
lines changed

5 files changed

+250
-12
lines changed

cmd/schemagen/map_types.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ var types = map[string]string{
1414
"blob": "[]byte",
1515
"boolean": "bool",
1616
"counter": "int",
17-
"date": "string",
18-
"decimal": "float32",
17+
"date": "time.Time",
18+
"decimal": "inf.Dec",
1919
"double": "float64",
20-
"duration": "unit32",
20+
"duration": "gocql.Duration",
2121
"float": "float32",
2222
"inet": "string",
2323
"int": "int32",
2424
"smallint": "int16",
2525
"text": "string",
26-
"time": "uint32",
27-
"timestamp": "uint32",
28-
"timeuuid": "string",
26+
"time": "time.Duration",
27+
"timestamp": "time.Time",
28+
"timeuuid": "[16]byte",
2929
"tinyint": "int8",
30-
"uuid": "gocql.UUID",
30+
"uuid": "[16]byte",
3131
"varchar": "string",
3232
"varint": "int64",
3333
}
@@ -67,7 +67,7 @@ func mapScyllaToGoType(s string) string {
6767

6868
typeStr := "struct {\n"
6969
for i, t := range types {
70-
typeStr = typeStr + "\t\tFiled" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
70+
typeStr = typeStr + "\t\tField" + strconv.Itoa(i+1) + " " + mapScyllaToGoType(t) + "\n"
7171
}
7272
typeStr = typeStr + "\t}"
7373

cmd/schemagen/map_types_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (C) 2017 ScyllaDB
2+
// Use of this source code is governed by a ALv2-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestMapScyllaToGoType(t *testing.T) {
12+
tests := []struct {
13+
input string
14+
want string
15+
}{
16+
{"ascii", "string"},
17+
{"bigint", "int64"},
18+
{"blob", "[]byte"},
19+
{"boolean", "bool"},
20+
{"counter", "int"},
21+
{"date", "time.Time"},
22+
{"decimal", "inf.Dec"},
23+
{"double", "float64"},
24+
{"duration", "gocql.Duration"},
25+
{"float", "float32"},
26+
{"inet", "string"},
27+
{"int", "int32"},
28+
{"smallint", "int16"},
29+
{"text", "string"},
30+
{"time", "time.Duration"},
31+
{"timestamp", "time.Time"},
32+
{"timeuuid", "[16]byte"},
33+
{"tinyint", "int8"},
34+
{"uuid", "[16]byte"},
35+
{"varchar", "string"},
36+
{"varint", "int64"},
37+
{"map<int, text>", "map[int32]string"},
38+
{"list<int>", "[]int32"},
39+
{"set<int>", "[]int32"},
40+
{"tuple<boolean, int, smallint>", "struct {\n\t\tField1 bool\n\t\tField2 int32\n\t\tField3 int16\n\t}"},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.input, func(t *testing.T) {
44+
if got := mapScyllaToGoType(tt.input); got != tt.want {
45+
t.Errorf("mapScyllaToGoType() = %v, want %v", got, tt.want)
46+
}
47+
})
48+
}
49+
}

cmd/schemagen/schemagen.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "embed"
66
"flag"
77
"fmt"
8+
"go/format"
89
"html/template"
910
"io/ioutil"
1011
"log"
@@ -84,7 +85,13 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
8485
imports := make([]string, 0)
8586
for _, t := range md.Tables {
8687
for _, c := range t.Columns {
87-
if c.Validator == "uuid" && !existsInSlice(imports, "github.com/gocql/gocql") {
88+
if (c.Validator == "timestamp" || c.Validator == "date" || c.Validator == "duration" || c.Validator == "time") && !existsInSlice(imports, "time") {
89+
imports = append(imports, "time")
90+
}
91+
if c.Validator == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
92+
imports = append(imports, "gopkg.in/inf.v0")
93+
}
94+
if c.Validator == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
8895
imports = append(imports, "github.com/gocql/gocql")
8996
}
9097
}
@@ -101,8 +108,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
101108
if err = t.Execute(buf, data); err != nil {
102109
return nil, fmt.Errorf("template: %w", err)
103110
}
104-
//return format.Source(buf.Bytes())
105-
return buf.Bytes(), nil
111+
return format.Source(buf.Bytes())
106112
}
107113

108114
func createSession() (gocqlx.Session, error) {

cmd/schemagen/testdata/models.go.txt

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

33
package foobar
44

5-
import "github.com/scylladb/gocqlx/v2/table"
5+
import (
6+
"github.com/scylladb/gocqlx/v2/table"
7+
)
68

79
// Table models.
810
var (
@@ -41,3 +43,19 @@ var (
4143
SortKey: []string{},
4244
})
4345
)
46+
47+
type PlaylistsStruct struct {
48+
Album string
49+
Artist string
50+
Id [16]byte
51+
SongId [16]byte
52+
Title string
53+
}
54+
type SongsStruct struct {
55+
Album string
56+
Artist string
57+
Data []byte
58+
Id [16]byte
59+
Tags []string
60+
Title string
61+
}

example_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestExample(t *testing.T) {
3737
session.ExecStmt(`DROP KEYSPACE examples`)
3838

3939
basicCreateAndPopulateKeyspace(t, session)
40+
createAndPopulateKeyspaceAllTypes(t, session)
4041
basicReadScyllaVersion(t, session)
4142

4243
datatypesBlob(t, session)
@@ -154,6 +155,170 @@ func basicCreateAndPopulateKeyspace(t *testing.T, session gocqlx.Session) {
154155
}
155156
}
156157

158+
// This example shows how to use query builders and table models to build
159+
// queries with all types. It uses "BindStruct" function for parameter binding and "Select"
160+
// function for loading data to a slice.
161+
func createAndPopulateKeyspaceAllTypes(t *testing.T, session gocqlx.Session) {
162+
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
163+
if err != nil {
164+
t.Fatal("create keyspace:", err)
165+
}
166+
167+
// generated with schemagen
168+
type CheckTypesStruct struct {
169+
AsciI string
170+
BigInt int64
171+
BloB []byte
172+
BooleaN bool
173+
DatE time.Time
174+
DecimaL inf.Dec
175+
DoublE float64
176+
DuratioN gocql.Duration
177+
FloaT float32
178+
Id [16]byte
179+
InT int32
180+
IneT string
181+
ListInt []int32
182+
MapIntText map[int32]string
183+
SetInt []int32
184+
SmallInt int16
185+
TexT string
186+
TimE time.Duration
187+
TimestamP time.Time
188+
TimeuuiD [16]byte
189+
TinyInt int8
190+
VarChar string
191+
VarInt int64
192+
}
193+
194+
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.check_types (
195+
asci_i ascii,
196+
big_int bigint,
197+
blo_b blob,
198+
boolea_n boolean,
199+
dat_e date,
200+
decima_l decimal,
201+
doubl_e double,
202+
duratio_n duration,
203+
floa_t float,
204+
ine_t inet,
205+
in_t int,
206+
small_int smallint,
207+
tex_t text,
208+
tim_e time,
209+
timestam_p timestamp,
210+
timeuui_d timeuuid,
211+
tiny_int tinyint,
212+
id uuid PRIMARY KEY,
213+
var_char varchar,
214+
var_int varint,
215+
map_int_text map<int, text>,
216+
list_int list<int>,
217+
set_int set<int>)`)
218+
if err != nil {
219+
t.Fatal("create table:", err)
220+
}
221+
222+
// generated with schemagen
223+
checkTypesTable := table.New(table.Metadata{
224+
Name: "examples.check_types",
225+
Columns: []string{
226+
"asci_i",
227+
"big_int",
228+
"blo_b",
229+
"boolea_n",
230+
"dat_e",
231+
"decima_l",
232+
"doubl_e",
233+
"duratio_n",
234+
"floa_t",
235+
"id",
236+
"in_t",
237+
"ine_t",
238+
"list_int",
239+
"map_int_text",
240+
"set_int",
241+
"small_int",
242+
"tex_t",
243+
"tim_e",
244+
"timestam_p",
245+
"timeuui_d",
246+
"tiny_int",
247+
"var_char",
248+
"var_int",
249+
},
250+
PartKey: []string{"id"},
251+
SortKey: []string{},
252+
})
253+
254+
// Insert song using query builder.
255+
insertCheckTypes := qb.Insert("examples.check_types").
256+
Columns("asci_i", "big_int", "blo_b", "boolea_n", "dat_e", "decima_l", "doubl_e", "duratio_n", "floa_t", "ine_t", "in_t", "small_int", "tex_t", "tim_e", "timestam_p", "timeuui_d", "tiny_int", "id", "var_char", "var_int", "map_int_text", "list_int", "set_int").Query(session)
257+
258+
var byteId [16]byte
259+
id := []byte("756716f7-2e54-4715-9f00-91dcbea6cf50")
260+
copy(byteId[:], id)
261+
262+
date := time.Date(2021, time.December, 11, 10, 23, 0, 0, time.UTC)
263+
var double float64 = 1.2
264+
var float float32 = 1.3
265+
var integer int32 = 123
266+
listInt := []int32{1, 2, 3}
267+
mapIntStr := map[int32]string{
268+
1: "a",
269+
2: "b",
270+
}
271+
setInt := []int32{2, 4, 6}
272+
var smallInt int16 = 12
273+
var tinyInt int8 = 14
274+
var varInt int64 = 20
275+
276+
insertCheckTypes.BindStruct(CheckTypesStruct{
277+
AsciI: "test qscci",
278+
BigInt: 9223372036854775806, //MAXINT64 - 1,
279+
BloB: []byte("this is blob test"),
280+
BooleaN: false,
281+
DatE: date,
282+
DecimaL: *inf.NewDec(1, 1),
283+
DoublE: double,
284+
DuratioN: gocql.Duration{Months: 1, Days: 1, Nanoseconds: 86400},
285+
FloaT: float,
286+
Id: byteId,
287+
InT: integer,
288+
IneT: "127.0.0.1",
289+
ListInt: listInt,
290+
MapIntText: mapIntStr,
291+
SetInt: setInt,
292+
SmallInt: smallInt,
293+
TexT: "text example",
294+
TimE: 86400000000,
295+
TimestamP: date,
296+
TimeuuiD: gocql.TimeUUID(),
297+
TinyInt: tinyInt,
298+
VarChar: "test varchar",
299+
VarInt: varInt,
300+
})
301+
if err := insertCheckTypes.ExecRelease(); err != nil {
302+
t.Fatal("ExecRelease() failed:", err)
303+
}
304+
305+
// Query and displays data.
306+
queryCheckTypes := checkTypesTable.SelectQuery(session)
307+
308+
queryCheckTypes.BindStruct(&CheckTypesStruct{
309+
Id: byteId,
310+
})
311+
312+
var items []*CheckTypesStruct
313+
if err := queryCheckTypes.Select(&items); err != nil {
314+
t.Fatal("Select() failed:", err)
315+
}
316+
317+
for _, i := range items {
318+
t.Logf("%+v", *i)
319+
}
320+
}
321+
157322
// This example shows how to load a single value using "Get" function.
158323
// Get can also work with UDTs and types that implement gocql marshalling functions.
159324
func basicReadScyllaVersion(t *testing.T, session gocqlx.Session) {

0 commit comments

Comments
 (0)