Skip to content

Commit 6277dbe

Browse files
authored
Merge pull request #19126 from callthingsoff/modernize_map_op
all: simplify and clean up
2 parents 75f2ae1 + 2415c82 commit 6277dbe

File tree

6 files changed

+26
-58
lines changed

6 files changed

+26
-58
lines changed

pkg/featuregate/feature_gate.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package featuregate
1818
import (
1919
"flag"
2020
"fmt"
21+
"maps"
2122
"sort"
2223
"strconv"
2324
"strings"
@@ -171,10 +172,7 @@ func New(name string, lg *zap.Logger) *featureGate {
171172
if lg == nil {
172173
lg = zap.NewNop()
173174
}
174-
known := map[Feature]FeatureSpec{}
175-
for k, v := range defaultFeatures {
176-
known[k] = v
177-
}
175+
known := maps.Clone(defaultFeatures)
178176

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

218216
// Copy existing state
219217
known := map[Feature]FeatureSpec{}
220-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
221-
known[k] = v
222-
}
218+
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
223219
enabled := map[Feature]bool{}
224-
for k, v := range f.enabled.Load().(map[Feature]bool) {
225-
enabled[k] = v
226-
}
220+
maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
227221

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

281275
// Copy existing state
282276
known := map[Feature]FeatureSpec{}
283-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
284-
known[k] = v
285-
}
277+
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
286278

287279
for name, spec := range features {
288280
if existingSpec, found := known[name]; found {
@@ -336,9 +328,7 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error {
336328
// GetAll returns a copy of the map of known feature names to feature specs.
337329
func (f *featureGate) GetAll() map[Feature]FeatureSpec {
338330
retval := map[Feature]FeatureSpec{}
339-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
340-
retval[k] = v
341-
}
331+
maps.Copy(retval, f.known.Load().(map[Feature]FeatureSpec))
342332
return retval
343333
}
344334

@@ -397,13 +387,9 @@ func (f *featureGate) KnownFeatures() []string {
397387
func (f *featureGate) DeepCopy() MutableFeatureGate {
398388
// Copy existing state.
399389
known := map[Feature]FeatureSpec{}
400-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
401-
known[k] = v
402-
}
390+
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
403391
enabled := map[Feature]bool{}
404-
for k, v := range f.enabled.Load().(map[Feature]bool) {
405-
enabled[k] = v
406-
}
392+
maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
407393

408394
// Construct a new featureGate around the copied state.
409395
// Note that specialFeatures is treated as immutable by convention,

pkg/report/report.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package report
1818

1919
import (
2020
"fmt"
21+
"maps"
2122
"math"
23+
"slices"
2224
"sort"
2325
"strings"
2426
"time"
@@ -63,7 +65,7 @@ type Stats struct {
6365
func (s *Stats) copy() Stats {
6466
ss := *s
6567
ss.ErrorDist = copyMap(ss.ErrorDist)
66-
ss.Lats = copyFloats(ss.Lats)
68+
ss.Lats = slices.Clone(ss.Lats)
6769
return ss
6870
}
6971

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

125127
func copyMap(m map[string]int) (c map[string]int) {
126128
c = make(map[string]int, len(m))
127-
for k, v := range m {
128-
c[k] = v
129-
}
130-
return c
131-
}
132-
133-
func copyFloats(s []float64) (c []float64) {
134-
c = make([]float64, len(s))
135-
copy(c, s)
129+
maps.Copy(c, m)
136130
return c
137131
}
138132

server/auth/jwt_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21+
"maps"
2122
"testing"
2223
"time"
2324

@@ -118,9 +119,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) {
118119
if opts["pub-key"] != "" && opts["priv-key"] != "" {
119120
t.Run("verify-only", func(t *testing.T) {
120121
newOpts := make(map[string]string, len(opts))
121-
for k, v := range opts {
122-
newOpts[k] = v
123-
}
122+
maps.Copy(newOpts, opts)
124123
delete(newOpts, "priv-key")
125124
verify, err := newTokenProviderJWT(lg, newOpts)
126125
if err != nil {

server/proxy/grpcproxy/adapter/chan_stream.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package adapter
1616

1717
import (
1818
"context"
19+
"maps"
1920

2021
"google.golang.org/grpc"
2122
"google.golang.org/grpc/codes"
@@ -38,9 +39,7 @@ func (ss *chanServerStream) SendHeader(md metadata.MD) error {
3839
}
3940
outmd := make(map[string][]string)
4041
for _, h := range append(ss.headers, md) {
41-
for k, v := range h {
42-
outmd[k] = v
43-
}
42+
maps.Copy(outmd, h)
4443
}
4544
select {
4645
case ss.headerc <- outmd:

tests/framework/e2e/cluster.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"flag"
2121
"fmt"
22+
"maps"
2223
"net/url"
2324
"path"
2425
"path/filepath"
@@ -634,9 +635,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
634635
args = append(args, fmt.Sprintf("--%s=%s", flag, value))
635636
}
636637
envVars := map[string]string{}
637-
for key, value := range cfg.EnvVars {
638-
envVars[key] = value
639-
}
638+
maps.Copy(envVars, cfg.EnvVars)
640639
var gofailPort int
641640
if cfg.GoFailEnabled {
642641
gofailPort = (i+1)*10000 + 2381

tools/etcd-dump-metrics/utils.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,17 @@
1414

1515
package main
1616

17-
import "sort"
17+
import (
18+
"maps"
19+
"slices"
20+
)
1821

1922
func aggSort(ss []string) (sorted []string) {
20-
set := make(map[string]struct{})
21-
for _, s := range ss {
22-
set[s] = struct{}{}
23-
}
24-
sorted = make([]string, 0, len(set))
25-
for k := range set {
26-
sorted = append(sorted, k)
27-
}
28-
sort.Strings(sorted)
29-
return sorted
23+
dup := slices.Clone(ss)
24+
slices.Sort(dup)
25+
return slices.Compact(dup)
3026
}
3127

3228
func sortMap(set map[string]struct{}) (sorted []string) {
33-
sorted = make([]string, 0, len(set))
34-
for k := range set {
35-
sorted = append(sorted, k)
36-
}
37-
sort.Strings(sorted)
38-
return sorted
29+
return slices.Sorted(maps.Keys(set))
3930
}

0 commit comments

Comments
 (0)