all: simplify and clean up
This patch modernizes the for-range-loop code to copy a map with "maps.Clone" and "maps.Copy", also eliminates "copyFloats" with "slices.Clone". Also simplify "aggSort" and "sortMap" with slices and maps functions. Signed-off-by: Jes Cok <xigua67damn@gmail.com>
This commit is contained in:
@ -18,6 +18,7 @@ package featuregate
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"maps"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -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,
|
||||
@ -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)
|
||||
@ -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 {
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
@ -18,7 +18,9 @@ package report
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -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 {
|
||||
|
@ -16,6 +16,7 @@ package adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maps"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -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:
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@ -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
|
||||
|
@ -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))
|
||||
}
|
||||
|
Reference in New Issue
Block a user