Conversation
Replace imperative var+append loops with declarative sliceutil.Filter and sliceutil.Map calls in pkg/cli. - mcp_list_tools.go: completeMCPListToolsArgs uses sliceutil.Filter for filtering common MCP server names by completion prefix - audit_report_analysis.go: generateFindings uses sliceutil.Map to transform MissingToolReport slice to string slice (first 3 tools) - firewall_log.go: analyzeFirewallLogs uses sliceutil.Filter to select firewall log files by name pattern (adds sliceutil import) All three changes reduce mutable state by replacing var+append patterns with direct immutable assignments using the existing sliceutil helpers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR replaces three imperative var + append slice-building patterns with declarative sliceutil.Filter and sliceutil.Map calls in pkg/cli, improving code clarity and consistency with existing codebase patterns.
Changes:
- Replaced manual filter loop in
completeMCPListToolsArgswithsliceutil.Filter - Replaced manual filter loop in
analyzeFirewallLogswithsliceutil.Filter(adding thesliceutilimport) - Replaced manual index-bounded loop in
generateFindingswithsliceutil.Mapon a capped slice
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pkg/cli/mcp_list_tools.go |
Replaced imperative filter loop with sliceutil.Filter for MCP server name completion |
pkg/cli/firewall_log.go |
Replaced imperative filter loop with sliceutil.Filter for firewall log file matching; added sliceutil import |
pkg/cli/audit_report_analysis.go |
Replaced pre-allocated loop with sliceutil.Map on a capped slice for extracting tool names |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| for i := 0; i < len(processedRun.MissingTools) && i < 3; i++ { | ||
| toolNames = append(toolNames, processedRun.MissingTools[i].Tool) | ||
| } | ||
| toolNames := sliceutil.Map(processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], func(t MissingToolReport) string { return t.Tool }) |
There was a problem hiding this comment.
This sliceutil.Map call is written as a single long line (~150 chars), while the sliceutil.Map call just above on lines 94-96 in the same function uses a multi-line format with the callback body on its own line. For consistency within this function, consider breaking this into multiple lines to match the style of the Map call on lines 94-96.
| toolNames := sliceutil.Map(processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], func(t MissingToolReport) string { return t.Tool }) | |
| toolNames := sliceutil.Map( | |
| processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], | |
| func(t MissingToolReport) string { | |
| return t.Tool | |
| }, | |
| ) |
Functional/Immutability Enhancements - Package:
pkg/cliApplies moderate, tasteful functional/immutability improvements to three files in
pkg/cli, replacing imperativevar+appendpatterns with declarativesliceutil.Filterandsliceutil.Mapcalls.Round-Robin Progress: First pass over
pkg/cli. Next run will process:pkg/consoleSummary of Changes
Transformative Data Operations — 3 sites improved
1.
pkg/cli/mcp_list_tools.go—completeMCPListToolsArgsReplaced a manual filter loop over
commonMCPServerNameswithsliceutil.Filter(the file already importedsliceutil):2.
pkg/cli/audit_report_analysis.go—generateFindingsReplaced pre-allocation + imperative index loop for first-3 tool names with a single
sliceutil.Mapcall on a capped slice (file already importedsliceutil):3.
pkg/cli/firewall_log.go—analyzeFirewallLogsReplaced a manual filter loop over glob results with
sliceutil.Filter(addssliceutilimport):Benefits
var x []Tmutation — values are now assigned once and not modifiedsliceutilhelpers already present in the file/codebaseTesting
TestGenerateFindings/TestGenerateRecommendations— passTestParseFirewallLog*/TestAnalyzeFirewallLogs*— passmake test-unit— all unit tests passmake fmt— code formatted correctlyReferences: