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

ref: Helpers #204

Merged
merged 4 commits into from
Aug 20, 2024
Merged

ref: Helpers #204

merged 4 commits into from
Aug 20, 2024

Conversation

mgaeta
Copy link
Contributor

@mgaeta mgaeta commented Aug 7, 2024

Description

  • Remove pkg/helpers/helpers.go.
    • This file pattern isn't descriptive
    • Moved functions to more descriptive files
    • Kept around referenced to public functions to preserve backwards-compatibility
  • Add pkg/test.go
    • There is a lot of repetition in connector tests
  • Fix docstrings

Summary by CodeRabbit

  • New Features

    • Introduced functionality to manage and extract rate limit information from HTTP headers.
    • Added a utility to parse full names into first and last names.
    • Implemented pagination utilities for efficient resource retrieval.
  • Bug Fixes

    • Corrected typographical errors in comments for better clarity.
  • Documentation

    • Updated comments to enhance the clarity of function purposes.
  • Tests

    • Added unit tests for rate limit data extraction and name parsing functionality.
    • Introduced integration tests for grant management in the connector framework.
  • Chores

    • Refactored and deprecated several utility functions, improving code maintainability.
    • Updated helper function calls to streamline dependencies and enhance code clarity.

@mgaeta mgaeta requested a review from jirwin as a code owner August 7, 2024 00:46
Copy link

coderabbitai bot commented Aug 7, 2024

Walkthrough

This update introduces a variety of improvements across multiple packages, enhancing code clarity, correcting typos, and optimizing functionality. Key changes include the deprecation of certain utility functions in favor of more modular replacements, the introduction of new testing capabilities, and minor documentation refinements. Overall, these adjustments aim to streamline the codebase, improve maintainability, and ensure robust testing for future developments.

Changes

Files Change Summary
pkg/annotations/annotations.go Updated comment for clarity on the New function.
pkg/field/fields.go, pkg/field/validation.go Corrected typos in comments.
pkg/helpers/helpers.go Deprecated several utility functions, replaced with modular functions from other packages.
pkg/ratelimit/grpc.go, pkg/ratelimit/http.go Updated comments and introduced rate limit data handling in a new file.
pkg/test/integration.go, pkg/test/pagination.go, pkg/test/ratelimit.go Added integration tests for grant management, pagination utilities, and rate limiting behavior.
pkg/types/resource/parse.go Introduced SplitFullName function for parsing names.
pkg/uhttp/contenttype.go, pkg/uhttp/wrapper.go Added content type handling functions, optimized existing functions to reduce dependencies.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API
    participant RateLimitHandler
    participant ResponseHandler

    User->>API: Sends request
    API->>RateLimitHandler: Check rate limit
    RateLimitHandler-->>API: Returns rate limit status
    API->>ResponseHandler: Prepare response
    ResponseHandler-->>User: Sends response
Loading

🐇 In a world of code both bright and neat,
Changes are made, oh what a treat!
Functions deprecated, new tests unfold,
A rabbit’s joy in improvements bold!
With every tweak, the code takes flight,
Hopping towards a future so bright! 🌟


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

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ccaa7f6 and 80f7d16.

Files selected for processing (17)
  • pkg/annotations/annotations.go (1 hunks)
  • pkg/field/fields.go (1 hunks)
  • pkg/field/validation.go (1 hunks)
  • pkg/helpers/helpers.go (1 hunks)
  • pkg/pagination/pagination_test.go (2 hunks)
  • pkg/ratelimit/grpc.go (1 hunks)
  • pkg/ratelimit/http.go (1 hunks)
  • pkg/ratelimit/http_test.go (1 hunks)
  • pkg/test/integration.go (1 hunks)
  • pkg/test/pagination.go (1 hunks)
  • pkg/test/ratelimit.go (1 hunks)
  • pkg/types/resource/parse.go (1 hunks)
  • pkg/types/resource/parse_test.go (1 hunks)
  • pkg/types/resource/user_trait.go (1 hunks)
  • pkg/uhttp/contenttype.go (1 hunks)
  • pkg/uhttp/contenttype_test.go (1 hunks)
  • pkg/uhttp/wrapper.go (6 hunks)
Files skipped from review due to trivial changes (5)
  • pkg/annotations/annotations.go
  • pkg/field/fields.go
  • pkg/field/validation.go
  • pkg/ratelimit/grpc.go
  • pkg/types/resource/user_trait.go
Additional context used
golangci-lint
pkg/helpers/helpers.go

12-12: Comment should end in a period

(godot)


17-17: Comment should end in a period

(godot)


22-22: Comment should end in a period

(godot)


27-27: Comment should end in a period

(godot)

pkg/pagination/pagination_test.go

228-228: use of println forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)


229-229: use of println forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)


230-230: use of println forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

