Skip to content

Commit

Permalink
Merge pull request #19126 from callthingsoff/modernize_map_op
Browse files Browse the repository at this point in the history
all: simplify and clean up
  • Loading branch information
ahrtr authored Jan 9, 2025
2 parents 75f2ae1 + 2415c82 commit 6277dbe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 58 deletions.
30 changes: 8 additions & 22 deletions pkg/featuregate/feature_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package featuregate
import (
"flag"
"fmt"
"maps"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -171,10 +172,7 @@ func New(name string, lg *zap.Logger) *featureGate {
if lg == nil {
lg = zap.NewNop()
}
known := map[Feature]FeatureSpec{}
for k, v := range defaultFeatures {
known[k] = v
}
known := maps.Clone(defaultFeatures)

f := &featureGate{
lg: lg,
Expand Down Expand Up @@ -217,13 +215,9 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {

// Copy existing state
known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
known[k] = v
}
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) {
enabled[k] = v
}
maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))

for k, v := range m {
k := Feature(k)
Expand Down Expand Up @@ -280,9 +274,7 @@ func (f *featureGate) Add(features map[Feature]FeatureSpec) error {

// Copy existing state
known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
known[k] = v
}
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))

for name, spec := range features {
if existingSpec, found := known[name]; found {
Expand Down Expand Up @@ -336,9 +328,7 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error {
// GetAll returns a copy of the map of known feature names to feature specs.
func (f *featureGate) GetAll() map[Feature]FeatureSpec {
retval := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
retval[k] = v
}
maps.Copy(retval, f.known.Load().(map[Feature]FeatureSpec))
return retval
}

Expand Down Expand Up @@ -397,13 +387,9 @@ func (f *featureGate) KnownFeatures() []string {
func (f *featureGate) DeepCopy() MutableFeatureGate {
// Copy existing state.
known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
known[k] = v
}
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) {
enabled[k] = v
}
maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))

// Construct a new featureGate around the copied state.
// Note that specialFeatures is treated as immutable by convention,
Expand Down
14 changes: 4 additions & 10 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package report

import (
"fmt"
"maps"
"math"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -63,7 +65,7 @@ type Stats struct {
func (s *Stats) copy() Stats {
ss := *s
ss.ErrorDist = copyMap(ss.ErrorDist)
ss.Lats = copyFloats(ss.Lats)
ss.Lats = slices.Clone(ss.Lats)
return ss
}

Expand Down Expand Up @@ -124,15 +126,7 @@ func (r *report) Stats() <-chan Stats {

func copyMap(m map[string]int) (c map[string]int) {
c = make(map[string]int, len(m))
for k, v := range m {
c[k] = v
}
return c
}

func copyFloats(s []float64) (c []float64) {
c = make([]float64, len(s))
copy(c, s)
maps.Copy(c, m)
return c
}

Expand Down
5 changes: 2 additions & 3 deletions server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"testing"
"time"

Expand Down Expand Up @@ -118,9 +119,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) {
if opts["pub-key"] != "" && opts["priv-key"] != "" {
t.Run("verify-only", func(t *testing.T) {
newOpts := make(map[string]string, len(opts))
for k, v := range opts {
newOpts[k] = v
}
maps.Copy(newOpts, opts)
delete(newOpts, "priv-key")
verify, err := newTokenProviderJWT(lg, newOpts)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions server/proxy/grpcproxy/adapter/chan_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adapter

import (
"context"
"maps"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -38,9 +39,7 @@ func (ss *chanServerStream) SendHeader(md metadata.MD) error {
}
outmd := make(map[string][]string)
for _, h := range append(ss.headers, md) {
for k, v := range h {
outmd[k] = v
}
maps.Copy(outmd, h)
}
select {
case ss.headerc <- outmd:
Expand Down
5 changes: 2 additions & 3 deletions tests/framework/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"flag"
"fmt"
"maps"
"net/url"
"path"
"path/filepath"
Expand Down Expand Up @@ -634,9 +635,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
args = append(args, fmt.Sprintf("--%s=%s", flag, value))
}
envVars := map[string]string{}
for key, value := range cfg.EnvVars {
envVars[key] = value
}
maps.Copy(envVars, cfg.EnvVars)
var gofailPort int
if cfg.GoFailEnabled {
gofailPort = (i+1)*10000 + 2381
Expand Down
25 changes: 8 additions & 17 deletions tools/etcd-dump-metrics/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,17 @@

package main

import "sort"
import (
"maps"
"slices"
)

func aggSort(ss []string) (sorted []string) {
set := make(map[string]struct{})
for _, s := range ss {
set[s] = struct{}{}
}
sorted = make([]string, 0, len(set))
for k := range set {
sorted = append(sorted, k)
}
sort.Strings(sorted)
return sorted
dup := slices.Clone(ss)
slices.Sort(dup)
return slices.Compact(dup)
}

func sortMap(set map[string]struct{}) (sorted []string) {
sorted = make([]string, 0, len(set))
for k := range set {
sorted = append(sorted, k)
}
sort.Strings(sorted)
return sorted
return slices.Sorted(maps.Keys(set))
}

0 comments on commit 6277dbe

Please sign in to comment.