refactor: replace hand-rolled loops with stdlib slices and maps helpers

This commit is contained in:
Julian Sommer
2026-09-01 12:21:22 +10:00
parent 16c2425184
commit 01cf08c22b
7 changed files with 37 additions and 75 deletions
+3 -5
View File
@@ -3,6 +3,8 @@ package ebsprovider
import (
"context"
"encoding/json"
"maps"
"slices"
"strings"
"github.com/nats-io/nats.go"
@@ -138,11 +140,7 @@ type headerCarrier nats.Header
func (c headerCarrier) Get(key string) string { return nats.Header(c).Get(key) }
func (c headerCarrier) Set(key, value string) { nats.Header(c).Set(key, value) }
func (c headerCarrier) Keys() []string {
keys := make([]string, 0, len(c))
for key := range c {
keys = append(keys, key)
}
return keys
return slices.Collect(maps.Keys(c))
}
// startClientSpan opens a span for an outbound request and injects the trace
+6 -15
View File
@@ -2,6 +2,7 @@ package gateway
import (
"context"
"net"
"net/http"
"strings"
)
@@ -66,22 +67,12 @@ func parseRegistryHost(host, suffix string) (accountID, region string, ok bool)
// stripPort removes a trailing :port from a host, leaving bare IPv6 intact.
func stripPort(host string) string {
if host == "" {
return ""
if h, _, err := net.SplitHostPort(host); err == nil {
return h
}
// Bracketed IPv6 with optional port: [::1]:5000 or [::1].
if strings.HasPrefix(host, "[") {
if end := strings.IndexByte(host, ']'); end >= 0 {
return host[1:end]
}
return host
}
// Single colon means host:port; multiple colons mean bare IPv6, never a
// registry host, so leave it untouched.
if strings.Count(host, ":") == 1 {
if h, _, found := strings.Cut(host, ":"); found {
return h
}
// No port: unwrap a bracketed IPv6 literal, leave anything else as-is.
if h, ok := strings.CutPrefix(host, "["); ok {
return strings.TrimSuffix(h, "]")
}
return host
}
+10 -27
View File
@@ -1,7 +1,8 @@
package gateway
import (
"sort"
"maps"
"slices"
gateway_ecrapi "github.com/mulgadc/spinifex/spinifex/gateway/ecrapi"
gateway_ecs "github.com/mulgadc/spinifex/spinifex/gateway/ecs"
@@ -56,36 +57,18 @@ func AWSOperationInventory() map[string]ServiceOperationInventory {
}
}
func mapKeys[K ~string, V any](values map[K]V) []string {
keys := make([]string, 0, len(values))
for key := range values {
keys = append(keys, string(key))
}
sort.Strings(keys)
return keys
func mapKeys[V any](values map[string]V) []string {
return slices.Sorted(maps.Keys(values))
}
func without(values, excluded []string) []string {
exclude := make(map[string]bool, len(excluded))
for _, value := range excluded {
exclude[value] = true
}
result := make([]string, 0, len(values))
for _, value := range values {
if !exclude[value] {
result = append(result, value)
}
}
return result
return slices.DeleteFunc(slices.Clone(values), func(value string) bool {
return slices.Contains(excluded, value)
})
}
func union(left, right []string) []string {
set := make(map[string]bool, len(left)+len(right))
for _, value := range left {
set[value] = true
}
for _, value := range right {
set[value] = true
}
return mapKeys(set)
merged := slices.Concat(left, right)
slices.Sort(merged)
return slices.Compact(merged)
}
+7 -12
View File
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log/slog"
"slices"
"strings"
"time"
@@ -249,21 +250,15 @@ func recordType(t string) (uint16, error) {
// recordSetExists reports whether the zone already holds a record set for
// (label, rtype); an upsert to it replaces in place and never grows the count.
func recordSetExists(cfg nsconfig.ConfigArr, label string, rtype uint16) bool {
for _, r := range cfg.Records {
if strings.EqualFold(r.Domain, label) && r.Type == rtype {
return true
}
}
return false
return slices.ContainsFunc(cfg.Records, func(r nsconfig.Records) bool {
return strings.EqualFold(r.Domain, label) && r.Type == rtype
})
}
func hasUpsert(changes []Change) bool {
for _, c := range changes {
if c.Action == ActionUpsert {
return true
}
}
return false
return slices.ContainsFunc(changes, func(c Change) bool {
return c.Action == ActionUpsert
})
}
// northstarZoneConfig bundles the parsed service config with the writer's
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"github.com/mulgadc/spinifex/spinifex/awserrors"
@@ -90,12 +91,9 @@ func matchWebIdentityStatement(actions []string, principalRaw, conditionRaw json
}
func matchWebIdentityAction(actions []string) bool {
for _, a := range actions {
if a == stsActionAssumeRoleWithWebIdentity || a == stsActionWildcard || a == globalWildcard {
return true
}
}
return false
return slices.ContainsFunc(actions, func(a string) bool {
return a == stsActionAssumeRoleWithWebIdentity || a == stsActionWildcard || a == globalWildcard
})
}
// matchFederatedPrincipal accepts Principal.Federated as a string or array.
+4 -5
View File
@@ -131,13 +131,12 @@ func ParseNBDURI(nbdURI string) (serverType, path, host string, port int, err er
}
if after, ok := strings.CutPrefix(nbdURI, "nbd://"); ok {
hostPort := after
colonIdx := strings.LastIndex(hostPort, ":")
if colonIdx < 0 {
var portStr string
host, portStr, err = net.SplitHostPort(after)
if err != nil {
return "", "", "", 0, fmt.Errorf("missing port in NBD TCP URI: %s", nbdURI)
}
host = hostPort[:colonIdx]
port, err = strconv.Atoi(hostPort[colonIdx+1:])
port, err = strconv.Atoi(portStr)
if err != nil {
return "", "", "", 0, fmt.Errorf("invalid port in NBD URI: %s", nbdURI)
}
+3 -5
View File
@@ -3,6 +3,8 @@ package vm
import (
"fmt"
"log/slog"
"maps"
"slices"
"strconv"
"strings"
)
@@ -139,11 +141,7 @@ func (m *Manager) ENISlotMapKeys(instance *VM) []string {
}
instance.ENIRequests.Mu.Lock()
defer instance.ENIRequests.Mu.Unlock()
out := make([]string, 0, len(instance.ENIRequests.AttachedByENIID))
for eniID := range instance.ENIRequests.AttachedByENIID {
out = append(out, eniID)
}
return out
return slices.Collect(maps.Keys(instance.ENIRequests.AttachedByENIID))
}
// CleanupENITap removes the tap + OVS port for an ENI. Exported wrapper over