Skip to content

[fp-enhancer] refactor(actionpins): use slices/maps stdlib for declarative transformations#28020

Merged
pelikhan merged 1 commit intomainfrom
fp-enhancer/actionpins-functional-improvements-b33980e1a782e00a
Apr 23, 2026
Merged

[fp-enhancer] refactor(actionpins): use slices/maps stdlib for declarative transformations#28020
pelikhan merged 1 commit intomainfrom
fp-enhancer/actionpins-functional-improvements-b33980e1a782e00a

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Summary

Applied moderate functional/immutability improvements to pkg/actionpins (round-robin pass 1/22).

Changes in pkg/actionpins/actionpins.go:

1. Declarative map-to-slice conversion

Replaced an imperative make + append loop with slices.Collect(maps.Values(...)):

// Before
pins := make([]ActionPin, 0, len(data.Entries))
for _, pin := range data.Entries {
    pins = append(pins, pin)
}

// After
pins := slices.Collect(maps.Values(data.Entries))

2. slices.SortFunc with cmp.Compare (two call sites)

Replaced sort.Slice (index-based closure) with slices.SortFunc (value-based closure) for cleaner, safer comparisons:

// Before
sort.Slice(pins, func(i, j int) bool {
    if pins[i].Version != pins[j].Version {
        return pins[i].Version > pins[j].Version
    }
    return pins[i].Repo < pins[j].Repo
})

// After
slices.SortFunc(pins, func(a, b ActionPin) int {
    if a.Version != b.Version {
        return cmp.Compare(b.Version, a.Version)
    }
    return cmp.Compare(a.Repo, b.Repo)
})

3. Idiomatic nil-check for container pins

Simplified the if/else nil-guard to a positive-first pattern:

// Before
if data.Containers == nil {
    cachedContainerPins = make(map[string]ContainerPin)
} else {
    cachedContainerPins = data.Containers
}

// After
cachedContainerPins = data.Containers
if cachedContainerPins == nil {
    cachedContainerPins = make(map[string]ContainerPin)
}

Benefits

  • Clarity: slices.SortFunc receives values directly (not indices), making comparisons easier to read and review
  • Safety: Eliminates accidental index-capture bugs in sort closures
  • Consistency: Aligns with existing maps.Copy, slices.Collect, maps.Keys usage elsewhere in the codebase

Testing

  • ✅ All pkg/actionpins tests pass
  • go vet clean
  • go build ./pkg/actionpins/ succeeds
  • ✅ No behavior changes

Round-Robin Progress

  • Processed: pkg/actionpins (1 of 22 packages)
  • Next run: pkg/agentdrain

References: §24828978425

Generated by Functional Pragmatist · ● 1.8M ·

  • expires on Apr 24, 2026, 10:09 AM UTC

…mations

Replace imperative sort.Slice and manual map-to-slice loops with
stdlib declarative equivalents:

- Use slices.Collect(maps.Values(...)) to convert map entries to a
  slice, replacing the explicit append loop
- Use slices.SortFunc with cmp.Compare for cleaner sort comparisons
  in both getActionPins and buildByRepoIndex
- Simplify nil container-pin check to idiomatic positive-first form

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review April 23, 2026 12:07
Copilot AI review requested due to automatic review settings April 23, 2026 12:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors pkg/actionpins to use newer Go stdlib maps/slices helpers for more declarative transformations and sorting, without intended behavior changes.

Changes:

  • Replace imperative map-to-slice loop with slices.Collect(maps.Values(...)).
  • Replace sort.Slice comparators with slices.SortFunc + cmp.Compare (including semver-descending sort).
  • Simplify container pin nil initialization logic.
Show a summary per file
File Description
pkg/actionpins/actionpins.go Refactors pin collection and sorting to maps/slices/cmp, plus minor nil-guard simplification.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@github-actions github-actions Bot mentioned this pull request Apr 23, 2026
@pelikhan pelikhan merged commit 327ad38 into main Apr 23, 2026
37 of 40 checks passed
@pelikhan pelikhan deleted the fp-enhancer/actionpins-functional-improvements-b33980e1a782e00a branch April 23, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants