-
Notifications
You must be signed in to change notification settings - Fork 121
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
schemagen cli #201
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
vendor | ||
# GoLand IDE | ||
.idea/ | ||
|
||
cmd/schemagen/schemagen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
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') | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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