cmd/schemagen: refactoring

- Replace log.Faltal with error wrapping in schemagen func
- Simplify tests, use temp dir and ioutil functions, remove boilerplate code
- In test use schemagen keyspace to avoid name conflict with examples
- Change template
This commit is contained in:
Michał Matczuk
2021-11-17 11:49:42 +01:00
committed by Michal Jan Matczuk
parent 39bf42f122
commit 8477485a45
7 changed files with 239 additions and 288 deletions

View File

@@ -141,7 +141,9 @@ package models
import "github.com/scylladb/gocqlx/v2/table" import "github.com/scylladb/gocqlx/v2/table"
var PlaylistsMetadata = table.Metadata{ // Table models.
var (
Playlists = table.New(table.Metadata{
Name: "playlists", Name: "playlists",
Columns: []string{ Columns: []string{
"album", "album",
@@ -158,10 +160,9 @@ var PlaylistsMetadata = table.Metadata{
"album", "album",
"artist", "artist",
}, },
} })
var PlaylistsTable = table.New(PlaylistsMetadata)
var SongsMetadata = table.Metadata{ Songs = table.New(table.Metadata{
Name: "songs", Name: "songs",
Columns: []string{ Columns: []string{
"album", "album",
@@ -175,8 +176,8 @@ var SongsMetadata = table.Metadata{
"id", "id",
}, },
SortKey: []string{}, SortKey: []string{},
} })
var SongsTable = table.New(SongsMetadata) )
``` ```
## Examples ## Examples

43
cmd/schemagen/camelize.go Normal file
View File

@@ -0,0 +1,43 @@
// 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 (
"fmt"
"unicode"
)
func camelize(s string) string {
buf := []byte(s)
out := make([]byte, 0, len(buf))
underscoreSeen := false
l := len(buf)
for i := 0; i < l; i++ {
if !(allowedBindRune(buf[i]) || buf[i] == '_') {
panic(fmt.Sprint("not allowed name ", s))
}
b := rune(buf[i])
if b == '_' {
underscoreSeen = true
continue
}
if (i == 0 || underscoreSeen) && unicode.IsLower(b) {
b = unicode.ToUpper(b)
underscoreSeen = false
}
out = append(out, byte(b))
}
return string(out)
}
func allowedBindRune(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
}

View File

@@ -0,0 +1,31 @@
// 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 TestCamelize(t *testing.T) {
tests := []struct {
input string
want string
}{
{"hello", "Hello"},
{"_hello", "Hello"},
{"__hello", "Hello"},
{"hello_", "Hello"},
{"hello_world", "HelloWorld"},
{"hello__world", "HelloWorld"},
{"_hello_world", "HelloWorld"},
{"helloWorld", "HelloWorld"},
{"HelloWorld", "HelloWorld"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := camelize(tt.input); got != tt.want {
t.Errorf("camelize() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -4,10 +4,12 @@ package {{.PackageName}}
import "github.com/scylladb/gocqlx/v2/table" import "github.com/scylladb/gocqlx/v2/table"
// Table models.
var (
{{with .Tables}} {{with .Tables}}
{{range .}} {{range .}}
{{$model_name := .Name | camelize}} {{$model_name := .Name | camelize}}
var {{$model_name}}Metadata = table.Metadata { {{$model_name}} = table.New(table.Metadata {
Name: "{{.Name}}", Name: "{{.Name}}",
Columns: []string{ Columns: []string{
{{- range .OrderedColumns}} {{- range .OrderedColumns}}
@@ -24,7 +26,7 @@ import "github.com/scylladb/gocqlx/v2/table"
"{{.Name}}", "{{.Name}}",
{{- end}} {{- end}}
}, },
} })
var {{$model_name}}Table = table.New({{$model_name}}Metadata)
{{end}}
{{end}} {{end}}
{{end}}
)

View File

@@ -7,12 +7,11 @@ import (
"fmt" "fmt"
"go/format" "go/format"
"html/template" "html/template"
"io" "io/ioutil"
"log" "log"
"os" "os"
"path" "path"
"strings" "strings"
"unicode"
"github.com/gocql/gocql" "github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v2" "github.com/scylladb/gocqlx/v2"
@@ -42,44 +41,34 @@ func main() {
log.Fatalln("missing required flag: keyspace") log.Fatalln("missing required flag: keyspace")
} }
schemagen() if err := schemagen(); err != nil {
log.Fatalf("failed to generate schema: %s", err)
}
} }
func schemagen() { func schemagen() error {
err := os.MkdirAll(*flagOutput, os.ModePerm) if err := os.MkdirAll(*flagOutput, os.ModePerm); err != nil {
if err != nil { return fmt.Errorf("create output directory: %w", err)
log.Fatalln("unable to create output directory:", err)
} }
session, err := createSession()
if err != nil {
return fmt.Errorf("open output file: %w", err)
}
metadata, err := session.KeyspaceMetadata(*flagKeyspace)
if err != nil {
return fmt.Errorf("fetch keyspace metadata: %w", err)
}
b, err := renderTemplate(metadata)
if err != nil {
return fmt.Errorf("render template: %w", err)
}
outputPath := path.Join(*flagOutput, *flagPkgname+".go") outputPath := path.Join(*flagOutput, *flagPkgname+".go")
f, err := os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
log.Fatalln("unable to open output file:", err)
}
metadata := fetchMetadata(createSession()) return ioutil.WriteFile(outputPath, b, os.ModePerm)
if err = renderTemplate(f, metadata); err != nil {
log.Fatalln("unable to output template:", err)
}
if err = f.Close(); err != nil {
log.Fatalln("unable to close output file:", err)
}
log.Println("File written to", outputPath)
} }
func fetchMetadata(s *gocqlx.Session) *gocql.KeyspaceMetadata { func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
md, err := s.KeyspaceMetadata(*flagKeyspace)
if err != nil {
log.Fatalln("unable to fetch keyspace metadata:", err)
}
return md
}
func renderTemplate(w io.Writer, md *gocql.KeyspaceMetadata) error {
t, err := template. t, err := template.
New("keyspace.tmpl"). New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}). Funcs(template.FuncMap{"camelize": camelize}).
@@ -95,68 +84,17 @@ func renderTemplate(w io.Writer, md *gocql.KeyspaceMetadata) error {
"Tables": md.Tables, "Tables": md.Tables,
} }
err = t.Execute(buf, data) if err = t.Execute(buf, data); err != nil {
if err != nil { return nil, fmt.Errorf("template: %w", err)
log.Fatalln("unable to execute models template:", err)
} }
return format.Source(buf.Bytes())
res, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalln("template output is not a valid go code:", err)
}
_, err = w.Write(res)
return err
} }
func createSession() *gocqlx.Session { func createSession() (gocqlx.Session, error) {
cluster := createCluster() cluster := gocql.NewCluster(clusterHosts()...)
s, err := gocqlx.WrapSession(cluster.CreateSession()) return gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
log.Fatalln("unable to create scylla session:", err)
}
return &s
} }
func createCluster() *gocql.ClusterConfig { func clusterHosts() []string {
clusterHosts := getClusterHosts()
return gocql.NewCluster(clusterHosts...)
}
func getClusterHosts() []string {
return strings.Split(*flagCluster, ",") return strings.Split(*flagCluster, ",")
} }
func camelize(s string) string {
buf := []byte(s)
out := make([]byte, 0, len(buf))
underscoreSeen := false
l := len(buf)
for i := 0; i < l; i++ {
if !(allowedBindRune(buf[i]) || buf[i] == '_') {
panic(fmt.Sprint("not allowed name ", s))
}
b := rune(buf[i])
if b == '_' {
underscoreSeen = true
continue
}
if (i == 0 || underscoreSeen) && unicode.IsLower(b) {
b = unicode.ToUpper(b)
underscoreSeen = false
}
out = append(out, byte(b))
}
return string(out)
}
func allowedBindRune(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
}

