Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change WithValue key type to any #103

Merged
merged 5 commits into from
Sep 13, 2024

Conversation

mdanialr
Copy link
Contributor

@mdanialr mdanialr commented Sep 12, 2024

📑 Description

Implementation of #630

Summary by CodeRabbit

  • New Features

    • Enhanced context handling allows for greater flexibility with key types.
    • Improved usability of context methods for storing and retrieving values.
    • Updated HTTP route handlers to access context values without type assertions, increasing robustness.
  • Bug Fixes

    • Updated dependency to a more recent version, potentially addressing existing issues.
  • Tests

    • Expanded test coverage for context functionality, verifying the handling of various key types.

✅ Checks

  • Added test cases for my code

Copy link

coderabbitai bot commented Sep 12, 2024

Walkthrough

The pull request introduces changes to the Context struct in context.go, simplifying context value handling by removing the ctxKey type and allowing any type for keys in the WithValue method. The Value method has been updated to return values associated with any key type directly. Additionally, the context_test.go file has been enhanced to include type-safe key testing, and the go.mod file reflects an update to a newer version of the github.com/goravel/framework dependency.

Changes

File Change Summary
context.go Removed ctxKey type; changed WithValue method to accept key any; streamlined Value method.
context_test.go Enhanced TestContext to include various key types; added custom key type for type-safe testing.
group_test.go Updated TestGroup to remove type assertions for context values, improving flexibility in value retrieval.
go.mod Updated github.com/goravel/framework version from v1.14.5 to v1.14.6-0.20240912145232-6543aeadf1bc.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 26ad091 and b764dfe.

Files selected for processing (1)
  • group_test.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • group_test.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mdanialr
Copy link
Contributor Author

@coderabbitai review

Copy link

coderabbitai bot commented Sep 12, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1c76e56 and 26ad091.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (3)
  • context.go (2 hunks)
  • context_test.go (1 hunks)
  • go.mod (1 hunks)
Additional comments not posted (7)
context_test.go (5)

10-11: LGTM!

The introduction of the customKeyType and the customKey variable is a good approach to ensure type-safe keys in the context. The empty struct pattern is commonly used for defining unique key types.


16-18: LGTM!

The addition of key-value pairs using various key types, including the custom key type, integer, and float, enhances the test coverage for the context functionality. The WithValue method is correctly used to store values in the context.


20-24: LGTM!

The updated assertions, which check values directly from httpCtx using the Value method, ensure that the values stored in the context can be correctly retrieved. The assertions cover a wide range of key types, improving the test coverage and enhancing the robustness of the context handling.


26-26: LGTM!

Obtaining the underlying context from httpCtx using the Context method is a common pattern and allows for accessing the underlying context correctly.


27-31: LGTM!

The updated assertions, which check values directly from the underlying context using the Value method, ensure that the values stored in the context can be correctly retrieved from the underlying context. The assertions cover a wide range of key types, improving the test coverage and enhancing the robustness of the context handling.

context.go (1)

40-41: LGTM! Verify the function usage.

The changes to the WithValue function look good. The acceptance of key as any allows for greater flexibility, and the removal of the ctxKey type simplifies the code.

However, ensure that all calls to WithValue have been updated to match the new signature and that the behavior remains as expected.

Run the following script to verify the function usage:

Verification successful

Function usage verified successfully.

The WithValue function is being used with various key types across the codebase, confirming that the new signature with any as the key type is correctly implemented. The changes are reflected in both test and non-test files, ensuring comprehensive coverage.

  • Instances found in:
    • context_test.go
    • context_request.go
    • group_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `WithValue` match the new signature.

# Test: Search for the function usage. Expect: No occurrences of the old signature.
rg --type go -A 5 $'WithValue\(.*string'

Length of output: 41


Script:

#!/bin/bash
# Description: Verify all function calls to `WithValue` use the new signature with `any` as the key type.

# Search for all instances of `WithValue` to ensure they align with the new signature.
rg --type go 'WithValue\(' -A 5

Length of output: 3277

go.mod (1)

9-9: Verify compatibility and test thoroughly.

The update to the github.com/goravel/framework dependency version looks good. However, since it's a development version, it's crucial to:

  1. Review the changes introduced in the updated version and ensure compatibility with the rest of the codebase.
  2. Perform thorough testing to identify any potential issues or unexpected behavior introduced by the updated dependency.

context.go Show resolved Hide resolved
@mdanialr
Copy link
Contributor Author

mdanialr commented Sep 12, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Sep 12, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (v1.2.x@1c76e56). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff            @@
##             v1.2.x     #103   +/-   ##
=========================================
  Coverage          ?   79.05%           
=========================================
  Files             ?       12           
  Lines             ?      850           
  Branches          ?        0           
=========================================
  Hits              ?      672           
  Misses            ?      150           
  Partials          ?       28           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect

context_test.go Show resolved Hide resolved
@hwbrzzl hwbrzzl merged commit 636f637 into goravel:v1.2.x Sep 13, 2024
7 checks passed
@mdanialr mdanialr deleted the ref/change-withvalue-key-type-to-any branch September 13, 2024 04:44
hwbrzzl added a commit that referenced this pull request Sep 21, 2024
* feat: add Shutdown to support graceful quit of HTTP Server (#98)

* add Shutdown to support graceful quit of HTTP Server

* improve unit testing

---------

Co-authored-by: 朱祥东 <[email protected]>

* feat: Optmize Shutdown (#99)

* refactor: change `WithValue` key type to any (#103)

* bump ver goravel framework

* ref: use any as the key type for WithValue

* test: more test cases for WithValue and Context

* test: swap the expected and actual value in assertion

* test: remove type assert

* upgrade: v1.2.4 (#104)

* update workflows

---------

Co-authored-by: zxdstyle <[email protected]>
Co-authored-by: 朱祥东 <[email protected]>
Co-authored-by: Danial <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants