Skip to content

Commit

Permalink
fixup! fix: Enable more linters
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Mar 7, 2024
1 parent b0e5272 commit b6fcbc0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
34 changes: 17 additions & 17 deletions internal/common/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions internal/common/function/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit b6fcbc0

Please sign in to comment.