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

🔥 Feature: Enhance CheckConstraint method for improved error handling #3356

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

JIeJaitt
Copy link
Contributor

@JIeJaitt JIeJaitt commented Mar 18, 2025

Description

Functions can now override custom constraint configurations and gofiber's built-in constraint configurations

close #3352

Copy link
Contributor

coderabbitai bot commented Mar 18, 2025

Walkthrough

This pull request refines constraint validation and error handling in the CheckConstraint method, reorders type-switch cases in the request ID extraction function, and expands test coverage for routing constraints. The changes focus on robust error checking, ensuring data integrity during conversions, and verifying route patterns against various constraint conditions.

Changes

File Path Change Summary
path.go Updated the CheckConstraint method: early return for custom constraints, explicit error checks for conversions (int, bool, float, UUID, date), required data validation, simplified control flow.
middleware/requestid/requestid.go Reordered case statements in the type switch within the FromContext function; no change to the core logic or error handling.
path_testcases_test.go Added new benchmark case for route pattern /api/v1/:param<regex(\\d+)> with two test cases: matching /api/v1/123 (true) and non-matching /api/v1/abc (false).

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant CheckConstraint
    Caller->>CheckConstraint: Call CheckConstraint(param)
    CheckConstraint->>CheckConstraint: Check for custom constraints
    alt Custom constraint found
        CheckConstraint-->>Caller: Return result
    else No custom constraint
        CheckConstraint->>CheckConstraint: Perform data conversions (int, bool, float, UUID, date)
        alt Conversion error occurs
            CheckConstraint-->>Caller: Return false
        else Conversions valid
            CheckConstraint->>CheckConstraint: Validate required constraint data and types
            CheckConstraint-->>Caller: Return true
        end
    end
Loading
sequenceDiagram
    participant Caller
    participant FromContext
    Caller->>FromContext: Call FromContext(context)
    FromContext->>FromContext: Check if context is context.Context
    alt context.Context match
        FromContext-->>Caller: Return request ID
    else
        FromContext->>FromContext: Evaluate if context is fiber.Ctx (moved to last)
        alt fiber.Ctx match
            FromContext-->>Caller: Return request ID
        else
            FromContext-->>Caller: Log error and return empty string
        end
    end
Loading

Poem

I'm a rabbit, coding through the night,
Hopping on bugs till the logic is right.
Custom checks and data conversions in sight,
Switches re-ordered with a twitch of delight.
New test cases bloom like carrots in a row,
I celebrate coding with a joyful toe-tap show!
Hop on, fellow coders, let our fixes grow! 🐰✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Free

📥 Commits

Reviewing files that changed from the base of the PR and between 5be0aae and 91f3f5d.

📒 Files selected for processing (2)
  • path.go (3 hunks)
  • path_testcases_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • path_testcases_test.go

Note

🎁 Summarized by CodeRabbit Free

Your organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above.

🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

@ReneWerner87 ReneWerner87 added this to the v3 milestone Mar 18, 2025
@JIeJaitt JIeJaitt marked this pull request as ready for review March 18, 2025 09:45
@ReneWerner87 ReneWerner87 requested a review from efectn March 18, 2025 09:45
@JIeJaitt JIeJaitt removed the request for review from efectn March 18, 2025 09:45
@JIeJaitt JIeJaitt requested a review from a team as a code owner March 18, 2025 09:45
@JIeJaitt JIeJaitt requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team March 18, 2025 09:45
@ReneWerner87
Copy link
Member

ReneWerner87 commented Mar 18, 2025

cool interesting

var num int
// First check if there's a custom constraint with the same name
// This allows custom constraints to override built-in constraints
for _, cc := range c.customConstraints {
Copy link
Member

Choose a reason for hiding this comment

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

Can you update the documentation about the behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, but Where in the docs should I explain this change?

Copy link
Member

Choose a reason for hiding this comment

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

No problem, but Where in the docs should I explain this change?

You can add a note to https://docs.gofiber.io/next/guide/routing#custom-constraint

@ReneWerner87
Copy link
Member

@JIeJaitt pls check the tests
image

Copy link

codecov bot commented Mar 19, 2025

Codecov Report

Attention: Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 83.66%. Comparing base (87f3f0c) to head (8d922b4).

Files with missing lines Patch % Lines
path.go 85.71% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3356      +/-   ##
==========================================
- Coverage   83.73%   83.66%   -0.08%     
==========================================
  Files         118      118              
  Lines       11728    11739      +11     
==========================================
  Hits         9821     9821              
- Misses       1481     1489       +8     
- Partials      426      429       +3     
Flag Coverage Δ
unittests 83.66% <88.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

📝 [Proposal]: ability to override CustomConstraint
3 participants