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

Use utils Trim functions instead of the strings/bytes functions #3087

Merged
merged 2 commits into from
Jul 24, 2024

Conversation

ReneWerner87
Copy link
Member

  • Performance improvement (non-breaking change which improves efficiency)

@ReneWerner87 ReneWerner87 added this to the v3 milestone Jul 24, 2024
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner July 24, 2024 06:13
@ReneWerner87 ReneWerner87 requested review from gaby, sixcolors and efectn and removed request for a team July 24, 2024 06:13
Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Walkthrough

The changes across the codebase focus on replacing standard string manipulation functions from the Go strings package with utility functions from github.com/gofiber/utils/v2. This transition enhances modularity, consistency, and potential performance by centralizing string trimming operations. Overall, these modifications improve whitespace handling and streamline string manipulation throughout the application.

Changes

Files Change Summary
client/response.go Replaced strings.TrimSpace with utils.Trim for whitespace handling in response bodies.
ctx.go, helpers.go Replaced strings.TrimRight, strings.TrimLeft, and strings.TrimSpace with utils equivalents for consistency.
middleware/cors/cors.go Updated from strings.TrimSpace to utils.Trim for handling origin strings in CORS middleware.
middleware/csrf/csrf.go Switched from strings.TrimSpace to utils.Trim for trimming origin strings in CSRF processing.
middleware/csrf/csrf_test.go Changed test functions to use utils.Trim instead of strings.TrimSpace for cookie header parsing.
middleware/pprof/pprof.go Replaced strings.TrimRight with utils.TrimRight for trimming trailing slashes in paths.
mount.go Updated to use utils.TrimRight for trimming trailing slashes in mount functions.
path.go Changed strings.TrimRight to utils.TrimRight for path manipulations; specified slashDelimiterStr type as byte.
redirect.go Replaced strings.Trim with utils.Trim for trimming in cookie value processing.
router.go Updated from strings.TrimRight to utils.TrimRight for handling trailing slashes in routes.
middleware/cache/manager_msgp_test.go, middleware/csrf/manager_msgp_test.go, middleware/limiter/manager_msgp_test.go Renamed test functions to include specific prefixes for improved clarity regarding their context.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Request
    Server->>Server: Trim spaces using utils.Trim
    Server->>Client: Response
Loading

🐇 "In the code we trim with glee,
A hop towards utility!
Strings now dance, clean and neat,
Performance gains make us fleet!
With every line, our joy expands,
As we embrace the code’s new hands!" 🌟


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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Copy link

codecov bot commented Jul 24, 2024

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 83.64%. Comparing base (011c8f8) to head (e4235bd).
Report is 1 commits behind head on main.

Files Patch % Lines
middleware/pprof/pprof.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3087      +/-   ##
==========================================
+ Coverage   83.60%   83.64%   +0.04%     
==========================================
  Files         115      115              
  Lines        8342     8342              
==========================================
+ Hits         6974     6978       +4     
+ Misses       1044     1041       -3     
+ Partials      324      323       -1     
Flag Coverage Δ
unittests 83.64% <95.65%> (+0.04%) ⬆️

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.

Copy link
Contributor

@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: 0

Outside diff range, codebase verification and nitpick comments (2)
middleware/pprof/pprof.go (1)

75-75: Ensure test coverage for the new utility function.

The change from strings.TrimRight to utils.TrimRight looks good and maintains the intended functionality.

However, no test cases covering the utils.TrimRight function were found. Please add appropriate test cases to ensure this utility function is covered by tests.

  • File: middleware/pprof/pprof.go
  • Line: 75
Analysis chain

Ensure test coverage for the new utility function.

The change from strings.TrimRight to utils.TrimRight looks good and maintains the intended functionality.

However, ensure that this change is covered by tests.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify test coverage for the new utility function `utils.TrimRight`.

# Test: Search for test cases covering the `utils.TrimRight` function. Expect: Test cases covering the utility function.
rg --type go -A 5 $'utils.TrimRight'

Length of output: 3147


Script:

#!/bin/bash
# Description: Verify test coverage for the new utility function `utils.TrimRight`.

# Search for test cases in Go test files that might be covering the `utils.TrimRight` function.
rg --type go -l '_test.go' | xargs rg 'utils.TrimRight'

Length of output: 53


Script:

#!/bin/bash
# Description: Verify test coverage for the new utility function `utils.TrimRight`.

# Search for any references to `TrimRight` in Go test files.
rg --type go -l '_test.go' | xargs rg 'TrimRight'

Length of output: 47

Tools
GitHub Check: codecov/patch

[warning] 75-75: middleware/pprof/pprof.go#L75
Added line #L75 was not covered by tests

mount.go (1)

43-43: Implementation of utils.TrimRight is missing.

The function utils.TrimRight is used in multiple places across the codebase, but its implementation could not be found. This is a critical issue that needs to be addressed.

  • Ensure that utils.TrimRight is implemented correctly in the utils package.
Analysis chain

Change approved.

The replacement of strings.TrimRight with utils.TrimRight aligns with the PR objectives of enhancing performance and consistency.

