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

@@ -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,