-
Notifications
You must be signed in to change notification settings - Fork 1
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
ref: Helpers #204
Conversation
WalkthroughThis 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
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
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this 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
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 twoPageState
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 thehelpers
package.
85-85
: LGTM!The
WithErrorResponse
function is well-structured and the change simplifies the code by removing the dependency on thehelpers
package.
103-103
: LGTM!The
WithRatelimitData
function is well-structured and the change simplifies the code by removing the dependency on thehelpers
package.
119-119
: LGTM!The
WithXMLResponse
function is well-structured and the change simplifies the code by removing the dependency on thehelpers
package.
135-138
: LGTM!The
WithResponse
function is well-structured and the change simplifies the code by removing the dependency on thehelpers
package.
80f7d16
to
7539c5c
Compare
There was a problem hiding this 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
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 thatpflag
is used in thepkg/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
7539c5c
to
cdc7cdf
Compare
There was a problem hiding this 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
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 usingrequire
assertions.pkg/ratelimit/http_test.go (1)
11-38
: LGTM!The tests for
ExtractRateLimitData
are comprehensive and cover different scenarios usingrequire
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
andGrantsIntegrationTest
functions are well-structured and make good use of therequire
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
, andAssertNoRatelimitAnnotations
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
andExtractRateLimitData
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 toratelimit
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 toIsJSONContentType
fromratelimit
package.The function now directly calls
IsJSONContentType
from theratelimit
package, enhancing clarity by associating the check with its specific functionality.
142-142
: Direct call toIsJSONContentType
fromratelimit
package.The function now directly calls
IsJSONContentType
from theratelimit
package, which aligns with the refactoring strategy for better maintainability.
160-160
: Direct call toExtractRateLimitData
fromratelimit
package.The function now directly calls
ExtractRateLimitData
from theratelimit
package, which is consistent with the refactoring to source utility functions from their specific packages.
176-176
: Direct call toIsXMLContentType
fromratelimit
package.The function now directly calls
IsXMLContentType
from theratelimit
package, enhancing clarity by associating the check with its specific functionality.
192-195
: Direct calls toIsJSONContentType
andIsXMLContentType
fromratelimit
package.The function now directly calls
IsJSONContentType
andIsXMLContentType
from theratelimit
package, aligning with the refactoring strategy for improved code clarity and maintainability.
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 |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Description
pkg/helpers/helpers.go
.pkg/test.go
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Tests
Chores