Skip to content

Commit a76d005

Browse files
authored
Use maps.Copy for simpler map handling (#7009)
Signed-off-by: eveneast <[email protected]>
1 parent 8524386 commit a76d005

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

admin_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"crypto/x509"
2020
"encoding/json"
2121
"fmt"
22+
"maps"
2223
"net/http"
2324
"net/http/httptest"
2425
"reflect"
@@ -335,9 +336,7 @@ func TestAdminHandlerBuiltinRouteErrors(t *testing.T) {
335336

336337
func testGetMetricValue(labels map[string]string) float64 {
337338
promLabels := prometheus.Labels{}
338-
for k, v := range labels {
339-
promLabels[k] = v
340-
}
339+
maps.Copy(promLabels, labels)
341340

342341
metric, err := adminMetrics.requestErrors.GetMetricWith(promLabels)
343342
if err != nil {
@@ -377,9 +376,7 @@ func (m *mockModule) CaddyModule() ModuleInfo {
377376

378377
func TestNewAdminHandlerRouterRegistration(t *testing.T) {
379378
originalModules := make(map[string]ModuleInfo)
380-
for k, v := range modules {
381-
originalModules[k] = v
382-
}
379+
maps.Copy(originalModules, modules)
383380
defer func() {
384381
modules = originalModules
385382
}()
@@ -479,9 +476,7 @@ func TestAdminRouterProvisioning(t *testing.T) {
479476
for _, test := range tests {
480477
t.Run(test.name, func(t *testing.T) {
481478
originalModules := make(map[string]ModuleInfo)
482-
for k, v := range modules {
483-
originalModules[k] = v
484-
}
479+
maps.Copy(originalModules, modules)
485480
defer func() {
486481
modules = originalModules
487482
}()
@@ -774,9 +769,7 @@ func (m *mockIssuerModule) CaddyModule() ModuleInfo {
774769

775770
func TestManageIdentity(t *testing.T) {
776771
originalModules := make(map[string]ModuleInfo)
777-
for k, v := range modules {
778-
originalModules[k] = v
779-
}
772+
maps.Copy(originalModules, modules)
780773
defer func() {
781774
modules = originalModules
782775
}()

caddyconfig/httpcaddyfile/directives.go

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

1717
import (
1818
"encoding/json"
19+
"maps"
1920
"net"
2021
"slices"
2122
"sort"
@@ -365,9 +366,7 @@ func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
365366
// copy existing matcher definitions so we can augment
366367
// new ones that are defined only in this scope
367368
matcherDefs := make(map[string]caddy.ModuleMap, len(h.matcherDefs))
368-
for key, val := range h.matcherDefs {
369-
matcherDefs[key] = val
370-
}
369+
maps.Copy(matcherDefs, h.matcherDefs)
371370

372371
// find and extract any embedded matcher definitions in this scope
373372
for i := 0; i < len(segments); i++ {

cmd/commandfuncs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"io/fs"
2626
"log"
27+
"maps"
2728
"net"
2829
"net/http"
2930
"os"
@@ -703,9 +704,7 @@ func AdminAPIRequest(adminAddr, method, uri string, headers http.Header, body io
703704
if body != nil {
704705
req.Header.Set("Content-Type", "application/json")
705706
}
706-
for k, v := range headers {
707-
req.Header[k] = v
708-
}
707+
maps.Copy(req.Header, headers)
709708

710709
// make an HTTP client that dials our network type, since admin
711710
// endpoints aren't always TCP, which is what the default transport

0 commit comments

Comments
 (0)