Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemagen cli #201

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
vendor
# GoLand IDE
.idea/

cmd/schemagen/schemagen
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,82 @@ t.Log(people)
// stdout: [{Michał Matczuk [michal@scylladb.com]}]
```

## Generating table metadata with schemagen

Installation

```bash
go get -u "github.com/scylladb/gocqlx/v2/cmd/schemagen"
```

Usage:
```bash
$GOBIN/schemagen [flags]

Flags:
-cluster string
a comma-separated list of host:port tuples (default "127.0.0.1")
-keyspace string
keyspace to inspect (required)
-output string
the name of the folder to output to (default "models")
-pkgname string
the name you wish to assign to your generated package (default "models")
```

Example:

Running the following command for `examples` keyspace:
```bash
$GOBIN/schemagen -cluster="127.0.0.1:9042" -keyspace="examples" -output="models" -pkgname="models"
```

Generates `models/models.go` as follows:
```go
// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.

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)
```

## Examples

You can find lots of examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
Expand Down
30 changes: 30 additions & 0 deletions cmd/schemagen/keyspace.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.

package {{.PackageName}}

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

{{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}}
162 changes: 162 additions & 0 deletions cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"bytes"
_ "embed"
"flag"
"fmt"
"go/format"
"html/template"
"io"
"log"
"os"
"path"
"strings"
"unicode"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v2"
_ "github.com/scylladb/gocqlx/v2/table"
)

var (
cmd = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagCluster = cmd.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
flagKeyspace = cmd.String("keyspace", "", "keyspace to inspect")
flagPkgname = cmd.String("pkgname", "models", "the name you wish to assign to your generated package")
flagOutput = cmd.String("output", "models", "the name of the folder to output to")
)

var (
//go:embed keyspace.tmpl
keyspaceTmpl string
)

func main() {
err := cmd.Parse(os.Args[1:])
if err != nil {
log.Fatalln("can't parse flags")
}

if *flagKeyspace == "" {
log.Fatalln("missing required flag: keyspace")
}

schemagen()
}

func schemagen() {
err := os.MkdirAll(*flagOutput, os.ModePerm)
if err != nil {
log.Fatalln("unable to create output directory:", err)
}

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())

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 {
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.
New("keyspace.tmpl").
Funcs(template.FuncMap{"camelize": camelize}).
Parse(keyspaceTmpl)

if err != nil {
log.Fatalln("unable to parse models template:", err)
}

buf := &bytes.Buffer{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea here is that you do not need the buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for the format.Source below, otherwise will need to re-read the file

data := map[string]interface{}{
"PackageName": *flagPkgname,
"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)
}

_, 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
}

func createCluster() *gocql.ClusterConfig {
clusterHosts := getClusterHosts()
return gocql.NewCluster(clusterHosts...)
}

func getClusterHosts() []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