Skip to content

Replace repeated map-to-sorted-slice pattern in domains.go with slices.Sorted(maps.Keys(...)) #28492

@github-actions

Description

@github-actions

Problem

pkg/workflow/domains.go repeats the same 4-line "map-as-set to sorted slice" idiom 8 times:

domainMap := make(map[string]bool)
// ... fill domainMap ...
result := make([]string, 0, len(domainMap))
for d := range domainMap {
    result = append(result, d)
}
sort.Strings(result)
return result

Locations (all in pkg/workflow/domains.go)

Function Line
getEcosystemDomains (compound path) 291
getDomainsFromRuntimes 346
GetAllowedDomains 452
GetBlockedDomains 643
GetAPITargetDomains 799
mergeAPITargetDomains 877
expandAllowedDomains 956
expandAllowedDomains (second map) 983

Impact

  • Severity: Medium
  • Maintainability: New domain functions copy the same boilerplate rather than calling a utility
  • Consistency: The rest of the codebase already uses the idiomatic replacement — slices.Sorted(maps.Keys(...)) — at pkg/workflow/compiler_main_job.go:99 and pkg/cli/run_workflow_validation.go:240

Recommendation

Add "maps" and "slices" to the import block in domains.go and replace each 4-line collect-and-sort block with a single expression:

// Before (4 lines):
result := make([]string, 0, len(domainMap))
for d := range domainMap {
    result = append(result, d)
}
sort.Strings(result)

// After (1 line):
result := slices.Sorted(maps.Keys(domainMap))

The sort import can be removed from domains.go once all 8 occurrences are replaced (verify no other sort.* calls remain).

Also update the similar pattern in sortedMapKeys in pkg/workflow/map_helpers.go (which converts map[string]string keys) to use slices.Sorted(maps.Keys(m)).

Before / After Example (getEcosystemDomains)

// Before
domainMap := make(map[string]bool)
for _, component := range components {
    for _, d := range getEcosystemDomains(component) {
        domainMap[d] = true
    }
}
result := make([]string, 0, len(domainMap))
for d := range domainMap {
    result = append(result, d)
}
sort.Strings(result)
return result

// After
domainMap := make(map[string]bool)
for _, component := range components {
    for _, d := range getEcosystemDomains(component) {
        domainMap[d] = true
    }
}
return slices.Sorted(maps.Keys(domainMap))

Validation

  • go build ./pkg/workflow/ passes
  • go test ./pkg/workflow/... passes
  • golangci-lint run ./pkg/workflow/ shows no new warnings
  • No functional change — output order is identical (both approaches yield sorted unique keys)

Estimated Effort: Small

Generated by Sergo - Serena Go Expert · ● 630.9K ·

  • expires on May 2, 2026, 8:33 PM UTC

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions