-
Notifications
You must be signed in to change notification settings - Fork 57
refactor: use slices.Contains to simplify code #323
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: fengyuchuanshen <[email protected]>
WalkthroughTwo files were updated to replace explicit loops for membership checks with slices.Contains, adding the slices import. Logic and public APIs remain unchanged. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Pre-merge checks (3 passed)✅ Passed checks (3 passed)
Poem
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
pkg/apis/akash.network/v2beta2/node_info.go (1)
5-8: Likely JSON tag bug: Model uses json:"string".Assuming the field should marshal as “model”, fix the tag.
Apply:
type GPUCapabilities struct { Vendor string `json:"vendor" capabilities:"vendor"` - Model string `json:"string" capabilities:"model"` + Model string `json:"model" capabilities:"model"` }
🧹 Nitpick comments (4)
cluster/hostname.go (4)
377-381: Fix error message to report the blocked domain, not the hostname.Minor UX nit; the
%qcurrently printshostname.- return fmt.Errorf("%w: domain %q is blocked by this provider", ErrHostnameNotAllowed, hostname) + return fmt.Errorf("%w: domain %q is blocked by this provider", ErrHostnameNotAllowed, blockedDomain)
272-274: Optional: precompute a set for exact hostnames to make checks O(1).If
isHostnameBlockedis on hot paths, consider a map alongside the slice.Example:
type hostnameService struct { inUse map[string]hostnameID @@ - blockedHostnames []string + blockedHostnames []string + blockedHostnamesSet map[string]struct{} blockedDomains []string }Populate in
newHostnameService:blockedHostnames := make([]string, 0) blockedDomains := make([]string, 0) +blockedSet := make(map[string]struct{}) @@ blockedHostnames = append(blockedHostnames, name[1:]) } else { blockedHostnames = append(blockedHostnames, name) } } + for _, h := range blockedHostnames { blockedSet[h] = struct{}{} } @@ hs := &hostnameService{ @@ - blockedHostnames: blockedHostnames, + blockedHostnames: blockedHostnames, + blockedHostnamesSet: blockedSet,Use it in
isHostnameBlocked:- if slices.Contains(hs.blockedHostnames, hostname) { + if _, ok := hs.blockedHostnamesSet[hostname]; ok {
372-385: Optional: normalize case in isHostnameBlocked for safety.Callers lower-case today, but normalizing here removes that coupling.
func (hs *hostnameService) isHostnameBlocked(hostname string) error { + hostname = strings.ToLower(hostname) if slices.Contains(hs.blockedHostnames, hostname) {
62-71: Replace opaque error message "bob".Unclear to operators. Prefer a descriptive error.
- return nil, errors.New("bob") + return nil, errors.New("wait canceled")If this path represents a timeout, consider wiring a context with deadline and returning
context.DeadlineExceeded.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
cluster/hostname.go(2 hunks)pkg/apis/akash.network/v2beta2/node_info.go(2 hunks)
🔇 Additional comments (4)
pkg/apis/akash.network/v2beta2/node_info.go (2)
19-21: LGTM: simpler, equivalent semantics.
slices.Containspreserves prior behavior for nil/empty slices and improves readability.
3-3: Confirmed: Go ≥1.21 — no action required. go.mod contains "go 1.24.2" and "toolchain go1.24.6". Workflows use actions/setup-go@v5 with GOVERSION populated by script/tools.sh gotoolchain (which reads the toolchain from go.mod), so CI will install go1.24.6.cluster/hostname.go (2)
6-6: LGTM: stdlib slices import is appropriate.
372-375: LGTM: direct membership check reads better.Same O(n) behavior as the prior loop; clearer intent.
|
@troian @cloud-j-luna Hi, Could you please review this PR at your convenience? Thank you very much. |
There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.