refactor: replace map iteration with maps.Copy/Clone

Signed-off-by: whosehang <whosehang@outlook.com>
This commit is contained in:
whosehang 2025-05-03 09:24:10 +08:00
parent 95647cba1d
commit fea3638384
5 changed files with 11 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package configuration
import ( import (
"bytes" "bytes"
"maps"
"net/http" "net/http"
"reflect" "reflect"
"strings" "strings"
@ -507,9 +508,7 @@ func copyConfig(config Configuration) *Configuration {
configCopy.Log = config.Log configCopy.Log = config.Log
configCopy.Catalog = config.Catalog configCopy.Catalog = config.Catalog
configCopy.Log.Fields = make(map[string]interface{}, len(config.Log.Fields)) configCopy.Log.Fields = make(map[string]interface{}, len(config.Log.Fields))
for k, v := range config.Log.Fields { maps.Copy(configCopy.Log.Fields, config.Log.Fields)
configCopy.Log.Fields[k] = v
}
configCopy.Storage = Storage{config.Storage.Type(): Parameters{}} configCopy.Storage = Storage{config.Storage.Type(): Parameters{}}
for k, v := range config.Storage.Parameters() { for k, v := range config.Storage.Parameters() {
@ -528,9 +527,7 @@ func copyConfig(config Configuration) *Configuration {
configCopy.Notifications.Endpoints = append(configCopy.Notifications.Endpoints, config.Notifications.Endpoints...) configCopy.Notifications.Endpoints = append(configCopy.Notifications.Endpoints, config.Notifications.Endpoints...)
configCopy.HTTP.Headers = make(http.Header) configCopy.HTTP.Headers = make(http.Header)
for k, v := range config.HTTP.Headers { maps.Copy(configCopy.HTTP.Headers, config.HTTP.Headers)
configCopy.HTTP.Headers[k] = v
}
configCopy.HTTP.TLS.ClientCAs = make([]string, 0, len(config.HTTP.TLS.ClientCAs)) configCopy.HTTP.TLS.ClientCAs = make([]string, 0, len(config.HTTP.TLS.ClientCAs))
configCopy.HTTP.TLS.ClientCAs = append(configCopy.HTTP.TLS.ClientCAs, config.HTTP.TLS.ClientCAs...) configCopy.HTTP.TLS.ClientCAs = append(configCopy.HTTP.TLS.ClientCAs, config.HTTP.TLS.ClientCAs...)
configCopy.HTTP.TLS.ClientAuth = config.HTTP.TLS.ClientAuth configCopy.HTTP.TLS.ClientAuth = config.HTTP.TLS.ClientAuth

View File

@ -3,6 +3,7 @@ package auth
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"maps"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -38,9 +39,7 @@ func (w *testAuthenticationWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Re
auth := r.Header.Get("Authorization") auth := r.Header.Get("Authorization")
if auth == "" || !w.authCheck(auth) { if auth == "" || !w.authCheck(auth) {
h := rw.Header() h := rw.Header()
for k, values := range w.headers { maps.Copy(h, w.headers)
h[k] = values
}
rw.WriteHeader(http.StatusUnauthorized) rw.WriteHeader(http.StatusUnauthorized)
return return
} }

View File

@ -2,6 +2,7 @@ package dcontext
import ( import (
"context" "context"
"maps"
"sync" "sync"
"github.com/google/uuid" "github.com/google/uuid"
@ -52,9 +53,7 @@ type stringMapContext struct {
// supports string keys. // supports string keys.
func WithValues(ctx context.Context, m map[string]interface{}) context.Context { func WithValues(ctx context.Context, m map[string]interface{}) context.Context {
mo := make(map[string]interface{}, len(m)) // make our own copy. mo := make(map[string]interface{}, len(m)) // make our own copy.
for k, v := range m { maps.Copy(mo, m)
mo[k] = v
}
return stringMapContext{ return stringMapContext{
Context: ctx, Context: ctx,

View File

@ -1,6 +1,7 @@
package notifications package notifications
import ( import (
"maps"
"net/http" "net/http"
"time" "time"
@ -92,7 +93,5 @@ func (e *Endpoint) ReadMetrics(em *EndpointMetrics) {
*em = e.metrics.EndpointMetrics *em = e.metrics.EndpointMetrics
// Map still need to copied in a threadsafe manner. // Map still need to copied in a threadsafe manner.
em.Statuses = make(map[string]int) em.Statuses = make(map[string]int)
for k, v := range e.metrics.Statuses { maps.Copy(em.Statuses, e.metrics.Statuses)
em.Statuses[k] = v
}
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"maps"
"net/http" "net/http"
"net/url" "net/url"
"sort" "sort"
@ -137,9 +138,7 @@ func (app *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.responseMap[request.String()] = responses[1:] app.responseMap[request.String()] = responses[1:]
responseHeader := w.Header() responseHeader := w.Header()
for k, v := range response.Headers { maps.Copy(responseHeader, response.Headers)
responseHeader[k] = v
}
w.WriteHeader(response.StatusCode) w.WriteHeader(response.StatusCode)