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 ( import (
"fmt" "fmt"
"maps"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta" "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 // merge two maps, always taking the value on the right
func mergeStrStrMaps(current, desired map[string]string) map[string]string { func mergeStrStrMaps(current, desired map[string]string) map[string]string {
result := make(map[string]string) result := make(map[string]string)
for k, v := range current { maps.Copy(result, current)
result[k] = v maps.Copy(result, desired)
}
for k, desiredVal := range desired {
result[k] = desiredVal
}
return result return result
} }

@ -24,6 +24,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"maps"
"os" "os"
"path/filepath" "path/filepath"
"strings" "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. // If the value is a map, the maps will be merged recursively.
func MergeMaps(a, b map[string]interface{}) map[string]interface{} { func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a)) out := make(map[string]interface{}, len(a))
for k, v := range a { maps.Copy(out, a)
out[k] = v
}
for k, v := range b { for k, v := range b {
if v, ok := v.(map[string]interface{}); ok { if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok { if bv, ok := out[k]; ok {

@ -19,6 +19,7 @@ package util
import ( import (
"fmt" "fmt"
"log" "log"
"maps"
"github.com/mitchellh/copystructure" "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{} { func copyMap(src map[string]interface{}) map[string]interface{} {
m := make(map[string]interface{}, len(src)) m := make(map[string]interface{}, len(src))
for k, v := range src { maps.Copy(m, src)
m[k] = v
}
return m return m
} }

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

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"maps"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -249,9 +250,7 @@ func (e Engine) initFunMap(t *template.Template) {
} }
// Set custom template funcs // Set custom template funcs
for k, v := range e.CustomTemplateFuncs { maps.Copy(funcMap, e.CustomTemplateFuncs)
funcMap[k] = v
}
t.Funcs(funcMap) t.Funcs(funcMap)
} }

@ -19,6 +19,7 @@ package engine
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"maps"
"strings" "strings"
"text/template" "text/template"
@ -73,9 +74,7 @@ func funcMap() template.FuncMap {
}, },
} }
for k, v := range extra { maps.Copy(f, extra)
f[k] = v
}
return f return f
} }

@ -19,6 +19,7 @@ package driver // import "helm.sh/helm/v4/pkg/storage/driver"
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"maps"
"sort" "sort"
"strconv" "strconv"
"time" "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)) slog.Debug("failed to get release custom labels", "namespace", record.Namespace, "key", record.Key, slog.Any("error", err))
return nil, err return nil, err
} }
for k, v := range getReleaseSystemLabels(release) { maps.Copy(release.Labels, getReleaseSystemLabels(release))
release.Labels[k] = v
}
if filter(release) { if filter(release) {
releases = append(releases, release) releases = append(releases, release)

Loading…
Cancel
Save