Additional comments not posted (32)
pkg/uhttp/contenttype_test.go (2)

10-18: LGTM!

The test case correctly verifies that IsJSONContentType returns true for a JSON content type.


20-28: LGTM!

The test case correctly verifies that IsJSONContentType returns false for a non-JSON content type.

pkg/uhttp/contenttype.go (2)

5-8: LGTM! Variable definition is correct.

The xmlContentTypes variable is correctly defined and initialized.


22-31: LGTM! Function implementation is correct.

The IsXMLContentType function is correctly implemented.

pkg/ratelimit/http_test.go (2)

11-27: LGTM! Test case implementation is correct.

The test case for status code http.StatusOK is well-implemented and correctly checks the function's behavior.


29-38: LGTM! Test case implementation is correct.

The test case for status code http.StatusTooManyRequests is well-implemented and correctly checks the function's behavior.

pkg/test/integration.go (2)

17-26: LGTM!

The function assertGrants is well-structured and uses appropriate error handling and assertions.


30-57: LGTM!

The function GrantsIntegrationTest is comprehensive and uses assertions to verify the expected behavior of grant and revoke operations.

pkg/test/ratelimit.go (4)

12-25: LGTM!

The function isRatelimitingAnnotation correctly checks for rate limiting annotations and handles errors appropriately.


28-35: LGTM!

The function IsRatelimited correctly iterates over the annotations and checks for rate limiting.


37-44: LGTM!

The function AssertWasRatelimited correctly asserts that a request was rate limited.


46-57: LGTM!

The function AssertNoRatelimitAnnotations correctly asserts that there are no rate limiting annotations.

pkg/test/pagination.go (4)

13-55: LGTM!

The function exhaustPagination correctly handles pagination and retrieves the full list of resources, with appropriate error handling and rate limit checks.


58-67: LGTM!

The function ExhaustResourcePagination correctly retrieves the full list of resources using pagination.


69-78: LGTM!

The function ExhaustEntitlementPagination correctly retrieves the full list of entitlements for a resource using pagination.


81-90: LGTM!

The function ExhaustGrantPagination correctly retrieves the full list of grants for a resource using pagination.

pkg/ratelimit/http.go (6)

12-16: LGTM!

The limitHeaders variable is correctly defined and includes a comment about non-standard headers.


18-22: LGTM!

The remainingHeaders variable is correctly defined and includes a comment about non-standard headers.


24-29: LGTM!

The resetAtHeaders variable is correctly defined and includes comments about non-standard headers and a common header for 429 responses.


31-31: LGTM!

The thirtyYears constant is correctly defined using a calculation.


34-61: LGTM!

The parseTime function is well-written and handles multiple datetime formats and edge cases. The comments explain the logic clearly.


63-127: LGTM!

The ExtractRateLimitData function is well-structured and handles various headers and status codes. The comments explain the logic clearly.

pkg/pagination/pagination_test.go (5)

4-4: LGTM!

The TestPaginationBagMarshalling function is well-structured and covers various test cases for marshalling and unmarshalling.


4-4: LGTM!

The comparePageState function is simple and correctly compares the fields of two PageState objects.


4-4: LGTM!

The TestPaginationBag function is well-structured and covers various test cases for pushing and popping states in the pagination bag.


4-4: LGTM!

The TestPageBagMarshalUnmarshal function is well-structured and covers various test cases for marshalling and unmarshalling.


4-4: LGTM!

The TestPaginationTokenUnmarshalEmptyString function is well-structured and covers the test case for unmarshalling an empty pagination token.

pkg/uhttp/wrapper.go (5)

61-61: LGTM!

The WithJSONResponse function is well-structured and the change simplifies the code by removing the dependency on the helpers package.


85-85: LGTM!

The WithErrorResponse function is well-structured and the change simplifies the code by removing the dependency on the helpers package.


103-103: LGTM!

The WithRatelimitData function is well-structured and the change simplifies the code by removing the dependency on the helpers package.


119-119: LGTM!

The WithXMLResponse function is well-structured and the change simplifies the code by removing the dependency on the helpers package.


135-138: LGTM!

The WithResponse function is well-structured and the change simplifies the code by removing the dependency on the helpers package.

pkg/types/resource/parse.go Outdated Show resolved Hide resolved
pkg/types/resource/parse.go Outdated Show resolved Hide resolved
pkg/types/resource/parse_test.go Outdated Show resolved Hide resolved
pkg/uhttp/contenttype.go Outdated Show resolved Hide resolved
pkg/helpers/helpers.go Outdated Show resolved Hide resolved
pkg/helpers/helpers.go Outdated Show resolved Hide resolved
pkg/helpers/helpers.go Outdated Show resolved Hide resolved
pkg/helpers/helpers.go Outdated Show resolved Hide resolved
pkg/pagination/pagination_test.go Outdated Show resolved Hide resolved
@mgaeta mgaeta force-pushed the marcos/ref/helpers branch from 80f7d16 to 7539c5c Compare August 7, 2024 01:05
@mgaeta mgaeta changed the title [WIP] ref: Helpers ref: Helpers Aug 7, 2024
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 80f7d16 and 7539c5c.

