Repository: helm/helm
Pull request #30850 (2025-05-09 06:34:23)

refactor: use maps.Copy for cleaner map handling
<!--  Thanks for sending a pull request!  Here are some tips for you:
1. Make sure to read the Contributing Guide before submitting your PR: https://github.com/helm/helm/blob/main/CONTRIBUTING.md
2. If this PR closes another issue, add 'closes #<issue number>' somewhere in the PR summary. GitHub will automatically close that issue when this PR gets merged. Alternatively, adding 'refs #<issue number>' will not close the issue, but help provide the reviewer more context.-->

**What this PR does / why we need it**:


Optimize code using a more modern writing style.

**Special notes for your reviewer**:

**If applicable**:
- [ ] this PR contains user facing changes (the `docs needed` label should be applied if so)
- [ ] this PR contains unit tests
- [ ] this PR has been tested for backwards compatibility



Patch:
diff --git a/pkg/action/validate.go b/pkg/action/validate.go
index 22db74041..e1021860f 100644
--- a/pkg/action/validate.go
+++ b/pkg/action/validate.go
@@ -18,6 +18,7 @@ package action
 
 import (
 	"fmt"
+	"maps"
 
 	apierrors "k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/api/meta"
@@ -194,11 +195,7 @@ func mergeAnnotations(obj runtime.Object, annotations map[string]string) error {
 // merge two maps, always taking the value on the right
 func mergeStrStrMaps(current, desired map[string]string) map[string]string {
 	result := make(map[string]string)
-	for k, v := range current {
-		result[k] = v
-	}
-	for k, desiredVal := range desired {
-		result[k] = desiredVal
-	}
+	maps.Copy(result, current)
+	maps.Copy(result, desired)
 	return result
 }
diff --git a/pkg/chart/v2/loader/load.go b/pkg/chart/v2/loader/load.go
index 7838b577f..f0905e508 100644
--- a/pkg/chart/v2/loader/load.go
+++ b/pkg/chart/v2/loader/load.go
@@ -24,6 +24,7 @@ import (
 	"fmt"
 	"io"
 	"log"
+	"maps"
 	"os"
 	"path/filepath"
 	"strings"
@@ -238,9 +239,7 @@ func LoadValues(data io.Reader) (map[string]interface{}, error) {
 // If the value is a map, the maps will be merged recursively.
 func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
 	out := make(map[string]interface{}, len(a))
-	for k, v := range a {
-		out[k] = v
-	}
+	maps.Copy(out, a)
 	for k, v := range b {
 		if v, ok := v.(map[string]interface{}); ok {
 			if bv, ok := out[k]; ok {
diff --git a/pkg/chart/v2/util/coalesce.go b/pkg/chart/v2/util/coalesce.go
index 76dfdfa1a..a3e0f5ae8 100644
--- a/pkg/chart/v2/util/coalesce.go
+++ b/pkg/chart/v2/util/coalesce.go
@@ -19,6 +19,7 @@ package util
 import (
 	"fmt"
 	"log"
+	"maps"
 
 	"github.com/mitchellh/copystructure"
 
@@ -182,9 +183,7 @@ func coalesceGlobals(printf printFn, dest, src map[string]interface{}, prefix st
 
 func copyMap(src map[string]interface{}) map[string]interface{} {
 	m := make(map[string]interface{}, len(src))
-	for k, v := range src {
-		m[k] = v
-	}
+	maps.Copy(m, src)
 	return m
 }
 
diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go
index 839ad4a31..750eb7f1d 100644
--- a/pkg/engine/engine.go
+++ b/pkg/engine/engine.go
@@ -20,6 +20,7 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"maps"
 	"path"
 	"path/filepath"
 	"regexp"
@@ -249,9 +250,7 @@ func (e Engine) initFunMap(t *template.Template) {
 	}
 
 	// Set custom template funcs
-	for k, v := range e.CustomTemplateFuncs {
-		funcMap[k] = v
-	}
+	maps.Copy(funcMap, e.CustomTemplateFuncs)
 
 	t.Funcs(funcMap)
 }
diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go
index c1f590018..a97f8f104 100644
--- a/pkg/engine/funcs.go
+++ b/pkg/engine/funcs.go
@@ -19,6 +19,7 @@ package engine
 import (
 	"bytes"
 	"encoding/json"
+	"maps"
 	"strings"
 	"text/template"
 
@@ -73,9 +74,7 @@ func funcMap() template.FuncMap {
 		},
 	}
 
-	for k, v := range extra {
-		f[k] = v
-	}
+	maps.Copy(f, extra)
 
 	return f
 }
diff --git a/pkg/storage/driver/sql.go b/pkg/storage/driver/sql.go
index c3740b9a3..46f6c6b2e 100644
--- a/pkg/storage/driver/sql.go
+++ b/pkg/storage/driver/sql.go
@@ -19,6 +19,7 @@ package driver // import "helm.sh/helm/v4/pkg/storage/driver"
 import (
 	"fmt"
 	"log/slog"
+	"maps"
 	"sort"
 	"strconv"
 	"time"
@@ -367,9 +368,7 @@ func (s *SQL) List(filter func(*rspb.Release) bool) ([]*rspb.Release, error) {
 			slog.Debug("failed to get release custom labels", "namespace", record.Namespace, "key", record.Key, slog.Any("error", err))
 			return nil, err
 		}
-		for k, v := range getReleaseSystemLabels(release) {
-			release.Labels[k] = v
-		}
+		maps.Copy(release.Labels, getReleaseSystemLabels(release))
 
 		if filter(release) {
 			releases = append(releases, release)


Test patch:
diff --git a/pkg/chart/v2/util/coalesce_test.go b/pkg/chart/v2/util/coalesce_test.go
index 3d4ee4fa8..e2c45a435 100644
--- a/pkg/chart/v2/util/coalesce_test.go
+++ b/pkg/chart/v2/util/coalesce_test.go
@@ -19,6 +19,7 @@ package util
 import (
 	"encoding/json"
 	"fmt"
+	"maps"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -144,9 +145,7 @@ func TestCoalesceValues(t *testing.T) {
 	// to CoalesceValues as argument, so that we can
 	// use it for asserting later
 	valsCopy := make(Values, len(vals))
-	for key, value := range vals {
-		valsCopy[key] = value
-	}
+	maps.Copy(valsCopy, vals)
 
 	v, err := CoalesceValues(c, vals)
 	if err != nil {
@@ -304,9 +303,7 @@ func TestMergeValues(t *testing.T) {
 	// to MergeValues as argument, so that we can
 	// use it for asserting later
 	valsCopy := make(Values, len(vals))
-	for key, value := range vals {
-		valsCopy[key] = value
-	}
+	maps.Copy(valsCopy, vals)
 
 	v, err := MergeValues(c, vals)
 	if err != nil {