Skip to content

Commit

Permalink
cmd/schemagen: refactoring
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mmatczuk committed Nov 17, 2021
1 parent 39bf42f commit 8477485
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 282 deletions.
73 changes: 37 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,43 @@ package models

import "github.com/scylladb/gocqlx/v2/table"

var PlaylistsMetadata = table.Metadata{
Name: "playlists",
Columns: []string{
"album",
"artist",
"id",
"song_id",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{
"title",
"album",
"artist",
},
}
var PlaylistsTable = table.New(PlaylistsMetadata)

var SongsMetadata = table.Metadata{
Name: "songs",
Columns: []string{
"album",
"artist",
"data",
"id",
"tags",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{},
}
var SongsTable = table.New(SongsMetadata)
// Table models.
var (
Playlists = table.New(table.Metadata{
Name: "playlists",
Columns: []string{
"album",
"artist",
"id",
"song_id",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{
"title",
"album",
"artist",
},
})

Songs = table.New(table.Metadata{
Name: "songs",
Columns: []string{
"album",
"artist",
"data",
"id",
"tags",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{},
})
)
```

## Examples
Expand Down
43 changes: 43 additions & 0 deletions cmd/schemagen/camelize.go
Original file line number Diff line number Diff line change
@@ -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')
}
31 changes: 31 additions & 0 deletions cmd/schemagen/camelize_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
48 changes: 25 additions & 23 deletions cmd/schemagen/keyspace.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ package {{.PackageName}}

import "github.com/scylladb/gocqlx/v2/table"

// Table models.
var (
{{with .Tables}}
{{range .}}
{{$model_name := .Name | camelize}}
var {{$model_name}}Metadata = table.Metadata {
Name: "{{.Name}}",
Columns: []string{
{{- range .OrderedColumns}}
"{{.}}",
{{- end}}
},
PartKey: []string {
{{- range .PartitionKey}}
"{{.Name}}",
{{- end}}
},
SortKey: []string{
{{- range .ClusteringColumns}}
"{{.Name}}",
{{- end}}
},
}
var {{$model_name}}Table = table.New({{$model_name}}Metadata)
{{end}}
{{end}}
{{range .}}
{{$model_name := .Name | camelize}}
{{$model_name}} = table.New(table.Metadata {
Name: "{{.Name}}",
Columns: []string{
{{- range .OrderedColumns}}
"{{.}}",
{{- end}}
},
PartKey: []string {
{{- range .PartitionKey}}
"{{.Name}}",
{{- end}}
},
SortKey: []string{
{{- range .ClusteringColumns}}
"{{.Name}}",
{{- end}}
},
})
{{end}}
{{end}}
)
110 changes: 24 additions & 86 deletions cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"fmt"
"go/format"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
"unicode"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v2"
Expand Down Expand Up @@ -42,44 +41,34 @@ func main() {
log.Fatalln("missing required flag: keyspace")
}

schemagen()
if err := schemagen(); err != nil {
log.Fatalf("failed to generate schema: %s", err)
}
}

func schemagen() {
err := os.MkdirAll(*flagOutput, os.ModePerm)
if err != nil {
log.Fatalln("unable to create output directory:", err)
func schemagen() error {
if err := os.MkdirAll(*flagOutput, os.ModePerm); err != nil {
return fmt.Errorf("create output directory: %w", err)
}

outputPath := path.Join(*flagOutput, *flagPkgname+".go")
f, err := os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
session, err := createSession()
if err != nil {
log.Fatalln("unable to open output file:", err)
return fmt.Errorf("open output file: %w", err)
}

metadata := fetchMetadata(createSession())

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)
metadata, err := session.KeyspaceMetadata(*flagKeyspace)
if err != nil {
return fmt.Errorf("fetch keyspace metadata: %w", err)
}

log.Println("File written to", outputPath)
}

func fetchMetadata(s *gocqlx.Session) *gocql.KeyspaceMetadata {
md, err := s.KeyspaceMetadata(*flagKeyspace)
b, err := renderTemplate(metadata)
if err != nil {
log.Fatalln("unable to fetch keyspace metadata:", err)
return fmt.Errorf("render template: %w", err)
}
outputPath := path.Join(*flagOutput, *flagPkgname+".go")

return md
return ioutil.WriteFile(outputPath, b, os.ModePerm)
}

func renderTemplate(w io.Writer, md *gocql.KeyspaceMetadata) error {
func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
t, err := template.
New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}).
Expand All @@ -95,68 +84,17 @@ func renderTemplate(w io.Writer, md *gocql.KeyspaceMetadata) error {
"Tables": md.Tables,
}

err = t.Execute(buf, data)
if err != nil {
log.Fatalln("unable to execute models template:", err)
}

res, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalln("template output is not a valid go code:", err)
if err = t.Execute(buf, data); err != nil {
return nil, fmt.Errorf("template: %w", err)
}

_, err = w.Write(res)

return err
}

func createSession() *gocqlx.Session {
cluster := createCluster()
s, err := gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
log.Fatalln("unable to create scylla session:", err)
}
return &s
return format.Source(buf.Bytes())
}

func createCluster() *gocql.ClusterConfig {
clusterHosts := getClusterHosts()
return gocql.NewCluster(clusterHosts...)
func createSession() (gocqlx.Session, error) {
cluster := gocql.NewCluster(clusterHosts()...)
return gocqlx.WrapSession(cluster.CreateSession())
}

func getClusterHosts() []string {
func clusterHosts() []string {
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')
}
Loading

0 comments on commit 8477485

Please sign in to comment.