refactor: use maps.Copy for cleaner map handling

Signed-off-by: yetyear <flite@outlook.com>
pull/30850/head
yetyear 4 months ago
parent 4d580c6b95
commit 03448d1d79

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

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

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

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

@ -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)
}

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

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

Loading…
Cancel
Save