refactor: slices.Sorted over map keys and one shared IsNil check

This commit is contained in:
sam committed 2026-10-10 11:14:36 +11:00
1 parent 20d6ec93db
commit f2f07fcb9b
18 files changed
+49 -175

No files matched your search

+2 -11
View File
@@ -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) {
+2 -4
View File
@@ -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
}
+2 -5
View File
@@ -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])