Skip to content

Commit

Permalink
[collection] add helpers easier to use with slices (#543)
Browse files Browse the repository at this point in the history
<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description

Add small helpers in `collection` for ease of use and reduce verbosity

Replaces #542 


### Test Coverage

<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->

- [x]  This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [ ] Additional tests are not required for this change (e.g.
documentation update).
  • Loading branch information
acabarbaye authored Jan 10, 2025
1 parent 820f12d commit de98a8b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions utils/collection/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package collection

import "slices"

// Remove looks for elements in a slice. If they're found, it will
// remove them.
func Remove(slice []string, val ...string) []string {
Expand All @@ -16,6 +18,9 @@ func GenericRemove(equal func(string, string) bool, slice []string, val ...strin
if len(val) == 0 {
return slice
}
if len(val) == 1 {
return slices.DeleteFunc(slice, func(v string) bool { return equal(v, val[0]) })
}
list := make([]string, 0, len(slice))
found := make([]bool, len(val))

Expand Down
5 changes: 5 additions & 0 deletions utils/collection/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package collection

import (
"slices"
"strings"

mapset "github.com/deckarep/golang-set/v2"
Expand All @@ -25,6 +26,10 @@ func FindInSlice(strict bool, slice []string, val ...string) (int, bool) {
if len(val) == 0 || len(slice) == 0 {
return -1, false
}
if strict && len(val) == 1 {
idx := slices.Index(slice, val[0])
return idx, idx >= 0
}

inSlice := make(map[string]int, len(slice))
for i := range slice {
Expand Down
4 changes: 2 additions & 2 deletions utils/filesystem/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func TestNewLimits(t *testing.T) {
require.NoError(t, NoLimits().Validate())
require.NoError(t, DefaultLimits().Validate())
require.NoError(t, NoLimits().Validate()) //nolint:typecheck
require.NoError(t, DefaultLimits().Validate()) //nolint:typecheck
assert.True(t, DefaultLimits().Apply())
assert.False(t, NoLimits().Apply())
}
2 changes: 1 addition & 1 deletion utils/git/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func TestNewConfig(t *testing.T) {
require.NoError(t, DefaultLimits().Validate())
require.NoError(t, DefaultLimits().Validate()) //nolint:typecheck
}
4 changes: 2 additions & 2 deletions utils/git/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func TestNewLimits(t *testing.T) {
require.NoError(t, NoLimits().Validate())
require.NoError(t, DefaultLimits().Validate())
require.NoError(t, NoLimits().Validate()) //nolint:typecheck
require.NoError(t, DefaultLimits().Validate()) //nolint:typecheck
assert.True(t, DefaultLimits().Apply())
assert.False(t, NoLimits().Apply())
}

0 comments on commit de98a8b

Please sign in to comment.