feat: modernise to Go 1.26 — iterators, slices.Sorted, maps.Keys
Some checks failed
Security Scan / security (push) Successful in 10s
Test / test (push) Failing after 37s

- List() now returns deterministic alphabetical order via slices.Sorted(maps.Keys())
- Add All() iter.Seq2[string, Backend] for iterator-based registry access
- Use slices.Insert for prepend in Discover
- Use maps.Values in Default() fallback
- Remove redundant sort.Strings in tests (List() is now sorted)

Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-02-23 05:02:24 +00:00
parent b65283bae7
commit 6333fa7b6d
3 changed files with 21 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"slices"
)
// DiscoveredModel describes a model directory found by Discover.
@ -39,7 +40,7 @@ func Discover(baseDir string) ([]DiscoveredModel, error) {
// Also check baseDir itself (in case it's a model directory).
if m, ok := probeModelDir(baseDir); ok {
// Prepend so the base dir appears first.
models = append([]DiscoveredModel{m}, models...)
models = slices.Insert(models, 0, m)
}
return models, nil

View file

@ -67,6 +67,8 @@ import (
"context"
"fmt"
"iter"
"maps"
"slices"
"sync"
"time"
)
@ -212,15 +214,24 @@ func Get(name string) (Backend, bool) {
return b, ok
}
// List returns the names of all registered backends.
// List returns the names of all registered backends in alphabetical order.
func List() []string {
backendsMu.RLock()
defer backendsMu.RUnlock()
names := make([]string, 0, len(backends))
for name := range backends {
names = append(names, name)
return slices.Sorted(maps.Keys(backends))
}
// All returns an iterator over all registered backends.
func All() iter.Seq2[string, Backend] {
return func(yield func(string, Backend) bool) {
backendsMu.RLock()
defer backendsMu.RUnlock()
for k, v := range backends {
if !yield(k, v) {
return
}
}
}
return names
}
// Default returns the first available backend.
@ -236,7 +247,7 @@ func Default() (Backend, error) {
}
}
// Fall back to any available
for _, b := range backends {
for b := range maps.Values(backends) {
if b.Available() {
return b, nil
}

View file

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"iter"
"sort"
"sync"
"testing"
@ -96,9 +95,7 @@ func TestRegister_Good_Multiple(t *testing.T) {
Register(&stubBackend{name: "beta", available: true})
Register(&stubBackend{name: "gamma", available: true})
names := List()
sort.Strings(names)
assert.Equal(t, []string{"alpha", "beta", "gamma"}, names)
assert.Equal(t, []string{"alpha", "beta", "gamma"}, List())
}
func TestRegister_Ugly_Overwrites(t *testing.T) {
@ -150,9 +147,7 @@ func TestList_Good_Populated(t *testing.T) {
Register(&stubBackend{name: "a", available: true})
Register(&stubBackend{name: "b", available: true})
names := List()
sort.Strings(names)
assert.Equal(t, []string{"a", "b"}, names)
assert.Equal(t, []string{"a", "b"}, List())
}
// --- Default ---