Files selected for processing (17)
  • go.mod (2 hunks)
  • pkg/annotations/annotations.go (1 hunks)
  • pkg/field/fields.go (1 hunks)
  • pkg/field/validation.go (1 hunks)
  • pkg/helpers/helpers.go (1 hunks)
  • pkg/ratelimit/grpc.go (1 hunks)
  • pkg/ratelimit/http.go (1 hunks)
  • pkg/ratelimit/http_test.go (1 hunks)
  • pkg/test/integration.go (1 hunks)
  • pkg/test/pagination.go (1 hunks)
  • pkg/test/ratelimit.go (1 hunks)
  • pkg/types/resource/parse.go (1 hunks)
  • pkg/types/resource/parse_test.go (1 hunks)
  • pkg/types/resource/user_trait.go (1 hunks)
  • pkg/uhttp/contenttype.go (1 hunks)
  • pkg/uhttp/contenttype_test.go (1 hunks)
  • pkg/uhttp/wrapper.go (6 hunks)
Files skipped from review due to trivial changes (4)
  • pkg/annotations/annotations.go
  • pkg/field/fields.go
  • pkg/field/validation.go
  • pkg/types/resource/user_trait.go
Files skipped from review as they are similar to previous changes (12)
  • pkg/helpers/helpers.go
  • pkg/ratelimit/grpc.go
  • pkg/ratelimit/http.go
  • pkg/ratelimit/http_test.go
  • pkg/test/integration.go
  • pkg/test/pagination.go
  • pkg/test/ratelimit.go
  • pkg/types/resource/parse.go
  • pkg/types/resource/parse_test.go
  • pkg/uhttp/contenttype.go
  • pkg/uhttp/contenttype_test.go
  • pkg/uhttp/wrapper.go
Additional comments not posted (1)
go.mod (1)

27-27: LGTM! Verify the usage of the new direct dependency.

The addition of github.com/spf13/pflag as a direct dependency is approved.

However, ensure that pflag is actively used in the codebase.

Verification successful

Verify the usage of pflag in the codebase.

The addition of github.com/spf13/pflag as a direct dependency is approved. The search results show that pflag is used in the pkg/config/config.go file, indicating active usage in the codebase.

  • pkg/config/config.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `pflag` in the codebase.

# Test: Search for the usage of `pflag`. Expect: Occurrences of `pflag` usage.
rg --type go 'pflag'

Length of output: 77203

@mgaeta mgaeta force-pushed the marcos/ref/helpers branch from 7539c5c to cdc7cdf Compare August 19, 2024 18:51
@mgaeta mgaeta requested a review from ggreer August 19, 2024 18:52
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

Outside diff range, codebase verification and nitpick comments (5)
pkg/types/resource/parse.go (1)

5-6: Correct the typo in the docstring.

The docstring has a typo: "everything to the left is last name" should be "everything to the right is last name".

-// everything to the left is last name.
+// everything to the right is last name.
pkg/helpers/helpers.go (4)

12-14: Correct the comment to end with a period.

The comment should end with a period.

// Deprecated: see resource.SplitFullName.

17-19: Correct the comment to end with a period.

The comment should end with a period.

// Deprecated: see ratelimit.ExtractRateLimitData.

22-24: Correct the comment to end with a period.

The comment should end with a period.

// Deprecated: see contenttype.IsJSONContentType.

27-29: Correct the comment to end with a period.

The comment should end with a period.

// Deprecated: see contenttype.IsXMLContentType.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7539c5c and cdc7cdf.

Files selected for processing (16)
  • pkg/annotations/annotations.go (1 hunks)
  • pkg/field/fields.go (1 hunks)
  • pkg/field/validation.go (1 hunks)
  • pkg/helpers/helpers.go (1 hunks)
  • pkg/ratelimit/grpc.go (1 hunks)
  • pkg/ratelimit/http.go (1 hunks)
  • pkg/ratelimit/http_test.go (1 hunks)
  • pkg/test/integration.go (1 hunks)
  • pkg/test/pagination.go (1 hunks)
  • pkg/test/ratelimit.go (1 hunks)
  • pkg/types/resource/parse.go (1 hunks)
  • pkg/types/resource/parse_test.go (1 hunks)
  • pkg/types/resource/user_trait.go (1 hunks)
  • pkg/uhttp/contenttype.go (1 hunks)
  • pkg/uhttp/contenttype_test.go (1 hunks)
  • pkg/uhttp/wrapper.go (6 hunks)
Files skipped from review due to trivial changes (5)
  • pkg/annotations/annotations.go
  • pkg/field/fields.go
  • pkg/field/validation.go
  • pkg/ratelimit/grpc.go
  • pkg/types/resource/user_trait.go
Additional comments not posted (15)
pkg/uhttp/contenttype.go (2)

10-24: LGTM!

The IsJSONContentType function is well-optimized using a single return statement.


15-25: LGTM!

The IsXMLContentType function appropriately normalizes the content type and checks against a list of XML content types.

pkg/uhttp/contenttype_test.go (1)

10-28: LGTM!

The tests for IsJSONContentType are well-structured and cover both success and failure cases using require assertions.

pkg/ratelimit/http_test.go (1)

11-38: LGTM!

The tests for ExtractRateLimitData are comprehensive and cover different scenarios using require assertions.

pkg/types/resource/parse_test.go (1)

9-54: Good use of table-driven tests and edge case coverage.

The test function TestHelpers_SplitFullName effectively uses table-driven tests and covers a range of scenarios, including edge cases like empty strings and multiple spaces. This enhances readability and maintainability.

pkg/test/integration.go (1)

17-58: Well-structured integration test functions with appropriate assertions.

The assertGrants and GrantsIntegrationTest functions are well-structured and make good use of the require package for assertions. The test effectively verifies the creation and revocation of grants.

pkg/test/ratelimit.go (1)

12-57: Clear and focused rate limit assertion functions.

The functions isRatelimitingAnnotation, IsRatelimited, AssertWasRatelimited, and AssertNoRatelimitAnnotations are clear and effectively focused on handling rate limit checks and assertions.

pkg/test/pagination.go (1)

13-91: Effective use of generics for pagination handling.

The exhaustPagination function uses generics to handle pagination, improving code reusability. The specific functions (ExhaustResourcePagination, ExhaustEntitlementPagination, ExhaustGrantPagination) are clear and concise.

pkg/ratelimit/http.go (1)

33-126: Robust rate limit data extraction with comprehensive error handling.

The parseTime and ExtractRateLimitData functions are robust and handle various edge cases and potential errors effectively. The code ensures accurate extraction of rate limit data from HTTP headers.

pkg/uhttp/wrapper.go (6)

17-17: Import change to ratelimit package.

The import statement has been updated to use the ratelimit package, which is consistent with the refactoring to source utility functions directly from their respective packages.


118-118: Direct call to IsJSONContentType from ratelimit package.

The function now directly calls IsJSONContentType from the ratelimit package, enhancing clarity by associating the check with its specific functionality.


142-142: Direct call to IsJSONContentType from ratelimit package.

The function now directly calls IsJSONContentType from the ratelimit package, which aligns with the refactoring strategy for better maintainability.


160-160: Direct call to ExtractRateLimitData from ratelimit package.

The function now directly calls ExtractRateLimitData from the ratelimit package, which is consistent with the refactoring to source utility functions from their specific packages.


176-176: Direct call to IsXMLContentType from ratelimit package.

The function now directly calls IsXMLContentType from the ratelimit package, enhancing clarity by associating the check with its specific functionality.


192-195: Direct calls to IsJSONContentType and IsXMLContentType from ratelimit package.

The function now directly calls IsJSONContentType and IsXMLContentType from the ratelimit package, aligning with the refactoring strategy for improved code clarity and maintainability.

Comment on lines +7 to +17
func SplitFullName(name string) (string, string) {
names := strings.SplitN(name, " ", 2)
var firstName, lastName string
if len(names) > 0 {
firstName = names[0]
}
if len(names) > 1 {
lastName = names[1]
}

return firstName, lastName
Copy link

@coderabbitai coderabbitai bot Aug 19, 2024

Choose a reason for hiding this comment

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

Optimize the function logic.

The function logic can be simplified by directly assigning the results of strings.SplitN to firstName and lastName.

func SplitFullName(name string) (firstName, lastName string) {
	names := strings.SplitN(name, " ", 2)

	if len(names) > 0 {
		firstName = names[0]
	}
	if len(names) > 1 {
		lastName = names[1]
	}

	return firstName, lastName
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We prefer not to have named return values.

Copy link

Choose a reason for hiding this comment

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

@mgaeta, understood. I'll remember your preference against using named return values for future suggestions. Thank you for clarifying!


Learnings added
Learnt from: mgaeta
PR: ConductorOne/baton-sdk#204
File: pkg/types/resource/parse.go:7-17
Timestamp: 2024-08-19T18:59:22.437Z
Learning: In the `baton-sdk` repository, avoid using named return values in function signatures, as per the user's preference.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@ggreer ggreer merged commit a743ff5 into main Aug 20, 2024
4 checks passed
@ggreer ggreer deleted the marcos/ref/helpers branch August 20, 2024 17:43
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.

2 participants