-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
refactor: use slices.Equal to simplify code #23384
Conversation
Signed-off-by: fudancoder <[email protected].>
📝 WalkthroughWalkthroughThe pull request modifies the Changes
Possibly related PRs
Suggested reviewers
Finishing Touches
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
Documentation and Community
|
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
🧹 Nitpick comments (2)
core/header/service_test.go (2)
58-65
: Consider extracting repeated byte array into a constant.The same byte array literal is used multiple times, making the code harder to maintain and read. Consider extracting it into a constant.
package header import ( "crypto/sha256" "slices" "testing" "time" ) +const ( + testHash = "\x26\xb0\xb8\x3e\x72\x81\xbe\x3b\x11\x76\x58\xb6\xf2\x63\x6d\x03\x68\xca\xd3\xd7\x4f\x22\x24\x34\x28\xf5\x40\x1a\x4b\x70\x89\x7e" +) // ... rest of the code ... - if !slices.Equal([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.Hash) { - t.Errorf("expected Hash %v, got %v", []byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.Hash) + expectedHash := []byte(testHash) + if !slices.Equal(expectedHash, info.Hash) { + t.Errorf("expected Hash %v, got %v", expectedHash, info.Hash) } if info.Time != time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC) { t.Errorf("expected Time %v, got %v", time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC), info.Time) } - if !slices.Equal([]byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.AppHash) { - t.Errorf("expected AppHash %v, got %v", []byte{0x26, 0xb0, 0xb8, 0x3e, 0x72, 0x81, 0xbe, 0x3b, 0x11, 0x76, 0x58, 0xb6, 0xf2, 0x63, 0x6d, 0x3, 0x68, 0xca, 0xd3, 0xd7, 0x4f, 0x22, 0x24, 0x34, 0x28, 0xf5, 0x40, 0x1a, 0x4b, 0x70, 0x89, 0x7e}, info.AppHash) + if !slices.Equal(expectedHash, info.AppHash) { + t.Errorf("expected AppHash %v, got %v", expectedHash, info.AppHash) }
Line range hint
37-70
: Consider adding error test cases.The tests thoroughly cover the happy path but could benefit from additional test cases for error scenarios:
- Invalid byte slice length
- Corrupted data
- Invalid chain ID length
Would you like me to provide example test cases for these scenarios?
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
core/header/service_test.go
(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
core/header/service_test.go (2)
Pattern **/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern **/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"
⏰ Context from checks skipped due to timeout of 90000ms (14)
- GitHub Check: tests (03)
- GitHub Check: tests (02)
- GitHub Check: tests (01)
- GitHub Check: tests (00)
- GitHub Check: test-hubl
- GitHub Check: test-simapp-v2
- GitHub Check: test-sim-nondeterminism
- GitHub Check: test-system-v2
- GitHub Check: test-integration
- GitHub Check: build (arm64)
- GitHub Check: golangci-lint
- GitHub Check: build (amd64)
- GitHub Check: Analyze
- GitHub Check: Summary
🔇 Additional comments (2)
core/header/service_test.go (2)
33-35
: Clean and effective use of slices.Equal!Good replacement of custom byte comparison with the standard library's
slices.Equal
. The error message remains clear and informative.
5-5
: Verify Go version compatibility for slices package.The
slices
package was introduced in Go 1.21. Let's ensure the project's minimum Go version is compatible.✅ Verification successful
Go version compatibility confirmed for slices package
The project uses Go 1.23, which fully supports the
slices
package (introduced in Go 1.21). The import change is safe to use.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Check go.mod for minimum Go version grep -r "^go 1\." . | head -n 1Length of output: 152
Description
use slices.Equal to simplify code
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
in the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.
I have...
Summary by CodeRabbit
Refactor
slices.Equal
functionTests