fix: RecordToAttrsMap slice pre-allocation and FindAttrByGroupAndKey group traversal#37
Conversation
…group traversal - RecordToAttrsMap: change make([]slog.Attr, r.NumAttrs()) to make([]slog.Attr, 0, r.NumAttrs()) to avoid leading zero-valued elements that produce spurious empty-key entries - FindAttrByGroupAndKey: compare attrs[i].Key against groups[0] instead of key to correctly traverse into named groups
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #37 +/- ##
==========================================
+ Coverage 95.34% 96.15% +0.80%
==========================================
Files 4 4
Lines 258 260 +2
==========================================
+ Hits 246 250 +4
+ Misses 10 9 -1
+ Partials 2 1 -1
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:
|
There was a problem hiding this comment.
Pull request overview
Fixes two small but user-visible correctness issues in slogcommon: map conversion from slog.Record could include spurious empty-key entries, and grouped attribute lookup could fail to traverse into named groups.
Changes:
- Fix
RecordToAttrsMappre-allocation to avoid leading zero-valuedslog.Attrentries. - Fix
FindAttrByGroupAndKeyto match ongroups[0](group name) and only return when a nested match is actually found. - Update/add tests to assert the corrected behavior for both cases.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
attributes.go |
Pre-allocates attrs with length 0 and capacity r.NumAttrs() to prevent empty-key map entries. |
attributes_test.go |
Turns the prior “bug documentation” into regression assertions (Len == 2, no "" key). |
finder.go |
Corrects group traversal comparison (attrs[i].Key == groups[0]) and continues searching if a matching group doesn’t contain the key. |
finder_test.go |
Updates cases to expect successful lookup inside a named group and adds a not-found case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
make([]slog.Attr, 0, r.NumAttrs()) to avoid leading zero-valued
elements that produce spurious empty-key entries
instead of key to correctly traverse into named groups