From b6fcbc0aa81a992739b04e9ab430794829fa0280 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Thu, 7 Mar 2024 14:25:44 -0300 Subject: [PATCH] fixup! fix: Enable more linters Signed-off-by: Mateus Oliveira --- internal/common/function/function.go | 34 +++++++++++------------ internal/common/function/function_test.go | 8 +++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/internal/common/function/function.go b/internal/common/function/function.go index 219adf7..f6a9c0e 100644 --- a/internal/common/function/function.go +++ b/internal/common/function/function.go @@ -44,7 +44,7 @@ func AddNonAdminLabels(labels map[string]string) map[string]string { constant.ManagedByLabel: constant.ManagedByLabelValue, } - mergedLabels, err := mergeUniqueKeyTOfTMaps(defaultLabels, labels) + mergedLabels, err := mergeMaps(defaultLabels, labels) if err != nil { // TODO logger _, _ = fmt.Println("Error merging labels:", err) @@ -64,7 +64,7 @@ func AddNonAdminBackupAnnotations(ownerNamespace string, ownerName string, owner constant.NabOriginUUIDAnnotation: ownerUUID, } - mergedAnnotations, err := mergeUniqueKeyTOfTMaps(defaultAnnotations, existingAnnotations) + mergedAnnotations, err := mergeMaps(defaultAnnotations, existingAnnotations) if err != nil { // TODO logger _, _ = fmt.Println("Error merging annotations:", err) @@ -198,27 +198,27 @@ func GetNonAdminBackupFromVeleroBackup(ctx context.Context, clientInstance clien return nonAdminBackup, nil } -// TODO import? -// Similar to as pkg/common/common.go:AppendUniqueKeyTOfTMaps from github.com/openshift/oadp-operator -func mergeUniqueKeyTOfTMaps[T comparable](userMap ...map[T]T) (map[T]T, error) { - var base map[T]T - for i, mapElements := range userMap { - if mapElements == nil { +// TODO import? Similar to as pkg/common/common.go:AppendUniqueKeyTOfTMaps from github.com/openshift/oadp-operator + +// Return map, of the same type as the input maps, that contains all keys/values from all input maps. +// Key/value pairs that are identical in different input maps, are added only once to return map. +// If a key exists in more than one input map, with a different value, an error is returned +func mergeMaps[T comparable](maps ...map[T]T) (map[T]T, error) { + merge := make(map[T]T) + for _, m := range maps { + if m == nil { continue } - if base == nil { - base = make(map[T]T) - } - for k, v := range mapElements { - existingValue, found := base[k] + for k, v := range m { + existingValue, found := merge[k] if found { if existingValue != v { - return nil, fmt.Errorf("conflicting key %v with value %v in map %d may not override %v", k, v, i, existingValue) + return nil, fmt.Errorf("conflicting key %v: has both value %v and value %v in input maps", k, v, existingValue) } - } else { - base[k] = v + continue } + merge[k] = v } } - return base, nil + return merge, nil } diff --git a/internal/common/function/function_test.go b/internal/common/function/function_test.go index 4091a22..281923f 100644 --- a/internal/common/function/function_test.go +++ b/internal/common/function/function_test.go @@ -35,7 +35,7 @@ import ( "github.com/migtools/oadp-non-admin/internal/common/constant" ) -func TestMergeUniqueKeyTOfTMaps(t *testing.T) { +func TestMergeMaps(t *testing.T) { const ( d = "d" delta = "delta" @@ -93,13 +93,13 @@ func TestMergeUniqueKeyTOfTMaps(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := mergeUniqueKeyTOfTMaps(tt.args...) + got, err := mergeMaps(tt.args...) if (err != nil) != tt.wantErr { - t.Errorf("mergeUniqueKeyTOfTMaps() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("mergeMaps() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("mergeUniqueKeyTOfTMaps() = %v, want %v", got, tt.want) + t.Errorf("mergeMaps() = %v, want %v", got, tt.want) } }) }