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

schemagen cli #201

merged 1 commit into from
Nov 17, 2021

Conversation

Bobochka
Copy link
Contributor

No description provided.

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")
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
### 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:
Copy link
Contributor

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 ...


var (
//go:embed keyspace.tmpl
rootFs embed.FS
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

createSession())))
}

func assert(err error, msg string) {
Copy link
Contributor

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.

}

func schemagen() {
writeToFile(
Copy link
Contributor

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.

Copy link
Contributor

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.


func writeToFile(b []byte) {
outputPath := path.Join(*flagOutput, *flagPkgname+".go")
ensureDir(outputPath)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just call MkdirAll here.

return md
}

func renderTemplate(md *gocql.KeyspaceMetadata) []byte {
Copy link
Contributor

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.

return strings.Split(*flagCluster, ",")
}

func Camelize(s string) string {
Copy link
Contributor

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.

}
}

func resultWant(pkgname string) string {
Copy link
Contributor

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.

Copy link
Contributor

@mmatczuk mmatczuk left a 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.

@Bobochka
Copy link
Contributor Author

@mmatczuk thanks for the comments, addressed most of them.

  1. Asked inline regarding embed
  2. Camelize - made unexported, should it be moved to reflectx package maybe?

README.md Outdated

Example:

Running the following command for `examples` [schema](https://github.com/Bobochka/gocqlx/blob/412899a9b2910d00f9f723ea304c3abef2263021/example_test.go#L77-L97):
Copy link
Contributor

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
Comment on lines 18 to 20
cmd/schemagen/schemagen
cmd/schemagen/models
cmd/schemagen/asdf
Copy link
Contributor

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.

Copy link
Contributor Author

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

"embed"
"flag"
"fmt"
"github.com/scylladb/gocqlx/v2"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please group imports.

Comment on lines 62 to 63
err = renderTemplate(f, metadata)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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{}
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

@mmatczuk
Copy link
Contributor

Overall it looks much better now.
I added some follow up inline comments.
Moving Camelize to reflectx makes indeed more sense.
Finish that first and move it with a separate PR.

@mmatczuk
Copy link
Contributor

Can we merge?

@Bobochka
Copy link
Contributor Author

Please @mmatczuk

@mmatczuk
Copy link
Contributor

Please fix lint errors and squash commits.

@Bobochka
Copy link
Contributor Author

Done

@mmatczuk mmatczuk merged commit 1bfe101 into scylladb:master Nov 17, 2021
@mmatczuk
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants