-
Notifications
You must be signed in to change notification settings - Fork 268
refactor: use a more modern writing style to simplify code #1854
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: deepdring <[email protected]>
WalkthroughRefactors internal logic in two areas: replaces a manual map-copy loop with maps.Copy in app/app.go, and simplifies context height calculation in x/cronos/rpc/api.go using max(blockNumber-1, 1). No public APIs changed; behavior preserved. Changes
Sequence Diagram(s)Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
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: 2
🧹 Nitpick comments (1)
app/app.go (1)
1346-1348
: Prefer maps.Clone and pre-size; consider deep-copying slice values if immutability is desired.Functional behavior is fine and matches the prior shallow-copy semantics. For clarity and a tiny perf win, either use maps.Clone or pre-size the map when using maps.Copy. If external callers must not mutate the inner []string, switch to a deep copy.
Use maps.Clone (simplest):
func GetMaccPerms() map[string][]string { - dupMaccPerms := make(map[string][]string) - maps.Copy(dupMaccPerms, maccPerms) - return dupMaccPerms + return maps.Clone(maccPerms) }Or keep maps.Copy but pre-size:
-dupMaccPerms := make(map[string][]string) +dupMaccPerms := make(map[string][]string, len(maccPerms)) maps.Copy(dupMaccPerms, maccPerms)If you want immutability guarantees, deep copy the slice values:
func GetMaccPerms() map[string][]string { out := make(map[string][]string, len(maccPerms)) for k, v := range maccPerms { out[k] = append([]string(nil), v...) } return out }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
app/app.go
(2 hunks)x/cronos/rpc/api.go
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: semgrep-cloud-platform/scan
- GitHub Check: semgrep-cloud-platform/scan
@@ -8,6 +8,7 @@ import ( | |||
"fmt" | |||
"io" | |||
"io/fs" | |||
"maps" |
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.
💡 Verification agent
🧩 Analysis chain
Confirm toolchain supports stdlib “maps” (Go 1.21+).
The new import relies on the standard library package maps, which requires Go 1.21+. It looks consistent with existing usage of slices (also Go 1.21+), but please confirm CI/tooling is pinned accordingly to avoid build breaks.
Run this script to verify the repo’s Go version and CI setup:
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "go.mod go/toolchain directives:"
rg -nP '^(go|toolchain)\s+\d' go.mod || true
sed -n '1,20p' go.mod | nl -ba
echo -e "\nCheck workflows pinning setup-go:"
fd -t f -a .github/workflows -e yml -e yaml | while read -r f; do
echo "==> $f"
rg -nP 'setup-go|go-version' "$f" || true
done
echo -e "\nSearch for local helpers named max/min that might mask missing stdlib cmp.Max/Min:"
rg -nP -C2 '(?m)^\s*func\s+(max|min)\s*\[.*\]\s*\(.*\)|^\s*func\s+(max|min)\s*\('
Length of output: 268
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Checking go.mod for go/toolchain directives:"
grep -nE '^\s*(go|toolchain)\s+' go.mod || echo "No go/toolchain directives found"
echo -e "\nInspecting top of go.mod (first 20 lines):"
head -n 20 go.mod
echo -e "\nScanning GitHub Actions workflows for Go version pins:"
find .github/workflows -type f \( -name '*.yml' -o -name '*.yaml' \) | while read -r wf; do
echo "==> $wf"
grep -nE 'setup-go|go-version' "$wf" || echo " (no Go version pin found)"
done
echo -e "\nSearching for local max/min functions:"
rg -nP -C2 '(?m)^\s*func\s+(max|min)\s*\[' --color=always || echo "No generic max/min helpers detected"
Length of output: 5528
Ensure all CI workflows pin Go ≥1.21 (maps package available in Go 1.21+)
go.mod already specifies go 1.23.12
, so the new maps
import is supported by your toolchain. A search also found no local max
/min
helpers that could shadow stdlib functions.
However, CI is currently inconsistent:
- Some workflows pin to older Go (1.22.x)
•.github/workflows/lint.yml
usesgo-version: '1.22.7'
•.github/workflows/sims.yml
usesgo-version: '^1.22.0'
- Several workflows don’t install Go explicitly, relying on the runner’s default (which may be < 1.23)
•build.yml
,container.yml
,gosec.yml
,lint-pr.yml
,pr_labeler.yml
,proto.yml
,release.yml
,semgrep.yml
,test.yml
Please update each workflow that builds, tests, or lints Go code to include an explicit setup, for example:
- uses: actions/setup-go@v5
with:
go-version: '1.23.12'
Or use a semver range:
go-version: '^1.23.0'
so that your CI always runs on a Go version compatible with the maps
package.
🤖 Prompt for AI Agents
In app/app.go around line 11 the new "maps" import requires Go ≥1.21, but CI
uses older or unspecified Go versions; update all GitHub workflow files that
build/test/lint Go (at least .github/workflows/lint.yml, sims.yml, build.yml,
container.yml, gosec.yml, lint-pr.yml, pr_labeler.yml, proto.yml, release.yml,
semgrep.yml, test.yml) to explicitly install a compatible Go version (e.g., add
an actions/setup-go@v5 step with go-version: '1.23.12' or a semver range like
'^1.23.0') so the CI runners always use Go >=1.21 (prefer matching go.mod, e.g.,
1.23.12).
contextHeight := max(blockNumber-1, | ||
// 0 is a special value in `ContextWithHeight` | ||
contextHeight = 1 | ||
} | ||
1) | ||
rsp, err := api.cronosQueryClient.ReplayBlock(rpctypes.ContextWithHeight(contextHeight), req) |
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.
max is undefined here; this will not compile. Use cmp.Max or an explicit guard.
Go has no predeclared max identifier. Unless package rpc defines a local max[T] helper (it doesn’t in this file), this line won’t build. Use cmp.Max (Go 1.21+) or a simple conditional.
Apply one of the following fixes.
Option A — modern and concise (requires Go 1.21+):
@@
-import (
+import (
+ "cmp"
@@
- // minus one to get the context of block beginning
- contextHeight := max(blockNumber-1,
- // 0 is a special value in `ContextWithHeight`
- 1)
+ // minus one to get the context of block beginning; avoid 0 (special "latest") height.
+ contextHeight := cmp.Max(blockNumber-1, int64(1))
Option B — minimal change, no new import:
-// minus one to get the context of block beginning
-contextHeight := max(blockNumber-1,
- // 0 is a special value in `ContextWithHeight`
- 1)
+// minus one to get the context of block beginning; avoid 0 (special "latest") height.
+contextHeight := blockNumber - 1
+if contextHeight < 1 {
+ contextHeight = 1
}
🤖 Prompt for AI Agents
In x/cronos/rpc/api.go around lines 282-285, the call to max(...) is undefined
and will not compile; replace it either by using cmp.Max (add import "cmp") and
set contextHeight := cmp.Max(blockNumber-1, 1), or implement an explicit guard
without new imports: compute contextHeight by checking if blockNumber-1 < 1 then
contextHeight = 1 else contextHeight = blockNumber-1; update imports accordingly
if you choose cmp.Max.
friendly ping |
Optimize code using a more modern writing style. Official support from Go, for more details visit https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize.
PR Checklist:
make
)make test
)go fmt
)golangci-lint run
)go list -json -m all | nancy sleuth
)Thank you for your code, it's appreciated! :)
Summary by CodeRabbit