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

Feature/closure param support #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

khchehab
Copy link

@khchehab khchehab commented Jun 27, 2024

Hi, this is my first contribution and I hope I did this right, please let me know if I missed anything.

When closing a connection/statement/row set I use defer blocks with parameters of what I'm closing. This was not being checked and using golangci-lint with this linter reported these errors and that the row set was not being closed.

I added this check so that it takes into consideration if a close was done inside a defer block closure that takes in a parameter as well.

I added test cases for successes and failures as well.

I also updated the packages since there was an issue when building it without any changes, due to a panic from one of the indirect dependencies.

log.Fatal(err)
}

defer func(rows pgx.Rows) {

Choose a reason for hiding this comment

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

Do you plan to support the t.Cleanup way, I mean, either the detection, or adding them to correct cases ?

I didn't look at all code (past, or added) but I'm wondering.

So I prefer to raise the point, and get a "already supported, RTFM" 😁😅

Of course, it can be addressed in another PR

var username string
err = stmt.QueryRowContext(ctx, id).Scan(&username)
switch {
case err == sql.ErrNoRows:

Choose a reason for hiding this comment

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

Non-blocking side remark.

I know you are using the direct sql lib method, but I would have used errors.Is

Suggested change
case err == sql.ErrNoRows:
case errors.Is(err, sql.ErrNoRows):

My suggestion is related to all the codebase

defer func(rows pgx.Rows) {
rows.Close()
}(rows)
}

Choose a reason for hiding this comment

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

Based on the copy paste you do with small variation, maybe these function could be generated.

But I would understand if you think it overkill

@@ -2,3 +2,5 @@
testdata/pgx_examples/missing_close.go:8:26: Rows/Stmt/NamedStmt was not closed
testdata/pgx_examples/missing_close.go:17:28: Rows/Stmt/NamedStmt was not closed
testdata/pgx_examples/missing_close.go:26:28: Rows/Stmt/NamedStmt was not closed
testdata/pgx_examples/missing_close_defer.go:10:26: Rows/Stmt/NamedStmt was not closed
testdata/pgx_examples/missing_close_defer.go:23:26: Rows/Stmt/NamedStmt was not closed

Choose a reason for hiding this comment

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

I'm surprised to see such a file

Why not using the / want Go idiomatic comment in the code?

@khchehab
Copy link
Author

Hi @ccoVeille, thank you for your feedback. I checked your review points and and I agree with them but I did not change anything that was existing since I don't know if the author of the repo intended to use certain approaches or not. I only added the feature that I needed to support.

Do you think we should change it?

@ccoVeille
Copy link

I agree with you. As I said I didn't look at the code base.

@ccoVeille
Copy link

ccoVeille commented Jun 28, 2024

Let's wait for @ryanrolds to look at them. But I'm fine with your changes @khchehab, no need to address my comments before merging

This one is the one that maybe addressed in another issue if Ryan finds it useful

#37 (comment)

@ryanrolds
Copy link
Owner

ryanrolds commented Jul 29, 2024

Thank you for your patience, I've been busy isn't the case anymore. I will work reviewing this into my todos this week.

Thank you.

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.

3 participants