-
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
schemagen cli #201
Conversation
README.md
Outdated
-cluster string | ||
a comma-separated list of host:port tuples (default "127.0.0.1") | ||
-keyspace string | ||
keyspace to inspect (default "system") |
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.
Defaulting to system makes little sense, if not set exit with error.
README.md
Outdated
@@ -103,6 +103,34 @@ t.Log(people) | |||
// stdout: [{Michał Matczuk [michal@scylladb.com]}] | |||
``` | |||
|
|||
### Generating table metadata structs with schemagen |
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.
### Generating table metadata structs with schemagen | |
## Generating table metadata with schemagen |
README.md
Outdated
go get -u "github.com/scylladb/gocqlx/v2/cmd/schemagen" | ||
``` | ||
|
||
Examples: |
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.
Let's use a real example not help.
The following example generates ...
cmd/schemagen/schemagen.go
Outdated
|
||
var ( | ||
//go:embed keyspace.tmpl | ||
rootFs embed.FS |
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.
You can use template string
instead.
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.
Can you elaborate please? Not sure I understood.
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.
Sure, the go embed accepts 3 things
- embed.FS - if you have many files
- string - if you have a single file and want it as a string
- []byte - if you have a single file
cmd/schemagen/schemagen.go
Outdated
createSession()))) | ||
} | ||
|
||
func assert(err error, msg string) { |
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.
Panic here is not great.
I'd encourage you to use log
package instead of prints and use log.Fatalf here.
This function is not very idiomatic and I'd like to see it inlined with log.Fatalf.
cmd/schemagen/schemagen.go
Outdated
} | ||
|
||
func schemagen() { | ||
writeToFile( |
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.
Not very idiomatic, IMO reads better squashed to a single line.
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.
After reading more code open and handle file here (inline writeToFile) and add file as io.Writer to renderTemplate.
cmd/schemagen/schemagen.go
Outdated
|
||
func writeToFile(b []byte) { | ||
outputPath := path.Join(*flagOutput, *flagPkgname+".go") | ||
ensureDir(outputPath) |
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.
Just call MkdirAll here.
cmd/schemagen/schemagen.go
Outdated
return md | ||
} | ||
|
||
func renderTemplate(md *gocql.KeyspaceMetadata) []byte { |
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 would be much better to accept io.Writer here.
cmd/schemagen/schemagen.go
Outdated
return strings.Split(*flagCluster, ",") | ||
} | ||
|
||
func Camelize(s string) string { |
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.
We probably do not want to export it and maybe we want to use a library instead.
cmd/schemagen/schemagen_test.go
Outdated
} | ||
} | ||
|
||
func resultWant(pkgname string) string { |
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.
Add a golden file in testdata dir.
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.
Please find comments inline.
Please note that assertions are not error handling.
You assert that something always holds true, asserting that user input is correct at all times is a bad idea.
Please redo assertions as idiomatic error handling.
@mmatczuk thanks for the comments, addressed most of them.
|
README.md
Outdated
|
||
Example: | ||
|
||
Running the following command for `examples` [schema](https://github.com/Bobochka/gocqlx/blob/412899a9b2910d00f9f723ea304c3abef2263021/example_test.go#L77-L97): |
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.
The link points to your repo, I'm not sure we needed this perm link here, they tend to get obsolete.
.gitignore
Outdated
cmd/schemagen/schemagen | ||
cmd/schemagen/models | ||
cmd/schemagen/asdf |
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.
Do we need that, can we generate to temp dir.
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.
Will just remove it, tests do the clean up
cmd/schemagen/schemagen.go
Outdated
"embed" | ||
"flag" | ||
"fmt" | ||
"github.com/scylladb/gocqlx/v2" |
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.
Please group imports.
cmd/schemagen/schemagen.go
Outdated
err = renderTemplate(f, metadata) | ||
if err != nil { |
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.
err = renderTemplate(f, metadata) | |
if err != nil { | |
if err := renderTemplate(f, metadata); err != nil { |
log.Fatalln("unable to parse models template:", err) | ||
} | ||
|
||
buf := &bytes.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.
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
Overall it looks much better now. |
Can we merge? |
Please @mmatczuk |
Please fix lint errors and squash commits. |
Done |
Thanks! |
No description provided.