refactor: slices.Sorted over map keys and one shared IsNil check
This commit is contained in:
1 parent
20d6ec93db
commit
f2f07fcb9b
18 files changed
+49
-175
No files matched your search
@@ -3,6 +3,7 @@ package archiegateway
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -57,12 +58,7 @@ func mergeModelRefs(configured map[string]string, catalogs ...[]string) []string
|
||||
}
|
||||
}
|
||||
|
||||
models := make([]string, 0, len(unique))
|
||||
for ref := range unique {
|
||||
models = append(models, ref)
|
||||
}
|
||||
slices.Sort(models)
|
||||
return models
|
||||
return slices.Sorted(maps.Keys(unique))
|
||||
}
|
||||
|
||||
func newChatModelManager(configured map[string]string, catalogs ...[]string) *chatModelManager {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -135,11 +136,7 @@ func (b *server) disconnectAbsentMCPServers(ctx context.Context, desired map[str
|
||||
// applyDesiredMCPServers connects, reconnects or restarts the servers the
|
||||
// stored document names, skipping the ones already running unchanged.
|
||||
func (b *server) applyDesiredMCPServers(ctx context.Context, desired map[string]config.MCPServer, state mcpRegistryState) []error {
|
||||
names := make([]string, 0, len(desired))
|
||||
for name := range desired {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
names := slices.Sorted(maps.Keys(desired))
|
||||
|
||||
var problems []error
|
||||
for _, name := range names {
|
||||
|
||||
@@ -5,7 +5,8 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"sort"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -236,11 +237,7 @@ func formatInstallFailure(err error) string {
|
||||
}
|
||||
|
||||
func formatVersionChanges(result releaseupdate.Result) string {
|
||||
ids := make([]string, 0, len(result.Installed))
|
||||
for id := range result.Installed {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
ids := slices.Sorted(maps.Keys(result.Installed))
|
||||
parts := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
installed := result.Installed[id]
|
||||
@@ -266,11 +263,7 @@ func formatPendingReport(report releaseupdate.Report, running map[string]string)
|
||||
return formatHealthyReport(report, report.Verify(running))
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(report.Previous))
|
||||
for id := range report.Previous {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
ids := slices.Sorted(maps.Keys(report.Previous))
|
||||
versions := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
versions = append(versions, fmt.Sprintf("%s %s", id, report.Previous[id]))
|
||||
|
||||
@@ -2,7 +2,8 @@ package binding
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
@@ -113,10 +114,5 @@ func (b Binding) RepositoryFrom(values map[string]any) (owner, repo string, ok b
|
||||
}
|
||||
|
||||
func sortedKeys[V any](m map[string]V) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
return slices.Sorted(maps.Keys(m))
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package image
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"maps"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
)
|
||||
|
||||
// ErrDuplicate is returned when a provider name is already registered.
|
||||
@@ -28,7 +30,7 @@ func NewRegistry() *Registry {
|
||||
// Registration fails on a nil provider, an invalid Capability, or a
|
||||
// duplicate name.
|
||||
func (r *Registry) Register(p Provider) error {
|
||||
if isNilProvider(p) {
|
||||
if lifecycle.IsNil(p) {
|
||||
return errors.New("image: nil provider")
|
||||
}
|
||||
if err := p.Capability().Validate(); err != nil {
|
||||
@@ -56,12 +58,7 @@ func (r *Registry) Get(name string) (Provider, bool) {
|
||||
func (r *Registry) Names() []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
names := make([]string, 0, len(r.providers))
|
||||
for name := range r.providers {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
return slices.Sorted(maps.Keys(r.providers))
|
||||
}
|
||||
|
||||
// ByClass returns the sorted names of registered providers matching class.
|
||||
@@ -77,14 +74,3 @@ func (r *Registry) ByClass(class ProviderClass) []string {
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
|
||||
func isNilProvider(p Provider) bool {
|
||||
v := reflect.ValueOf(p)
|
||||
switch v.Kind() {
|
||||
case reflect.Invalid:
|
||||
return true
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sync"
|
||||
@@ -124,12 +125,7 @@ func (r *Registry[E, H]) Get(name string) (E, bool) {
|
||||
func (r *Registry[E, H]) Names() []string {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
names := make([]string, 0, len(r.engines))
|
||||
for name := range r.engines {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
return slices.Sorted(maps.Keys(r.engines))
|
||||
}
|
||||
|
||||
// Start starts every engine in registration order. An engine whose Start
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -323,11 +324,7 @@ func renderSetting(resource SettingResource, descriptor SettingDescriptor) strin
|
||||
}
|
||||
return fmt.Sprintf("%s (version %d):\n%s", resource.Kind, resource.Version, value)
|
||||
}
|
||||
names := make([]string, 0, len(resource.Value))
|
||||
for name := range resource.Value {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
names := slices.Sorted(maps.Keys(resource.Value))
|
||||
lines := []string{fmt.Sprintf("%s (version %d):", resource.Kind, resource.Version)}
|
||||
for _, name := range names {
|
||||
field := descriptor.Fields[name]
|
||||
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"maps"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -34,12 +36,7 @@ func BlastRadius(fsys fs.FS, changed []string) ([]string, error) {
|
||||
delete(reached, file)
|
||||
}
|
||||
|
||||
radius := make([]string, 0, len(reached))
|
||||
for file := range reached {
|
||||
radius = append(radius, file)
|
||||
}
|
||||
sort.Strings(radius)
|
||||
return radius, nil
|
||||
return slices.Sorted(maps.Keys(reached)), nil
|
||||
}
|
||||
|
||||
// ExposureCounts returns, for each file BlastRadius reaches, how many changed
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/domain/stableid"
|
||||
)
|
||||
|
||||
@@ -58,7 +58,7 @@ func NewManager() *Manager {
|
||||
// none. It refuses nil or unnamed providers, bad identifiers, missing
|
||||
// factories, duplicates and types another provider already owns.
|
||||
func (m *Manager) Register(provider StepTypeProvider) error {
|
||||
if isNilProvider(provider) {
|
||||
if lifecycle.IsNil(provider) {
|
||||
return errors.New("workflow step type provider is nil")
|
||||
}
|
||||
name := provider.Name()
|
||||
@@ -123,12 +123,7 @@ func (m *Manager) Settings() map[string]any {
|
||||
// StepTypes returns every registered step type name, sorted.
|
||||
func (m *Manager) StepTypes() []string {
|
||||
registry := m.Registry()
|
||||
names := make([]string, 0, len(registry))
|
||||
for name := range registry {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
return slices.Sorted(maps.Keys(registry))
|
||||
}
|
||||
|
||||
// Providers returns the registered provider names, sorted.
|
||||
@@ -137,17 +132,3 @@ func (m *Manager) Providers() []string {
|
||||
defer m.mu.Unlock()
|
||||
return slices.Sorted(slices.Values(m.providers))
|
||||
}
|
||||
|
||||
// isNilProvider catches a nil interface and a typed nil pointer alike: a
|
||||
// provider cannot answer Name() in either case, so it is refused rather than
|
||||
// dereferenced. Mirrors internal/domain/memory's engine check.
|
||||
func isNilProvider(provider StepTypeProvider) bool {
|
||||
value := reflect.ValueOf(provider)
|
||||
switch value.Kind() {
|
||||
case reflect.Invalid:
|
||||
return true
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package setup
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -42,11 +43,7 @@ func (s *EnvFileSink) Commit() error {
|
||||
if len(s.entries) == 0 {
|
||||
return nil
|
||||
}
|
||||
keys := make([]string, 0, len(s.entries))
|
||||
for k := range s.entries {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
keys := slices.Sorted(maps.Keys(s.entries))
|
||||
|
||||
var out []string
|
||||
existing, err := readEnvLines(s.path)
|
||||
|
||||
@@ -6,7 +6,9 @@ package tomlwrite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -70,11 +72,7 @@ func Apply(src []byte, edits []Edit) ([]byte, error) {
|
||||
}
|
||||
a.indexTables()
|
||||
|
||||
tables := make([]string, 0, len(order))
|
||||
for t := range order {
|
||||
tables = append(tables, t)
|
||||
}
|
||||
sort.Strings(tables)
|
||||
tables := slices.Sorted(maps.Keys(order))
|
||||
for _, t := range tables {
|
||||
a.resolveTable(t)
|
||||
}
|
||||
|
||||
+2
-15
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/domain/stableid"
|
||||
)
|
||||
|
||||
@@ -387,20 +387,7 @@ func (h *Host) ensureModulesLocked() {
|
||||
}
|
||||
|
||||
func isNilModule(module Module) bool {
|
||||
return isNilInterface(module)
|
||||
}
|
||||
|
||||
func isNilInterface(value any) bool {
|
||||
if value == nil {
|
||||
return true
|
||||
}
|
||||
reflected := reflect.ValueOf(value)
|
||||
switch reflected.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return reflected.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return lifecycle.IsNil(module)
|
||||
}
|
||||
|
||||
func safeManifest(module Module) (manifest Manifest, err error) {
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
@@ -106,7 +106,7 @@ func (p *Provider) Start(ctx context.Context) error {
|
||||
if p.name == "" || p.segment == "" {
|
||||
return errors.New("MCP tool provider name is required")
|
||||
}
|
||||
if isNilTransport(p.transport) {
|
||||
if lifecycle.IsNil(p.transport) {
|
||||
return errors.New("MCP tool provider transport is nil")
|
||||
}
|
||||
if err := p.Manifest().Validate(); err != nil {
|
||||
@@ -198,7 +198,7 @@ func (p *Provider) Stop(ctx context.Context) error {
|
||||
return fmt.Errorf("remove MCP media dir %q: %w", mediaDir, err)
|
||||
}
|
||||
}
|
||||
if isNilTransport(p.transport) {
|
||||
if lifecycle.IsNil(p.transport) {
|
||||
return nil
|
||||
}
|
||||
if err := p.transport.Stop(ctx); err != nil {
|
||||
@@ -428,16 +428,3 @@ func isASCIILetter(char rune) bool {
|
||||
func isASCIIDigit(char rune) bool {
|
||||
return char >= '0' && char <= '9'
|
||||
}
|
||||
|
||||
func isNilTransport(transport LifecycleTransport) bool {
|
||||
if transport == nil {
|
||||
return true
|
||||
}
|
||||
value := reflect.ValueOf(transport)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,12 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"maps"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
@@ -100,7 +101,7 @@ func (r *Registry) Register(engine Engine) error {
|
||||
// prepareRegistration validates an engine and builds its registration, the
|
||||
// validation Register, Add and Replace all share.
|
||||
func prepareRegistration(engine Engine, optional bool) (registration, error) {
|
||||
if isNilEngine(engine) {
|
||||
if lifecycle.IsNil(engine) {
|
||||
return registration{}, errors.New("tool provider is nil")
|
||||
}
|
||||
manifest, err := safeManifest(engine)
|
||||
@@ -390,11 +391,7 @@ func cloneRegistrations(source map[string]registration) map[string]registration
|
||||
}
|
||||
|
||||
func dependencyOrder(providers map[string]registration) ([]string, error) {
|
||||
ids := make([]string, 0, len(providers))
|
||||
for id := range providers {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
slices.Sort(ids)
|
||||
ids := slices.Sorted(maps.Keys(providers))
|
||||
|
||||
indegree := make(map[string]int, len(providers))
|
||||
dependents := make(map[string][]string, len(providers))
|
||||
@@ -486,19 +483,6 @@ func cloneManifest(manifest plugin.Manifest) plugin.Manifest {
|
||||
return clone
|
||||
}
|
||||
|
||||
func isNilEngine(engine Engine) bool {
|
||||
if engine == nil {
|
||||
return true
|
||||
}
|
||||
value := reflect.ValueOf(engine)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func safeManifest(engine Engine) (manifest plugin.Manifest, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
)
|
||||
|
||||
@@ -30,12 +31,7 @@ func (r *Registry) Has(id string) bool {
|
||||
func (r *Registry) RegisteredIDs() []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
ids := make([]string, 0, len(r.providers))
|
||||
for id := range r.providers {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
slices.Sort(ids)
|
||||
return ids
|
||||
return slices.Sorted(maps.Keys(r.providers))
|
||||
}
|
||||
|
||||
// RunningIDs returns the manifest IDs of the running providers, in start
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
@@ -147,16 +147,7 @@ func (s *Server) chatReady(w http.ResponseWriter) (*ChatService, bool) {
|
||||
}
|
||||
|
||||
func chatUpdateServiceConfigured(updates ChatUpdateService) bool {
|
||||
if updates == nil {
|
||||
return false
|
||||
}
|
||||
value := reflect.ValueOf(updates)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return !value.IsNil()
|
||||
default:
|
||||
return true
|
||||
}
|
||||
return !lifecycle.IsNil(updates)
|
||||
}
|
||||
|
||||
func (s *Server) handleChatSessions(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -107,10 +107,8 @@ func mergeComponents(history []string, live []logging.Entry) []string {
|
||||
seen[component] = struct{}{}
|
||||
}
|
||||
}
|
||||
components := make([]string, 0, len(seen))
|
||||
for component := range seen {
|
||||
components = append(components, component)
|
||||
}
|
||||
// Non-nil so the JSON response carries [] rather than null.
|
||||
components := slices.AppendSeq(make([]string, 0, len(seen)), maps.Keys(seen))
|
||||
slices.Sort(components)
|
||||
return components
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"reflect"
|
||||
"slices"
|
||||
"time"
|
||||
@@ -59,11 +60,7 @@ func (s *Server) registerSSEConn() (<-chan liveUpdate, []liveUpdate, <-chan stru
|
||||
s.conns = make(map[*liveSubscriber]struct{})
|
||||
}
|
||||
s.conns[subscriber] = struct{}{}
|
||||
keys := make([]string, 0, len(s.latest))
|
||||
for key := range s.latest {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
slices.Sort(keys)
|
||||
keys := slices.Sorted(maps.Keys(s.latest))
|
||||
snapshot := make([]liveUpdate, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
snapshot = append(snapshot, s.latest[key])
|
||||
|
||||
Reference in new issue
Block a user