Skip to content

Commit

Permalink
Remove unnecessary keysIn helper (#52671)
Browse files Browse the repository at this point in the history
* Remove unnecessary keysIn helper

* Remove slices.Any

It's a carbon copy of the standard library's slices.ContainsFunc.
  • Loading branch information
zmb3 authored Mar 3, 2025
1 parent 6e95ac6 commit cc3122e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 57 deletions.
10 changes: 0 additions & 10 deletions api/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,6 @@ func ContainSameUniqueElements[S ~[]E, E comparable](s1, s2 S) bool {
return true
}

// Any checks if any element of slice satisfy given predicate. If the slice is empty, it returns false.
func Any[S ~[]E, E any](s S, predicate func(E) bool) bool {
for _, e := range s {
if predicate(e) {
return true
}
}
return false
}

// All checks if all elements of slice satisfy given predicate. If the slice is empty, it returns true.
func All[S ~[]E, E any](s S, predicate func(E) bool) bool {
for _, e := range s {
Expand Down
34 changes: 0 additions & 34 deletions api/utils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,40 +108,6 @@ func TestContainSameUniqueElements(t *testing.T) {
}
}

func TestAny(t *testing.T) {
tests := []struct {
name string
inputSlice []int
predicate func(e int) bool
expected bool
}{
{
name: "empty slice",
inputSlice: []int{},
predicate: func(e int) bool { return e > 0 },
expected: false,
},
{
name: "non-empty slice with matching element",
inputSlice: []int{1, 2, 3},
predicate: func(e int) bool { return e > 0 },
expected: true,
},
{
name: "non-empty slice with no matching element",
inputSlice: []int{-1, -2, -3},
predicate: func(e int) bool { return e > 0 },
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, Any(tt.inputSlice, tt.predicate))
})
}
}

func TestAll(t *testing.T) {
tests := []struct {
name string
Expand Down
17 changes: 5 additions & 12 deletions lib/auth/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package auth
import (
"context"
"fmt"
"maps"
"math"
"path/filepath"
"slices"
Expand Down Expand Up @@ -740,14 +741,6 @@ func TestClusterName(t *testing.T) {
require.Equal(t, conf.ClusterName.GetClusterName(), cn.GetClusterName())
}

func keysIn[K comparable, V any](m map[K]V) []K {
result := make([]K, 0, len(m))
for k := range m {
result = append(result, k)
}
return result
}

type failingTrustInternal struct {
services.TrustInternal
}
Expand Down Expand Up @@ -994,7 +987,7 @@ func TestPresets(t *testing.T) {
defer mu.Unlock()
require.True(t, types.IsSystemResource(r))
require.Contains(t, expectedSystemRoles, r.GetName())
require.NotContains(t, keysIn(createdSystemRoles), r.GetName())
require.NotContains(t, slices.Collect(maps.Keys(createdSystemRoles)), r.GetName())
createdSystemRoles[r.GetName()] = r
}).
Maybe().
Expand All @@ -1004,8 +997,8 @@ func TestPresets(t *testing.T) {

err := createPresetRoles(ctx, roleManager)
require.NoError(t, err)
require.ElementsMatch(t, keysIn(createdPresets), expectedPresetRoles)
require.ElementsMatch(t, keysIn(createdSystemRoles), expectedSystemRoles)
require.ElementsMatch(t, slices.Collect(maps.Keys(createdPresets)), expectedPresetRoles)
require.ElementsMatch(t, slices.Collect(maps.Keys(createdSystemRoles)), expectedSystemRoles)
roleManager.AssertExpectations(t)

//
Expand Down Expand Up @@ -1142,7 +1135,7 @@ func TestPresets(t *testing.T) {
// Run multiple times to simulate starting auth on an
// existing cluster and asserting that everything still
// returns success
for i := 0; i < 2; i++ {
for range 2 {
err := createPresetRoles(ctx, as)
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion lib/srv/db/common/databaseobjectimportrule/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"log/slog"
"regexp"
"slices"
"sort"
"strings"
"unicode"
Expand Down Expand Up @@ -276,7 +277,7 @@ func matchPattern(pattern, value string) bool {
}

func matchAny(patterns []string, value string) bool {
return utils.Any(patterns, func(pattern string) bool {
return slices.ContainsFunc(patterns, func(pattern string) bool {
return matchPattern(pattern, value)
})
}
Expand Down

0 comments on commit cc3122e

Please sign in to comment.