Skip to content

Commit 60ac246

Browse files
feat: improves collection helpers (kserve#4579)
Signed-off-by: Bartosz Majsak <bartosz.majsak@gmail.com> Co-authored-by: Sivanantham <90966311+sivanantha321@users.noreply.github.com>
1 parent 7fc93ea commit 60ac246

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

pkg/utils/collections.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2025 The KServe Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"strings"
21+
)
22+
23+
type PredicateFn[T any] func(T) bool
24+
25+
func FilterSlice[T any](items []T, predicate PredicateFn[T]) []T {
26+
if len(items) == 0 {
27+
return []T{}
28+
}
29+
30+
result := make([]T, 0, len(items))
31+
for _, item := range items {
32+
if predicate(item) {
33+
result = append(result, item)
34+
}
35+
}
36+
return result
37+
}
38+
39+
func Includes[T comparable](slice []T, value T) bool {
40+
for _, v := range slice {
41+
if v == value {
42+
return true
43+
}
44+
}
45+
return false
46+
}
47+
48+
func IncludesArg(slice []string, arg string) bool {
49+
for _, v := range slice {
50+
if v == arg || strings.HasPrefix(v, arg) {
51+
return true
52+
}
53+
}
54+
return false
55+
}
56+
57+
func Filter[K comparable, V any](origin map[K]V, predicate PredicateFn[K]) map[K]V {
58+
result := make(map[K]V)
59+
for k, v := range origin {
60+
if predicate(k) {
61+
result[k] = v
62+
}
63+
}
64+
return result
65+
}
66+
67+
func Union[K comparable, V any](maps ...map[K]V) map[K]V {
68+
result := make(map[K]V)
69+
for _, m := range maps {
70+
for k, v := range m {
71+
result[k] = v
72+
}
73+
}
74+
return result
75+
}

pkg/utils/utils.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,6 @@ const (
4444
ErrValueExceedsInt32Limit = "value exceeds int32 limit %d"
4545
)
4646

47-
func Filter(origin map[string]string, predicate func(string) bool) map[string]string {
48-
result := make(map[string]string)
49-
for k, v := range origin {
50-
if predicate(k) {
51-
result[k] = v
52-
}
53-
}
54-
return result
55-
}
56-
57-
func Union(maps ...map[string]string) map[string]string {
58-
result := make(map[string]string)
59-
for _, m := range maps {
60-
for k, v := range m {
61-
result[k] = v
62-
}
63-
}
64-
return result
65-
}
66-
67-
func Includes(slice []string, value string) bool {
68-
for _, v := range slice {
69-
if v == value {
70-
return true
71-
}
72-
}
73-
return false
74-
}
75-
76-
func IncludesArg(slice []string, arg string) bool {
77-
for _, v := range slice {
78-
if v == arg || strings.HasPrefix(v, arg) {
79-
return true
80-
}
81-
}
82-
return false
83-
}
84-
8547
func AppendVolumeIfNotExists(slice []corev1.Volume, volume corev1.Volume) []corev1.Volume {
8648
for i := range slice {
8749
if slice[i].Name == volume.Name {

0 commit comments

Comments
 (0)