-
Notifications
You must be signed in to change notification settings - Fork 108
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
chore(primitives): Dropped hex.String
#2048
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes in this pull request introduce new functionalities for encoding and decoding big integers as hexadecimal strings in the Changes
Possibly related PRs
Suggested reviewers
Poem
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2048 +/- ##
==========================================
- Coverage 23.50% 23.39% -0.12%
==========================================
Files 356 355 -1
Lines 16054 16025 -29
Branches 12 12
==========================================
- Hits 3774 3749 -25
+ Misses 12115 12111 -4
Partials 165 165
|
hex.String
hex.String
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
🧹 Outside diff range comments (1)
mod/primitives/pkg/encoding/hex/u64_test.go (1)
Line range hint
57-77
: ImprovedTestValidateQuotedString
functionThe changes to this test function are well-implemented:
- The function has been correctly renamed to
TestValidateQuotedString
, reflecting the changes in the tested function.- The function now calls
hex.ValidateQuotedString
, which is consistent with the renaming.- Error handling has been simplified, improving readability.
These changes align well with the PR objectives of streamlining the code.
However, a minor improvement could be made to the error assertion:
Consider using
require.ErrorIs
instead ofrequire.Equal
for error comparison. This change would make the test more robust against potential wrapping of errors in the future. Here's the suggested change:- require.Equal(t, test.expected, err) + require.ErrorIs(t, err, test.expected)This change would apply to line 73 of the provided code.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (13)
- mod/primitives/pkg/bytes/b.go (1 hunks)
- mod/primitives/pkg/bytes/b_test.go (2 hunks)
- mod/primitives/pkg/encoding/hex/big_int.go (1 hunks)
- mod/primitives/pkg/encoding/hex/bit_int_test.go (1 hunks)
- mod/primitives/pkg/encoding/hex/bytes.go (1 hunks)
- mod/primitives/pkg/encoding/hex/format.go (1 hunks)
- mod/primitives/pkg/encoding/hex/format_test.go (1 hunks)
- mod/primitives/pkg/encoding/hex/hex_test.go (0 hunks)
- mod/primitives/pkg/encoding/hex/string.go (0 hunks)
- mod/primitives/pkg/encoding/hex/u64.go (0 hunks)
- mod/primitives/pkg/encoding/hex/u64_test.go (3 hunks)
- mod/primitives/pkg/math/u64.go (1 hunks)
- mod/primitives/pkg/math/u64_test.go (1 hunks)
💤 Files with no reviewable changes (3)
- mod/primitives/pkg/encoding/hex/hex_test.go
- mod/primitives/pkg/encoding/hex/string.go
- mod/primitives/pkg/encoding/hex/u64.go
🧰 Additional context used
🔇 Additional comments (18)
mod/primitives/pkg/bytes/b.go (1)
39-43
: Improved input validation and consistency in UnmarshalJSONThe changes to the
UnmarshalJSON
method are well-structured and improve the overall robustness of the code. The addition of input validation usinghex.ValidateQuotedString
is a good practice. The use ofb.UnmarshalText
promotes code reuse and aligns with Go's standard library approach.To ensure full compatibility, let's verify the behavior of
hex.ValidateQuotedString
:✅ Verification successful
Validation of
hex.ValidateQuotedString
ConfirmedThe
ValidateQuotedString
function is properly implemented and has corresponding test coverage, ensuring that input validation behaves as expected. This verifies the correctness and robustness of the changes made to theUnmarshalJSON
method.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the implementation of hex.ValidateQuotedString # Test: Check the implementation of ValidateQuotedString ast-grep --lang go --pattern 'func ValidateQuotedString(input []byte) ([]byte, error) { $$$ }' # Test: Check for any tests related to ValidateQuotedString rg --type go -i 'func TestValidateQuotedString'Length of output: 712
mod/primitives/pkg/encoding/hex/format_test.go (2)
21-21
: Approved: Package name change enhances test isolationChanging the package name from
hex
tohex_test
is a good practice. This change ensures that the tests are using the public API of thehex
package and prevents test code from being included in the main package.
Line range hint
1-71
: Verify the removal ofUnmarshalJSONText
functionThe AI summary mentions the removal of the
UnmarshalJSONText
function, but this change is not visible in the provided diff. It's important to verify that this removal doesn't negatively impact the package's functionality.Please run the following script to check for any remaining references to
UnmarshalJSONText
:If this search returns any results, we should review those occurrences to ensure they've been properly addressed or removed.
✅ Verification successful
Verified the removal of
UnmarshalJSONText
from thehex
package.
There are no remaining references toUnmarshalJSONText
in thepkg/encoding/hex
directory.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check for any remaining references to UnmarshalJSONText in the hex package # Test: Search for UnmarshalJSONText in the hex package rg --type go "UnmarshalJSONText" pkg/encoding/hexLength of output: 106
Script:
#!/bin/bash # Description: Search the entire repository for any references to UnmarshalJSONText # Search for UnmarshalJSONText in all Go files within the repository rg --type go "UnmarshalJSONText"Length of output: 115
mod/primitives/pkg/encoding/hex/big_int.go (3)
1-23
: LGTM: License, package declaration, and import are correct.The license header, package declaration, and import statement are all appropriate and correctly implemented.
73-81
: LGTM: MustToBigInt function is correctly implemented.The MustToBigInt function is a well-implemented wrapper around ToBigInt. It correctly panics on error and provides a clear, documented API for cases where the caller wants to avoid error checking.
1-81
: Overall, the file is well-implemented and aligns with PR objectives.This new file successfully introduces the functionality for encoding and decoding big integers as hexadecimal strings, as described in the AI-generated summary. The implementation is generally robust, with proper error handling and clear function signatures.
The changes align well with the PR objectives of streamlining the hex package by introducing standalone functions for BigInt operations that were previously part of the removed
hex.String
type.A few minor improvements were suggested:
- Clarifying the precondition for the
FromBigInt
function.- A potential optimization in the
ToBigInt
function for common cases.These changes will further enhance the clarity and efficiency of the code.
mod/primitives/pkg/encoding/hex/u64_test.go (2)
34-41
: Improved test cases forTestMarshalText
The changes to the
TestMarshalText
function are well-implemented:
- The expected output type has been correctly changed from
string
to[]byte
, which is more consistent with the typical return type ofMarshalText
methods in Go.- A new test case for a positive value (12345) has been added, improving the test coverage by including a non-zero, non-max value.
These changes enhance the robustness and completeness of the test suite.
44-52
: Enhanced test coverage inTestMarshalText
The changes to the test execution logic in
TestMarshalText
are well-implemented:
- The assertion has been correctly updated to use the new
[]byte
expected type, maintaining consistency with the test case changes.- A decoding step has been added (lines 50-52), which verifies the roundtrip consistency of the marshaling and unmarshaling process. This significantly improves the test coverage by ensuring that the encoded value can be correctly decoded back to its original form.
These changes enhance the overall quality and reliability of the test suite.
mod/primitives/pkg/encoding/hex/bit_int_test.go (3)
1-22
: LGTM: License header and package declaration are correct.The BUSL-1.1 license header is properly included, and the package is correctly declared as
hex_test
, following Go conventions for test files.
24-31
: LGTM: Import statements are appropriate.The import block includes all necessary packages, including standard library packages and the project's
hex
package. The use ofrequire
from the testify package is a good practice for concise and clear test assertions.
1-110
: Overall, excellent test coverage and structure.This new test file provides comprehensive coverage for the
hex
package'sBigInt
related functions. The tests are well-structured, use table-driven testing, and cover important scenarios including edge cases. The minor suggestions for improvement (adding a negative number test case and a very large number test case) would further enhance the robustness of the test suite.Great job on writing thorough and maintainable tests!
mod/primitives/pkg/encoding/hex/bytes.go (1)
Line range hint
1-81
: Overall, the changes improve input validation without disrupting the file structureThe modifications to
DecodeFixedJSON
enhance the input validation process while maintaining the overall structure and functionality of the file. The rest of the file, including other utility functions for hexadecimal encoding and decoding, remains unchanged and well-organized.mod/primitives/pkg/math/u64_test.go (1)
63-66
: Excellent addition of new test cases for improved coverage!The new test cases for "Invalid JSON text", "Invalid quoted JSON text", and "Empty JSON text" significantly enhance the robustness of the
TestU64_UnmarshalJSON
function. These additions cover important edge cases that were previously untested, ensuring that theUnmarshalJSON
method handles various error scenarios correctly.
- The "Invalid JSON text" case checks the behavior when an empty string is provided.
- The "Invalid quoted JSON text" case verifies the handling of an incomplete JSON string.
- The "Empty JSON text" case tests the response to an empty quoted string.
These improvements will help catch potential issues related to JSON unmarshalling, making the code more reliable and maintainable.
mod/primitives/pkg/bytes/b_test.go (2)
Line range hint
139-917
: Summary: Improved test coverage and naming clarity.The changes in this file enhance the test suite by:
- Adding a new test function
TestBytesUnmarshalJSONText
to cover JSON unmarshalling scenarios.- Renaming
TestUnmarshalJSON
toTestBytes48UnmarshalJSON
for better specificity.These modifications improve the overall quality and maintainability of the test suite.
917-917
: LGTM! Verify no unintended changes in the function body.The renaming of
TestUnmarshalJSON
toTestBytes48UnmarshalJSON
improves clarity by specifying the exact type being tested. This change aligns well with the naming convention of other test functions in the file.Please run the following script to verify that no unintended changes were made to the function body:
mod/primitives/pkg/encoding/hex/format.go (3)
23-26
: Necessary imports addedThe addition of the
"strings"
package is appropriate for the new string manipulation functions.
31-42
:IsValidHex
function enhances hex validationThe new
IsValidHex
function provides a robust and generic approach to hex string validation, effectively handling both[]byte
andstring
types. The use of type approximation~[]byte | ~string
aligns with Go's best practices for generics.
44-50
:ValidateQuotedString
function correctly validates quoted stringsThe
ValidateQuotedString
function efficiently checks for properly quoted input and returns the unquoted content, enhancing input validation processes.
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: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (1)
- mod/primitives/pkg/encoding/hex/u64_test.go (3 hunks)
🧰 Additional context used
🔇 Additional comments (1)
mod/primitives/pkg/encoding/hex/u64_test.go (1)
34-34
: Test cases updated to use[]byte
for expected valuesThe test cases in
TestMarshalText
have been correctly updated to use[]byte
for theexpected
field, matching the change in the expected output type. This ensures that the tests properly validate the function's output.Also applies to: 36-41
hex.String
hex.String
} | ||
return nil | ||
} | ||
|
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.
q: in UnmarshalUint64Text
, I wonder if we should use return strconv.ParseUint(string(raw), hexBase, 0)
instead of the current implementation?
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 comments (1)
mod/primitives/pkg/encoding/hex/format.go (1)
Line range hint
61-70
: Ensure consistent type constraints informatAndValidateNumber
The type constraint for
T
informatAndValidateNumber
is[]byte | string
, whereasIsValidHex
uses~[]byte | ~string
. For consistency and to support types derived from[]byte
orstring
, consider modifying the type constraint.Update the function signature to:
-func formatAndValidateNumber[T []byte | string](input T) (T, error) { +func formatAndValidateNumber[T ~[]byte | ~string](input T) (T, error) {This change aligns the type constraints and ensures that both functions handle the same set of types.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (4)
- mod/primitives/pkg/encoding/hex/big_int.go (1 hunks)
- mod/primitives/pkg/encoding/hex/bytes.go (1 hunks)
- mod/primitives/pkg/encoding/hex/format.go (1 hunks)
- mod/primitives/pkg/encoding/hex/u64.go (0 hunks)
💤 Files with no reviewable changes (1)
- mod/primitives/pkg/encoding/hex/u64.go
🧰 Additional context used
🔇 Additional comments (6)
mod/primitives/pkg/encoding/hex/bytes.go (2)
77-81
: Add comprehensive tests for ValidateQuotedString in bytes.goWhile
ValidateQuotedString
is implemented informat.go
and has tests inu64_test.go
, there are no tests covering its usage inbytes.go
. To ensure robust validation and prevent potential issues, please add relevant tests inbytes_test.go
to cover all edge cases and usage scenarios withinbytes.go
.
77-81
: Improved input validation, but verify compatibility and error handlingThe changes to
DecodeFixedJSON
enhance input validation by introducingValidateQuotedString
. This likely provides more robust checking than the previousisQuotedString
method. The error handling is also more specific, which could aid in debugging.However, please consider the following points:
- Ensure that
ValidateQuotedString
is thoroughly tested and handles all edge cases.- Verify that the change in error handling doesn't negatively impact any downstream error processing.
- Confirm that removing the explicit check for quoted strings doesn't break backwards compatibility with any existing clients.
To verify these points, run the following script:
✅ Verification successful
Validation of
ValidateQuotedString
implementation and usage confirmedThe
ValidateQuotedString
function is properly implemented and utilized across multiple modules. Comprehensive tests are in place to ensure its reliability. Additionally, the removal ofisQuotedString
does not appear to impact backward compatibility.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify ValidateQuotedString implementation, usage, and tests # Test 1: Find the implementation of ValidateQuotedString echo "Searching for ValidateQuotedString implementation:" rg --type go "func ValidateQuotedString" # Test 2: Check for other usages of ValidateQuotedString echo "Searching for other usages of ValidateQuotedString:" rg --type go "ValidateQuotedString\(" # Test 3: Look for any tests related to ValidateQuotedString echo "Searching for tests related to ValidateQuotedString:" rg --type go "Test.*ValidateQuotedString" # Test 4: Check for potential backwards compatibility issues echo "Searching for potential backwards compatibility issues:" rg --type go "isQuotedString"Length of output: 1353
mod/primitives/pkg/encoding/hex/big_int.go (2)
39-71
: LGTM:ToBigInt
function is well-implemented.The
ToBigInt
function correctly decodes a hexadecimal string into a*big.Int
with proper input validation and error handling.
73-81
: LGTM:MustToBigInt
function is correctly implemented.The
MustToBigInt
function effectively handles conversion by panicking on invalid inputs, ensuring that callers receive a valid*big.Int
or an immediate failure.mod/primitives/pkg/encoding/hex/format.go (2)
23-26
: Approved: Import statements correctly updatedThe import of
"strings"
is necessary for the new functions and is appropriately added alongside"errors"
.
44-50
: Approved: New functionValidateQuotedString
added correctlyThe
ValidateQuotedString
function correctly checks for quoted strings and returns the unquoted content, enhancing input validation.
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.
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: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (2)
- mod/primitives/pkg/math/u64.go (1 hunks)
- mod/primitives/pkg/math/u64_test.go (1 hunks)
Dropped
hex.String
type with the following steps:json.go
file fromhex
package. Moved UTs to client packages.To/FromUInt64
String
attributes. Only the counterparts inu64.go
file are really used. Moved relevant UTs.BigInt
methods out ofhex.String
into their own function.hex.NewString
ctor andhex.String
type as a wholeThe somewhat large number of lines changed is consequence of the reshuffling of tests to different files.
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Bug Fixes
Tests