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 (
"bytes"
"maps"
"net/http"
"reflect"
"strings"
@ -507,9 +508,7 @@ func copyConfig(config Configuration) *Configuration {
configCopy.Log = config.Log
configCopy.Catalog = config.Catalog
configCopy.Log.Fields = make(map[string]interface{}, len(config.Log.Fields))
for k, v := range config.Log.Fields {
configCopy.Log.Fields[k] = v
}
maps.Copy(configCopy.Log.Fields, config.Log.Fields)
configCopy.Storage = Storage{config.Storage.Type(): 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.HTTP.Headers = make(http.Header)
for k, v := range config.HTTP.Headers {
configCopy.HTTP.Headers[k] = v
}
maps.Copy(configCopy.HTTP.Headers, config.HTTP.Headers)
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.ClientAuth = config.HTTP.TLS.ClientAuth

View File

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

View File

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

View File

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

View File

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