♻️ refactor: use maps.Copy to simplify code#3490
Conversation
WalkthroughThe changes update several locations in the codebase to use the Go standard library's Changes
Suggested labels
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (3)
⏰ Context from checks skipped due to timeout of 90000ms (6)
🔇 Additional comments (7)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Pull Request Overview
This PR refactors manual map-merging loops to use the new Go 1.21 maps.Copy function for conciseness and readability.
Key changes:
- Replaced custom tag loop in the logger middleware with
maps.Copy. - Simplified cookie and path‐param setters by swapping loops for
maps.Copy. - Updated binder mapping logic to use
maps.Copy.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| middleware/logger/tags.go | Replaced manual merge of cfg.CustomTags with maps.Copy |
| client/request.go | Swapped loops in SetCookies/SetParams for maps.Copy |
| binder/mapping.go | Replaced loop populating newMap with maps.Copy |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3490 +/- ##
==========================================
- Coverage 83.75% 83.74% -0.01%
==========================================
Files 120 120
Lines 12246 12239 -7
==========================================
- Hits 10257 10250 -7
Misses 1564 1564
Partials 425 425
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@alexandear can you provide benchmark results |
var pathParam = PathParam{}
func BenchmarkSetParams(b *testing.B) {
params := map[string]string{
"a": "1",
"b": "2",
"c": "3",
}
b.Run("for-loop", func(b *testing.B) {
for b.Loop() {
for k, v := range params {
pathParam[k] = v
}
}
})
b.Run("maps-copy", func(b *testing.B) {
for b.Loop() {
maps.Copy(pathParam, params)
}
})
}$ go test -benchmem -run=^$ -bench ^BenchmarkSetParams$ github.com/gofiber/fiber/v3/client
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3/client
cpu: Apple M4 Pro
BenchmarkSetParams/for-loop-12 20934620 57.04 ns/op 0 B/op 0 allocs/op
BenchmarkSetParams/maps-copy-12 20611929 59.74 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/gofiber/fiber/v3/client 2.675s |
|
ok thx |
|
the 2ns is coming from function overhead |
Description
There is a new function added in the Go 1.21 standard library, which can make the code more concise and easy to read.
Changes introduced
forloops withmaps.Copy.Type of change
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.