View File

@@ -1,84 +1,51 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/scylladb/gocqlx/v2/gocqlxtest" "io/ioutil"
"io"
"os" "os"
"os/exec"
"path"
"strings"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"github.com/scylladb/gocqlx/v2/gocqlxtest"
) )
func TestCamelize(t *testing.T) { var flagUpdate = flag.Bool("update", false, "update golden file")
tests := []struct {
input string
want string
}{
{"hello", "Hello"},
{"_hello", "Hello"},
{"__hello", "Hello"},
{"hello_", "Hello"},
{"hello_world", "HelloWorld"},
{"hello__world", "HelloWorld"},
{"_hello_world", "HelloWorld"},
{"helloWorld", "HelloWorld"},
{"HelloWorld", "HelloWorld"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := camelize(tt.input); got != tt.want {
t.Errorf("camelize() = %v, want %v", got, tt.want)
}
})
}
}
func Test_schemagen_defaultParams(t *testing.T) { func TestSchemagen(t *testing.T) {
cleanup(t, "models") flag.Parse()
defer cleanup(t, "models")
createTestSchema(t) createTestSchema(t)
runSchemagen(t, "", "") b := runSchemagen(t, "foobar")
assertResult(t, "models", "models")
}
func Test_schemagen_customParams(t *testing.T) { const goldenFile = "testdata/models.go.txt"
cleanup(t, "asdf") if *flagUpdate {
defer cleanup(t, "asdf") if err := ioutil.WriteFile(goldenFile, b, os.ModePerm); err != nil {
createTestSchema(t) t.Fatal(err)
runSchemagen(t, "qwer", "asdf") }
assertResult(t, "qwer", "asdf") }
} golden, err := ioutil.ReadFile(goldenFile)
func cleanup(t *testing.T, output string) {
err := os.RemoveAll(output)
if err != nil { if err != nil {
t.Fatalf("could not delete %s directory: %v\n", output, err) t.Fatal(err)
} }
err = os.Remove("./schemagen") if diff := cmp.Diff(string(golden), string(b)); diff != "" {
if err != nil { t.Fatalf(diff)
t.Fatalf("could not delete binary: %v\n", err)
}
cmd := exec.Command("go", "build")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not build binary for schemagen: %v\nOutput:\n%v\n", err, string(out))
} }
} }
func createTestSchema(t *testing.T) { func createTestSchema(t *testing.T) {
t.Helper()
session := gocqlxtest.CreateSession(t) session := gocqlxtest.CreateSession(t)
defer session.Close() defer session.Close()
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`) err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS schemagen WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil { if err != nil {
t.Fatal("create keyspace:", err) t.Fatal("create keyspace:", err)
} }
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.songs ( err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.songs (
id uuid PRIMARY KEY, id uuid PRIMARY KEY,
title text, title text,
album text, album text,
@@ -89,7 +56,7 @@ func createTestSchema(t *testing.T) {
t.Fatal("create table:", err) t.Fatal("create table:", err)
} }
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.playlists ( err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
id uuid, id uuid,
title text, title text,
album text, album text,
@@ -101,59 +68,27 @@ func createTestSchema(t *testing.T) {
} }
} }
func runSchemagen(t *testing.T, pkgname, output string) { func runSchemagen(t *testing.T, pkgname string) []byte {
dir, err := os.Getwd() t.Helper()
dir, err := os.MkdirTemp("", "gocqlx")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
keyspace := "schemagen"
args := []string{"-keyspace=examples"} flagKeyspace = &keyspace
for _, arg := range os.Args { flagPkgname = &pkgname
if strings.HasPrefix(arg, "-cluster") { flagOutput = &dir
args = append(args, arg)
} if err := schemagen(); err != nil {
t.Fatalf("schemagen() error %s", err)
} }
if pkgname != "" { f := fmt.Sprintf("%s/%s.go", dir, pkgname)
args = append(args, fmt.Sprintf("-pkgname=%s", pkgname)) b, err := os.ReadFile(f)
}
if output != "" {
args = append(args, fmt.Sprintf("-output=%s", output))
}
cmd := exec.Command(path.Join(dir, "schemagen"), args...)
err = cmd.Run()
if err != nil { if err != nil {
t.Fatal(err) t.Fatalf("%s: %s", f, err)
} }
} return b
func assertResult(t *testing.T, pkgname, output string) {
path := fmt.Sprintf("%s/%s.go", output, pkgname)
res, err := os.ReadFile(path)
if err != nil {
t.Fatalf("can't read output file (%s): %s\n", path, err)
}
want := resultWant(t, pkgname)
if string(res) != want {
t.Fatalf("unexpected result: %s\nWanted:\n%s\n", string(res), want)
}
}
func resultWant(t *testing.T, pkgname string) string {
f, err := os.Open("testdata/models.go.txt")
if err != nil {
t.Fatalf("can't open testdata/models.go.txt")
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
t.Fatalf("can't read testdata/models.go.txt")
}
return strings.Replace(string(b), "{{pkgname}}", pkgname, 1)
} }

View File

@@ -1,10 +1,12 @@
// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT. // Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.
package {{pkgname}} package foobar
import "github.com/scylladb/gocqlx/v2/table" import "github.com/scylladb/gocqlx/v2/table"
var PlaylistsMetadata = table.Metadata{ // Table models.
var (
Playlists = table.New(table.Metadata{
Name: "playlists", Name: "playlists",
Columns: []string{ Columns: []string{
"album", "album",
@@ -21,10 +23,9 @@ var PlaylistsMetadata = table.Metadata{
"album", "album",
"artist", "artist",
}, },
} })
var PlaylistsTable = table.New(PlaylistsMetadata)
var SongsMetadata = table.Metadata{ Songs = table.New(table.Metadata{
Name: "songs", Name: "songs",
Columns: []string{ Columns: []string{
"album", "album",
@@ -38,5 +39,5 @@ var SongsMetadata = table.Metadata{
"id", "id",
}, },
SortKey: []string{}, SortKey: []string{},
} })
var SongsTable = table.New(SongsMetadata) )