generated from openmcp-project/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add collection utility functions #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21da7d5
add collection utility functions
Diaphteiros 4c455ed
add RemoveFinalizersWithPrefix method and rewrite utils tests
Diaphteiros 7a80bbe
add unit tests for AggregateSlice and AggregateMap
Diaphteiros 2a4ff14
remove RemoveFinalizersWithPrefix function because it is somewhat spe…
Diaphteiros c5b866a
rename variable to avoid confusing overshadowing
Diaphteiros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package collections | ||
|
||
// ProjectSlice takes a slice and a projection function and applies this function to each element of the slice. | ||
// It returns a new slice containing the results of the projection. | ||
// The original slice is not modified. | ||
// If the projection function is nil, it returns nil. | ||
func ProjectSlice[X any, Y any](src []X, project func(X) Y) []Y { | ||
if project == nil { | ||
return nil | ||
} | ||
res := make([]Y, len(src)) | ||
for i, x := range src { | ||
res[i] = project(x) | ||
} | ||
return res | ||
} | ||
|
||
// ProjectMapToSlice takes a map and a projection function and applies this function to each key-value pair in the map. | ||
// It returns a new slice containing the results of the projection. | ||
// The original map is not modified. | ||
// If the projection function is nil, it returns nil. | ||
func ProjectMapToSlice[K comparable, V any, R any](src map[K]V, project func(K, V) R) []R { | ||
if project == nil { | ||
return nil | ||
} | ||
res := make([]R, 0, len(src)) | ||
for k, v := range src { | ||
res = append(res, project(k, v)) | ||
} | ||
return res | ||
} | ||
|
||
// ProjectMapToMap takes a map and a projection function and applies this function to each key-value pair in the map. | ||
// It returns a new map containing the results of the projection. | ||
// The original map is not modified. | ||
// Note that the resulting map may be smaller if the projection function does not guarantee unique keys. | ||
// If the projection function is nil, it returns nil. | ||
func ProjectMapToMap[K1 comparable, V1 any, K2 comparable, V2 any](src map[K1]V1, project func(K1, V1) (K2, V2)) map[K2]V2 { | ||
if project == nil { | ||
return nil | ||
} | ||
res := make(map[K2]V2, len(src)) | ||
for k, v := range src { | ||
newK, newV := project(k, v) | ||
res[newK] = newV | ||
} | ||
return res | ||
} | ||
|
||
// AggregateSlice takes a slice, an aggregation function and an initial value. | ||
// It applies the aggregation function to each element of the slice, also passing in the current result. | ||
// For the first element, it uses the initial value as the current result. | ||
// Returns initial if the aggregation function is nil. | ||
func AggregateSlice[X any, Y any](src []X, agg func(X, Y) Y, initial Y) Y { | ||
if agg == nil { | ||
return initial | ||
} | ||
res := initial | ||
for _, x := range src { | ||
res = agg(x, res) | ||
} | ||
return res | ||
} | ||
|
||
// AggregateMap takes a map, an aggregation function and an initial value. | ||
// It applies the aggregation function to each key-value pair in the map, also passing in the current result. | ||
// For the first key-value pair, it uses the initial value as the current result. | ||
// Returns initial if the aggregation function is nil. | ||
// Note that the iteration order over the map elements is undefined and may vary between executions. | ||
func AggregateMap[K comparable, V any, R any](src map[K]V, agg func(K, V, R) R, initial R) R { | ||
Diaphteiros marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if agg == nil { | ||
return initial | ||
} | ||
res := initial | ||
for k, v := range src { | ||
res = agg(k, v, res) | ||
} | ||
return res | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package collections_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/openmcp-project/controller-utils/pkg/collections" | ||
"github.com/openmcp-project/controller-utils/pkg/pairs" | ||
) | ||
|
||
var _ = Describe("Utils Tests", func() { | ||
|
||
Context("ProjectSlice", func() { | ||
|
||
projectFunc := func(i int) int { | ||
return i * 2 | ||
} | ||
|
||
It("should use the projection function on each element of the slice", func() { | ||
src := []int{1, 2, 3, 4} | ||
projected := collections.ProjectSlice(src, projectFunc) | ||
Expect(projected).To(Equal([]int{2, 4, 6, 8})) | ||
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") | ||
}) | ||
|
||
It("should return an empty slice for an empty or nil input slice", func() { | ||
Expect(collections.ProjectSlice(nil, projectFunc)).To(BeEmpty()) | ||
Expect(collections.ProjectSlice([]int{}, projectFunc)).To(BeEmpty()) | ||
}) | ||
|
||
It("should return nil for a nil projection function", func() { | ||
src := []int{1, 2, 3, 4} | ||
projected := collections.ProjectSlice[int, int](src, nil) | ||
Expect(projected).To(BeNil()) | ||
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") | ||
}) | ||
|
||
}) | ||
|
||
Context("ProjectMapToSlice", func() { | ||
|
||
projectFunc := func(k string, v string) string { | ||
return k + ":" + v | ||
} | ||
|
||
It("should use the projection function on each key-value pair of the map", func() { | ||
src := map[string]string{"a": "1", "b": "2", "c": "3"} | ||
projected := collections.ProjectMapToSlice(src, projectFunc) | ||
Expect(projected).To(ConsistOf("a:1", "b:2", "c:3")) | ||
Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") | ||
}) | ||
|
||
It("should return an empty slice for an empty or nil input map", func() { | ||
Expect(collections.ProjectMapToSlice(nil, projectFunc)).To(BeEmpty()) | ||
Expect(collections.ProjectMapToSlice(map[string]string{}, projectFunc)).To(BeEmpty()) | ||
}) | ||
|
||
It("should return nil for a nil projection function", func() { | ||
src := map[string]string{"a": "1", "b": "2", "c": "3"} | ||
projected := collections.ProjectMapToSlice[string, string, string](src, nil) | ||
Expect(projected).To(BeNil()) | ||
Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") | ||
}) | ||
|
||
}) | ||
|
||
Context("ProjectMapToMap", func() { | ||
|
||
projectFunc := func(k string, v string) (string, int) { | ||
return k, len(v) | ||
} | ||
|
||
It("should use the projection function on each key-value pair of the map", func() { | ||
src := map[string]string{"a": "1", "b": "22", "c": "333"} | ||
projected := collections.ProjectMapToMap(src, projectFunc) | ||
Expect(projected).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) | ||
Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") | ||
}) | ||
|
||
It("should return an empty map for an empty or nil input map", func() { | ||
Expect(collections.ProjectMapToMap(nil, projectFunc)).To(BeEmpty()) | ||
Expect(collections.ProjectMapToMap(map[string]string{}, projectFunc)).To(BeEmpty()) | ||
}) | ||
|
||
It("should return nil for a nil projection function", func() { | ||
src := map[string]string{"a": "1", "b": "22", "c": "333"} | ||
projected := collections.ProjectMapToMap[string, string, string, int](src, nil) | ||
Expect(projected).To(BeNil()) | ||
Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") | ||
}) | ||
|
||
}) | ||
|
||
Context("AggregateSlice", func() { | ||
|
||
sum := func(val, s int) int { | ||
return val + s | ||
} | ||
stradd := func(val int, s string) string { | ||
return fmt.Sprintf("%s%d", s, val) | ||
} | ||
|
||
It("should return the initial value if the aggregation function is nil", func() { | ||
src := []int{1, 2, 3, 4} | ||
result := collections.AggregateSlice(src, nil, 0) | ||
Expect(result).To(Equal(0)) | ||
Expect(src).To(Equal([]int{1, 2, 3, 4})) | ||
}) | ||
|
||
It("should correctly aggregate the slice using the provided function", func() { | ||
src := []int{1, 2, 3, 4} | ||
result := collections.AggregateSlice(src, sum, 0) | ||
Expect(result).To(Equal(10)) | ||
Expect(src).To(Equal([]int{1, 2, 3, 4})) | ||
|
||
result2 := collections.AggregateSlice(src, stradd, "test") | ||
Expect(result2).To(Equal("test1234")) | ||
Expect(src).To(Equal([]int{1, 2, 3, 4})) | ||
}) | ||
|
||
It("should handle a nil input slice", func() { | ||
result := collections.AggregateSlice[int, int](nil, sum, 100) | ||
Expect(result).To(Equal(100)) | ||
}) | ||
|
||
}) | ||
|
||
Context("AggregateMap", func() { | ||
|
||
aggregate := func(k string, v int, agg pairs.Pair[string, int]) pairs.Pair[string, int] { | ||
return pairs.New(agg.Key+k, agg.Value+v) | ||
} | ||
|
||
It("should return the initial value if the aggregation function is nil", func() { | ||
src := map[string]int{"a": 1, "b": 2, "c": 3} | ||
result := collections.AggregateMap(src, nil, 0) | ||
Expect(result).To(Equal(0)) | ||
Expect(src).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) | ||
}) | ||
|
||
It("should correctly aggregate the map using the provided function", func() { | ||
src := map[string]int{"a": 1, "b": 2, "c": 3} | ||
result := collections.AggregateMap(src, aggregate, pairs.New("", 0)) | ||
Expect(result.Key).To(HaveLen(3)) | ||
Expect(result.Key).To(ContainSubstring("a")) | ||
Expect(result.Key).To(ContainSubstring("b")) | ||
Expect(result.Key).To(ContainSubstring("c")) | ||
Expect(result.Value).To(Equal(6)) | ||
Expect(src).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) | ||
}) | ||
|
||
It("should handle a nil input map", func() { | ||
result := collections.AggregateMap(nil, aggregate, pairs.New("", 0)) | ||
Expect(result.Key).To(BeEmpty()) | ||
Expect(result.Value).To(Equal(0)) | ||
}) | ||
|
||
}) | ||
|
||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.