However, ensure that all usages of utils.TrimRight are correct and consistent.

Also applies to: 73-73


Ensure utils.TrimRight is correctly implemented and used consistently.

The function utils.TrimRight is used in multiple places across the codebase. To verify its correctness, please review its implementation.


Ensure utils.TrimRight is correctly implemented and used consistently.

The function utils.TrimRight is used in multiple places across the codebase. To verify its correctness, please review its implementation.


Ensure utils.TrimRight is correctly implemented and used consistently.

The function utils.TrimRight is used in multiple places across the codebase. To verify its correctness, please review its implementation.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of `utils.TrimRight` in the codebase.

# Test: Search for the function usage. Expect: Correct and consistent usage.
rg --type go -A 5 $'utils.TrimRight'

Length of output: 3147


Script:

#!/bin/bash
# Description: Verify the implementation of `utils.TrimRight`.

# Test: Search for the definition of the function `TrimRight` in the `utils` package.
ast-grep --lang go --pattern $'func TrimRight($_, $_) $_'

Length of output: 58


Script:

#!/bin/bash
# Description: Verify the implementation of `utils.TrimRight`.

# Test: Search for the definition of the function `TrimRight` in the `utils` package.
rg --type go 'func TrimRight'

Length of output: 31


Script:

#!/bin/bash
# Description: Verify the implementation of `utils.TrimRight`.

# Test: Search for the term `TrimRight` in all files to locate its definition.
rg 'TrimRight'

Length of output: 758

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e2cd100 and 7e16a76.

Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing (11)
  • client/response.go (2 hunks)
  • ctx.go (5 hunks)
  • helpers.go (4 hunks)
  • middleware/cors/cors.go (2 hunks)
  • middleware/csrf/csrf.go (2 hunks)
  • middleware/csrf/csrf_test.go (2 hunks)
  • middleware/pprof/pprof.go (2 hunks)
  • mount.go (3 hunks)
  • path.go (3 hunks)
  • redirect.go (1 hunks)
  • router.go (2 hunks)
Additional context used
GitHub Check: codecov/patch
middleware/pprof/pprof.go

[warning] 75-75: middleware/pprof/pprof.go#L75
Added line #L75 was not covered by tests

Additional comments not posted (26)
middleware/pprof/pprof.go (1)

8-8: Import statement looks good.

The new dependency on github.com/gofiber/utils/v2 is correctly added.

client/response.go (1)

70-70: Ensure test coverage for the new utility function.

The change from strings.TrimSpace to utils.Trim looks good and maintains the intended functionality.

However, ensure that this change is covered by tests.

middleware/cors/cors.go (3)

9-9: Import statement looks good.

The new dependency on github.com/gofiber/utils/v2 is correctly added.


48-48: Ensure test coverage for the new utility function.

The change from strings.TrimSpace to utils.Trim looks good and maintains the intended functionality.

However, ensure that this change is covered by tests.

Verification successful

Ensure test coverage for utils.Trim function in middleware/cors/cors.go.

The utils.Trim function is used in multiple files, including test files. Specifically, it appears in middleware/csrf/csrf_test.go. Ensure that these test cases comprehensively cover the changes made in middleware/cors/cors.go.

  • Files to review for test coverage:
    • middleware/csrf/csrf_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify test coverage for the new utility function `utils.Trim`.

# Test: Search for test cases covering the `utils.Trim` function. Expect: Test cases covering the utility function.
rg --type go -A 5 $'utils.Trim'

Length of output: 7094


56-56: Ensure test coverage for the new utility function.

The change from strings.TrimSpace to utils.Trim looks good and maintains the intended functionality.

However, ensure that this change is covered by tests.

mount.go (1)

11-12: Import statement approved.

The import statement for github.com/gofiber/utils/v2 is necessary for using the utils.TrimRight function.

redirect.go (2)

Line range hint 11-12:
Import statement approved.

The import statement for github.com/gofiber/utils/v2 is necessary for using the utils.Trim function.


252-252: Change approved.

The replacement of strings.Trim with utils.Trim aligns with the PR objectives of enhancing performance and consistency.

However, ensure that all usages of utils.Trim are correct and consistent.

Also applies to: 255-255

middleware/csrf/csrf.go (2)

11-12: Import statement approved.

The import statement for github.com/gofiber/utils/v2 is necessary for using the utils.Trim function.


66-66: Change approved.

The replacement of strings.TrimSpace with utils.Trim aligns with the PR objectives of enhancing performance and consistency.

However, ensure that all usages of utils.Trim are correct and consistent.

Also applies to: 74-74

router.go (2)

256-256: Approved: Use of utils.TrimRight for consistency.

The replacement of strings.TrimRight with utils.TrimRight enhances consistency in string manipulation across the codebase.


327-327: Approved: Use of utils.TrimRight for consistency.

The replacement of strings.TrimRight with utils.TrimRight enhances consistency in string manipulation across the codebase.

helpers.go (4)

225-225: Approved: Use of utils.TrimRight for consistency.

