Skip to content

Commit bd26baf

Browse files
committed
Merge branch 'pr-2565-review'
2 parents 3b9b83b + 90b17b2 commit bd26baf

44 files changed

Lines changed: 205 additions & 238 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ linters:
77
- govet
88
- ineffassign
99
- unconvert
10+
- gocritic
1011

1112
# See: https://golangci-lint.run/usage/formatters/
1213
formatters:

conn.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,8 @@ optionLoop:
808808

809809
var err error
810810
sd, explicitPreparedStatement := c.preparedStatements[sql]
811-
if sd != nil || mode == QueryExecModeCacheStatement || mode == QueryExecModeCacheDescribe || mode == QueryExecModeDescribeExec {
811+
switch {
812+
case sd != nil || mode == QueryExecModeCacheStatement || mode == QueryExecModeCacheDescribe || mode == QueryExecModeDescribeExec:
812813
if sd == nil {
813814
sd, err = c.getStatementDescription(ctx, mode, sql)
814815
if err != nil {
@@ -846,15 +847,15 @@ optionLoop:
846847
} else {
847848
rows.resultReader = c.pgConn.ExecStatement(ctx, sd, c.eqb.ParamValues, c.eqb.ParamFormats, resultFormats)
848849
}
849-
} else if mode == QueryExecModeExec {
850+
case mode == QueryExecModeExec:
850851
err := c.eqb.Build(c.typeMap, nil, args)
851852
if err != nil {
852853
rows.fatal(err)
853854
return rows, rows.err
854855
}
855856

856857
rows.resultReader = c.pgConn.ExecParams(ctx, sql, c.eqb.ParamValues, nil, c.eqb.ParamFormats, c.eqb.ResultFormats)
857-
} else if mode == QueryExecModeSimpleProtocol {
858+
case mode == QueryExecModeSimpleProtocol:
858859
sql, err = c.sanitizeForSimpleQuery(sql, args...)
859860
if err != nil {
860861
rows.fatal(err)
@@ -872,7 +873,7 @@ optionLoop:
872873
}
873874

874875
return rows, nil
875-
} else {
876+
default:
876877
err = fmt.Errorf("unknown QueryExecMode: %v", mode)
877878
rows.fatal(err)
878879
return rows, rows.err

examples/chat/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ func main() {
1919
os.Exit(1)
2020
}
2121

22-
go listen()
22+
go func() {
23+
if err := listen(); err != nil {
24+
fmt.Fprintln(os.Stderr, err)
25+
os.Exit(1)
26+
}
27+
}()
2328

2429
fmt.Println(`Type a message and press enter.
2530
@@ -47,25 +52,22 @@ Type "exit" to quit.`)
4752
}
4853
}
4954

50-
func listen() {
55+
func listen() error {
5156
conn, err := pool.Acquire(context.Background())
5257
if err != nil {
53-
fmt.Fprintln(os.Stderr, "Error acquiring connection:", err)
54-
os.Exit(1)
58+
return fmt.Errorf("acquiring connection: %w", err)
5559
}
5660
defer conn.Release()
5761

5862
_, err = conn.Exec(context.Background(), "listen chat")
5963
if err != nil {
60-
fmt.Fprintln(os.Stderr, "Error listening to chat channel:", err)
61-
os.Exit(1)
64+
return fmt.Errorf("listening to chat channel: %w", err)
6265
}
6366

6467
for {
6568
notification, err := conn.Conn().WaitForNotification(context.Background())
6669
if err != nil {
67-
fmt.Fprintln(os.Stderr, "Error waiting for notification:", err)
68-
os.Exit(1)
70+
return fmt.Errorf("waiting for notification: %w", err)
6971
}
7072

7173
fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload)

mise.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[tools]
22
go = '1.26.3'
3+
"go:github.com/go-critic/go-critic/cmd/gocritic" = "latest"
34
"go:github.com/gordonklaus/ineffassign" = "latest"
45
"go:github.com/mdempsky/unconvert" = "latest"
56
"go:golang.org/x/tools/cmd/goimports" = "latest"

pgconn/auth_scram.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,12 @@ func (sc *scramClient) clientFirstMessage() []byte {
229229

230230
sc.clientFirstMessageBare = fmt.Appendf(nil, "n=,r=%s", sc.clientNonce)
231231

232-
if sc.authMechanism == scramSHA256PlusName {
232+
switch {
233+
case sc.authMechanism == scramSHA256PlusName:
233234
sc.clientGS2Header = []byte("p=tls-server-end-point,,")
234-
} else if sc.hasTLS {
235+
case sc.hasTLS:
235236
sc.clientGS2Header = []byte("y,,")
236-
} else {
237+
default:
237238
sc.clientGS2Header = []byte("n,,")
238239
}
239240

pgconn/config.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/pem"
88
"errors"
99
"fmt"
10-
"io"
1110
"maps"
1211
"math"
1312
"net"
@@ -308,9 +307,7 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con
308307
User: settings["user"],
309308
Password: settings["password"],
310309
RuntimeParams: make(map[string]string),
311-
BuildFrontend: func(r io.Reader, w io.Writer) *pgproto3.Frontend {
312-
return pgproto3.NewFrontend(r, w)
313-
},
310+
BuildFrontend: pgproto3.NewFrontend,
314311
BuildContextWatcherHandler: func(pgConn *PgConn) ctxwatch.Handler {
315312
return &DeadlineContextWatcherHandler{Conn: pgConn.conn}
316313
},
@@ -643,8 +640,9 @@ func parseKeywordValueSettings(s string) (map[string]string, error) {
643640

644641
key = strings.Trim(s[:eqIdx], " \t\n\r\v\f")
645642
s = strings.TrimLeft(s[eqIdx+1:], " \t\n\r\v\f")
646-
if len(s) == 0 {
647-
} else if s[0] != '\'' {
643+
switch {
644+
case len(s) == 0:
645+
case s[0] != '\'':
648646
end := 0
649647
for ; end < len(s); end++ {
650648
if asciiSpace[s[end]] == 1 {
@@ -657,11 +655,11 @@ func parseKeywordValueSettings(s string) (map[string]string, error) {
657655
}
658656
}
659657
}
660-
val = strings.Replace(strings.Replace(s[:end], "\\\\", "\\", -1), "\\'", "'", -1)
658+
val = strings.ReplaceAll(strings.ReplaceAll(s[:end], "\\\\", "\\"), "\\'", "'")
661659
// Consume the value and trim any subsequent whitespace so that
662660
// multiple trailing spaces don't cause a spurious parse failure.
663661
s = strings.TrimLeft(s[end:], " \t\n\r\v\f")
664-
} else { // quoted string
662+
default: // quoted string
665663
s = s[1:]
666664
end := 0
667665
for ; end < len(s); end++ {
@@ -675,7 +673,7 @@ func parseKeywordValueSettings(s string) (map[string]string, error) {
675673
if end == len(s) {
676674
return nil, errors.New("unterminated quoted string in connection info string")
677675
}
678-
val = strings.Replace(strings.Replace(s[:end], "\\\\", "\\", -1), "\\'", "'", -1)
676+
val = strings.ReplaceAll(strings.ReplaceAll(s[:end], "\\\\", "\\"), "\\'", "'")
679677
// Consume the closing quote and any subsequent whitespace.
680678
s = strings.TrimLeft(s[end+1:], " \t\n\r\v\f")
681679
}

pgconn/errors.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,13 @@ func (e *ParseConfigError) Unwrap() error {
151151
func normalizeTimeoutError(ctx context.Context, err error) error {
152152
var netErr net.Error
153153
if errors.As(err, &netErr) && netErr.Timeout() {
154-
if ctx.Err() == context.Canceled {
154+
switch ctx.Err() {
155+
case context.Canceled:
155156
// Since the timeout was caused by a context cancellation, the actual error is context.Canceled not the timeout error.
156157
return context.Canceled
157-
} else if ctx.Err() == context.DeadlineExceeded {
158+
case context.DeadlineExceeded:
158159
return &errTimeout{err: ctx.Err()}
159-
} else {
160+
default:
160161
return &errTimeout{err: err}
161162
}
162163
}

pgconn/pgconn.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,7 @@ func (pgConn *PgConn) WaitForNotification(ctx context.Context) error {
11251125
return normalizeTimeoutError(ctx, err)
11261126
}
11271127

1128-
switch msg.(type) {
1129-
case *pgproto3.NotificationResponse:
1128+
if _, ok := msg.(*pgproto3.NotificationResponse); ok {
11301129
return nil
11311130
}
11321131
}
@@ -1711,8 +1710,7 @@ func (rr *ResultReader) NextRow() bool {
17111710
return false
17121711
}
17131712

1714-
switch msg := msg.(type) {
1715-
case *pgproto3.DataRow:
1713+
if msg, ok := msg.(*pgproto3.DataRow); ok {
17161714
rr.rowValues = msg.Values
17171715
return true
17181716
}
@@ -2009,7 +2007,7 @@ func (pgConn *PgConn) EscapeString(s string) (string, error) {
20092007
return "", errors.New("EscapeString must be run with client_encoding=UTF8")
20102008
}
20112009

2012-
return strings.Replace(s, "'", "''", -1), nil
2010+
return strings.ReplaceAll(s, "'", "''"), nil
20132011
}
20142012

20152013
// CheckConn checks the underlying connection without writing any bytes. This is currently implemented by doing a read

pgconn/pgconn_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"log"
1211
"math"
1312
"net"
1413
"os"
@@ -4141,13 +4140,15 @@ func Example() {
41414140

41424141
pgConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
41434142
if err != nil {
4144-
log.Fatalln(err)
4143+
fmt.Fprintln(os.Stderr, err)
4144+
return
41454145
}
41464146
defer pgConn.Close(ctx)
41474147

41484148
result := pgConn.ExecParams(ctx, "select generate_series(1,3)", nil, nil, nil, nil).Read()
41494149
if result.Err != nil {
4150-
log.Fatalln(result.Err)
4150+
fmt.Fprintln(os.Stderr, result.Err)
4151+
return
41514152
}
41524153

41534154
for _, row := range result.Rows {

pgproto3/function_call.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ func (dst *FunctionCall) Decode(src []byte) error {
6666
// As a special case, -1 indicates a NULL argument value. No value bytes follow in the NULL case.
6767
argumentLength := int(int32(binary.BigEndian.Uint32(src[rp:])))
6868
rp += 4
69-
if argumentLength == -1 {
69+
switch {
70+
case argumentLength == -1:
7071
arguments[i] = nil
71-
} else if argumentLength < 0 {
72+
case argumentLength < 0:
7273
return &invalidMessageFormatErr{messageType: "FunctionCall"}
73-
} else {
74+
default:
7475
if len(src[rp:]) < argumentLength {
7576
return &invalidMessageFormatErr{messageType: "FunctionCall"}
7677
}

0 commit comments

Comments
 (0)