dsn: default Net to tcp in NewConfig so Addr-only configs round-trip#1770
Conversation
Closes go-sql-driver#1616. NewConfig() left Net empty, so building a Config in code with just an Addr and calling FormatDSN produced '/' (no tcp prefix, no address) instead of 'tcp(my-address)/'. Default Net to 'tcp' to match the docs, and skip the bare protocol in FormatDSN when there's no Addr so a fresh NewConfig().FormatDSN() doesn't grow a useless 'tcp' prefix. Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughFormatDSN now omits the (address) suffix when Addr is empty; it defaults Net to "tcp" when Net is empty and emits net(cfg.Addr) accordingly. New tests validate behavior for Net-only and Addr-only configurations. ChangesDSN formatting
🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR updates DSN formatting defaults so code-built configs with only Addr set include the default TCP network, addressing issue #1616.
Changes:
- Sets
NewConfig().Netto"tcp". - Changes
FormatDSNto avoid emitting a bare protocol when no address is set.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if len(cfg.Net) > 0 && len(cfg.Addr) > 0 { | ||
| buf.WriteString(cfg.Net) | ||
| if len(cfg.Addr) > 0 { | ||
| buf.WriteByte('(') | ||
| buf.WriteString(cfg.Addr) | ||
| buf.WriteByte(')') | ||
| } | ||
| buf.WriteByte('(') | ||
| buf.WriteString(cfg.Addr) | ||
| buf.WriteByte(')') |
| if len(cfg.Net) > 0 && len(cfg.Addr) > 0 { | ||
| buf.WriteString(cfg.Net) |
There was a problem hiding this comment.
| if len(cfg.Net) > 0 && len(cfg.Addr) > 0 { | |
| buf.WriteString(cfg.Net) | |
| if len(cfg.Addr) > 0 { | |
| net := cfg.Net | |
| if net == "" { | |
| net = "tcp" | |
| } | |
| buf.WriteString(net) |
There was a problem hiding this comment.
Good call, applied. I also dropped the NewConfig change since defaulting inline here covers Config{} literals too.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@dsn.go`:
- Around line 267-275: The current emission block only runs when cfg.Addr is
non-empty, dropping an explicit cfg.Net when Addr is empty; update the block so
it runs when either cfg.Net != "" or cfg.Addr != "" (e.g., if cfg.Net != "" ||
len(cfg.Addr) > 0), compute net := cfg.Net and default to "tcp" only if net ==
"" as before, then write buf.WriteString(net) and only add the '(' + cfg.Addr +
')' wrapper when cfg.Addr is non-empty (so preserve Net-only configs like
Net:"unix" while keeping existing behavior when Addr is present).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
b0823bb to
02c50c9
Compare
|
Good catch @coderabbitai. Restored the explicit-Net-without-Addr emission so non-default protocols like |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 1925
The 🐇 ✨ |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
dsn_test.go (1)
333-356: ⚡ Quick winConsider adding a test for the primary Addr-only scenario.
This test verifies
FormatDSNbehavior whenAddris empty, but the main issue from#1616was that setting onlyAddr(non-empty) produced an incorrect DSN. Consider adding a test case to directly verify that scenario:func TestFormatDSN_AddrOnly(t *testing.T) { cfg := NewConfig() cfg.Addr = "myhost:3306" got := cfg.FormatDSN() want := "tcp(myhost:3306)/" if got != want { t.Errorf("FormatDSN() with Addr-only = %q, want %q", got, want) } }This would explicitly verify that the documented default
Net="tcp"is preserved when formatting a config with onlyAddrset.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@dsn_test.go` around lines 333 - 356, Add a new unit test that covers the "Addr-only" case so FormatDSN preserves the default Net when only Addr is set: create a test (e.g., TestFormatDSN_AddrOnly) that calls NewConfig(), sets cfg.Addr = "myhost:3306" (leaving cfg.Net unset), invokes cfg.FormatDSN(), and asserts the returned string equals "tcp(myhost:3306)/"; reference FormatDSN and NewConfig to locate the implementation and add the test alongside TestFormatDSN_NetWithoutAddr.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@dsn_test.go`:
- Around line 333-356: Add a new unit test that covers the "Addr-only" case so
FormatDSN preserves the default Net when only Addr is set: create a test (e.g.,
TestFormatDSN_AddrOnly) that calls NewConfig(), sets cfg.Addr = "myhost:3306"
(leaving cfg.Net unset), invokes cfg.FormatDSN(), and asserts the returned
string equals "tcp(myhost:3306)/"; reference FormatDSN and NewConfig to locate
the implementation and add the test alongside TestFormatDSN_NetWithoutAddr.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 5e5e55ce-a2c2-4071-9140-a95a1b7a6363
📒 Files selected for processing (2)
dsn.godsn_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
- dsn.go
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Closes #1616.
NewConfig()leftNetempty, so building a Config in code with just anAddrand callingFormatDSNproduced/instead oftcp(my-address)/. DefaultNettotcp(matching the docs) and skip the bare protocol inFormatDSNwhen there's noAddr, so a freshNewConfig().FormatDSN()doesn't grow a uselesstcpprefix.