The replacement of strings.TrimRight with utils.TrimRight enhances consistency in string manipulation across the codebase.


339-339: Approved: Use of utils.TrimLeft for consistency.

The replacement of strings.TrimLeft with utils.TrimLeft enhances consistency in string manipulation across the codebase.


359-359: Approved: Use of utils.TrimLeft for consistency.

The replacement of strings.TrimLeft with utils.TrimLeft enhances consistency in string manipulation across the codebase.


462-462: Approved: Use of utils.Trim for consistency.

The replacement of strings.Trim with utils.Trim enhances consistency in string manipulation across the codebase.

path.go (3)

54-54: Approved: Explicit type declaration for slashDelimiterStr.

The explicit type declaration for slashDelimiterStr as byte improves type safety and readability.


164-164: Approved: Use of utils.TrimRight for consistency.

The replacement of strings.TrimRight with utils.TrimRight enhances consistency in string manipulation across the codebase.


236-236: Approved: Use of utils.TrimRight for consistency.

The replacement of strings.TrimRight with utils.TrimRight enhances consistency in string manipulation across the codebase.

middleware/csrf/csrf_test.go (2)

251-253: LGTM! But verify the usage of utils.Trim.

The change to use utils.Trim(header, ' ') instead of strings.TrimSpace(header) looks good and should improve consistency.

Ensure that utils.Trim is correctly used across the codebase.

Verification successful

Verified: The usage of utils.Trim is consistent across the codebase.

The change to use utils.Trim(header, ' ') instead of strings.TrimSpace(header) in middleware/csrf/csrf_test.go is appropriate and aligns with other instances of utils.Trim usage.

  • middleware/csrf/csrf_test.go lines 251-253
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `utils.Trim` across the codebase.

# Test: Search for the function usage. Expect: Only occurances of `utils.Trim`.
rg --type go -A 5 $'utils.Trim'

Length of output: 7094


142-144: LGTM! But verify the usage of utils.Trim.

The change to use utils.Trim(header, ' ') instead of strings.TrimSpace(header) looks good and should improve consistency.

Ensure that utils.Trim is correctly used across the codebase.

Verification successful

Verification Successful: Usage of utils.Trim is consistent across the codebase

The change to use utils.Trim(header, ' ') instead of strings.TrimSpace(header) is consistent with the usage of utils.Trim across the codebase.

  • middleware/csrf/csrf_test.go: Lines 142-144
  • redirect.go: Lines 1-5
  • router.go: Lines 1-5
  • path.go: Lines 1-5
  • mount.go: Lines 1-5
  • helpers.go: Lines 1-5
  • ctx.go: Lines 1-5
  • middleware/pprof/pprof.go: Lines 1-5
  • middleware/csrf/csrf.go: Lines 1-5
  • middleware/cors/cors.go: Lines 1-5
  • client/response.go: Lines 1-5
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `utils.Trim` across the codebase.

# Test: Search for the function usage. Expect: Only occurances of `utils.Trim`.
rg --type go -A 5 $'utils.Trim'

Length of output: 7094

ctx.go (5)

831-831: Verify the utility function utils.TrimRight.

Ensure that utils.TrimRight behaves as expected and handles edge cases correctly.


942-942: Verify the utility function utils.TrimRight.

Ensure that utils.TrimRight behaves as expected and handles edge cases correctly.


1813-1813: Verify the utility function utils.TrimRight.

Ensure that utils.TrimRight behaves as expected and handles edge cases correctly.


865-865: Verify the utility function utils.TrimLeft.

Ensure that utils.TrimLeft behaves as expected and handles edge cases correctly.


781-781: Verify the utility function utils.TrimRight.

Ensure that utils.TrimRight behaves as expected and handles edge cases correctly.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 7e16a76 Previous: e2cd100 Ratio
Benchmark_Compress_Levels/Zstd_LevelBestCompression - B/op 1 B/op 0 B/op +∞
Benchmark_Compress_Parallel/Zstd - B/op 2 B/op 0 B/op +∞
Benchmark_AppendMsgitem - MB/s 2706.94 MB/s 1778.55 MB/s 1.52

This comment was automatically generated by workflow using github-action-benchmark.

Copy link
Contributor

@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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7e16a76 and e4235bd.

Files selected for processing (3)
  • middleware/cache/manager_msgp_test.go (4 hunks)
  • middleware/csrf/manager_msgp_test.go (4 hunks)
  • middleware/limiter/manager_msgp_test.go (4 hunks)
Files skipped from review due to trivial changes (3)
  • middleware/cache/manager_msgp_test.go
  • middleware/csrf/manager_msgp_test.go
  • middleware/limiter/manager_msgp_test.go

@ReneWerner87 ReneWerner87 merged commit fadedcb into main Jul 24, 2024
14 of 16 checks passed
gaby pushed a commit that referenced this pull request Jul 25, 2024
* Use utils Trim functions instead of the strings/bytes functions

* rename Test and Benchmark functions with same name
@efectn efectn deleted the use-utils-improved-functions branch September 1, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant