🧹 chore: Improve context helpers#4093
🧹 chore: Improve context helpers#4093ReneWerner87 merged 4 commits intoupdate-middleware-helpers-for-context-typesfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (3)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## update-middleware-helpers-for-context-types #4093 +/- ##
===============================================================================
- Coverage 91.07% 90.99% -0.08%
===============================================================================
Files 119 119
Lines 11335 11333 -2
===============================================================================
- Hits 10323 10313 -10
- Misses 642 648 +6
- Partials 370 372 +2
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.
Code Review
This pull request introduces a new helper function fiber.StoreInContext to simplify storing request-scoped values in both c.Locals() and the request's context.Context. This is a great addition for consistency and reducing code duplication. The existing middleware (basicauth, csrf, keyauth, requestid, session) has been refactored to use this new helper, which makes the code cleaner and more maintainable. The changes also include updates to documentation and a new test for the helper function, which are well-written. The small adjustment in ValueFromContext to use Locals instead of Value for fiber.Ctx is also a good idiomatic improvement. Overall, this is a solid pull request with no issues found.
There was a problem hiding this comment.
Pull request overview
This PR introduces a new StoreInContext helper function that consolidates the pattern of storing values in both c.Locals() and c.Context() simultaneously. This refactoring improves code maintainability by eliminating duplication across multiple middlewares while ensuring request-scoped values remain accessible via both APIs.
Changes:
- Added
fiber.StoreInContexthelper function to store values in both Fiber locals and the request context - Modified
ValueFromContextto useLocals()instead ofValue()for fiber.Ctx types (functionally equivalent, as both usefasthttp.UserValueinternally) - Refactored five middlewares (session, requestid, keyauth, csrf, basicauth) to use the new helper
- Added comprehensive test coverage for
StoreInContext - Updated documentation in
whats_new.mdandcontext.md
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| helpers.go | Added StoreInContext helper and updated ValueFromContext to use Locals() for fiber.Ctx |
| helpers_test.go | Added test coverage for StoreInContext verifying values are stored in both locals and context |
| middleware/session/middleware.go | Refactored to use fiber.StoreInContext, removed context import |
| middleware/requestid/requestid.go | Refactored to use fiber.StoreInContext, removed context import |
| middleware/keyauth/keyauth.go | Refactored to use fiber.StoreInContext, removed context import |
| middleware/csrf/csrf.go | Refactored to use fiber.StoreInContext for both token and handler storage, removed context import |
| middleware/basicauth/basicauth.go | Refactored to use fiber.StoreInContext, removed context import |
| docs/whats_new.md | Documented the new StoreInContext helper function |
| docs/guide/context.md | Added guidance on using StoreInContext for request-scoped values |
05d6ba8
into
update-middleware-helpers-for-context-types
This pull request introduces a new StoreInContext helper function designed to streamline how request-scoped values are stored and retrieved within Fiber applications. This helper ensures that values are consistently accessible via both c.Locals() and the underlying context.Context, simplifying middleware development and promoting a more unified approach to context management across the framework. Existing middlewares have been refactored to adopt this new utility.