Skip to content

Commit

Permalink
rename to FilterMapUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis committed Dec 10, 2024
1 parent fe2f4d5 commit 4983f1e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/srv/discovery/database_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func (s *Server) startDatabaseWatchers() error {
Origin: types.OriginCloud,
Clock: s.clock,
PreFetchHookFn: func() {
discoveryConfigs := slices.CollectValues(
discoveryConfigs := slices.FilterMapUnique(
s.getAllDatabaseFetchers(),
func(f common.Fetcher) (s string, skip bool) {
return f.DiscoveryConfigName(), f.DiscoveryConfigName() == ""
return f.DiscoveryConfigName(), f.DiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down
12 changes: 6 additions & 6 deletions lib/srv/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,10 @@ func (s *Server) initAWSWatchers(matchers []types.AWSMatcher) error {
server.WithPollInterval(s.PollInterval),
server.WithTriggerFetchC(s.newDiscoveryConfigChangedSub()),
server.WithPreFetchHookFn(func() {
discoveryConfigs := libslices.CollectValues(
discoveryConfigs := libslices.FilterMapUnique(
s.getAllAWSServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() == ""
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down Expand Up @@ -725,10 +725,10 @@ func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMa
server.WithPollInterval(s.PollInterval),
server.WithTriggerFetchC(s.newDiscoveryConfigChangedSub()),
server.WithPreFetchHookFn(func() {
discoveryConfigs := libslices.CollectValues(
discoveryConfigs := libslices.FilterMapUnique(
s.getAllAzureServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() == ""
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down Expand Up @@ -791,10 +791,10 @@ func (s *Server) initGCPServerWatcher(ctx context.Context, vmMatchers []types.GC
server.WithPollInterval(s.PollInterval),
server.WithTriggerFetchC(s.newDiscoveryConfigChangedSub()),
server.WithPreFetchHookFn(func() {
discoveryConfigs := libslices.CollectValues(
discoveryConfigs := libslices.FilterMapUnique(
s.getAllGCPServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() == ""
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down
4 changes: 2 additions & 2 deletions lib/srv/discovery/kube_integration_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func (s *Server) startKubeIntegrationWatchers() error {
Origin: types.OriginCloud,
TriggerFetchC: s.newDiscoveryConfigChangedSub(),
PreFetchHookFn: func() {
discoveryConfigs := libslices.CollectValues(
discoveryConfigs := libslices.FilterMapUnique(
s.getKubeIntegrationFetchers(),
func(f common.Fetcher) (s string, skip bool) {
return f.DiscoveryConfigName(), f.DiscoveryConfigName() == ""
return f.DiscoveryConfigName(), f.DiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down
12 changes: 5 additions & 7 deletions lib/utils/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import (
"slices"
)

// CollectValues applies a function to all elements of a slice and collects them.
// The function returns the value to collect and whether the current element should be skipped.
// FilterMapUnique applies a function to all elements of a slice and collects them.
// The function returns the value to collect and whether the current element should be included.
// Returned values are sorted and deduplicated.
func CollectValues[T any, S cmp.Ordered](ts []T, fn func(T) (s S, skip bool)) []S {
func FilterMapUnique[T any, S cmp.Ordered](ts []T, fn func(T) (s S, include bool)) []S {
ss := make([]S, 0, len(ts))
for _, t := range ts {
s, skip := fn(t)
if skip {
continue
if s, include := fn(t); include {
ss = append(ss, s)
}
ss = append(ss, s)
}
slices.Sort(ss)
return slices.Compact(ss)
Expand Down
24 changes: 12 additions & 12 deletions lib/utils/slices/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ import (
"github.com/stretchr/testify/require"
)

func TestCollectValues(t *testing.T) {
func TestFilterMapUnique(t *testing.T) {
for _, tt := range []struct {
name string
input []string
collector func(string) (s string, skip bool)
collector func(string) (s string, include bool)
expected []string
}{
{
name: "no elements",
input: []string{},
collector: func(in string) (s string, skip bool) {
return in, false
collector: func(in string) (s string, include bool) {
return in, true
},
expected: []string{},
},
{
name: "multiple strings, all match",
input: []string{"x", "y"},
collector: func(in string) (s string, skip bool) {
return in, false
collector: func(in string) (s string, include bool) {
return in, true
},
expected: []string{"x", "y"},
},
{
name: "deduplicates items",
input: []string{"x", "y", "z", "x"},
collector: func(in string) (s string, skip bool) {
return in, false
collector: func(in string) (s string, include bool) {
return in, true
},
expected: []string{"x", "y", "z"},
},
{
name: "skipped values are not returned",
name: "includeped values are not returned",
input: []string{"x", "y", "z", ""},
collector: func(in string) (s string, skip bool) {
return in, in == ""
collector: func(in string) (s string, include bool) {
return in, in != ""
},
expected: []string{"x", "y", "z"},
},
} {
t.Run(tt.name, func(t *testing.T) {
got := CollectValues(tt.input, tt.collector)
got := FilterMapUnique(tt.input, tt.collector)
require.Equal(t, tt.expected, got)
})
}
Expand Down

0 comments on commit 4983f1e

Please sign in to comment.