Skip to content

Commit 77c385c

Browse files
committed
refactor(sliceutils): refactor the AssertContains function
This change refactors the `AssertContains` function to use `MapNoError` instead of `Map` to avoid needing to unwrap a `result.Result`. Signed-off-by: m-d-key <[email protected]>
1 parent 5cd2181 commit 77c385c

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

pkg/sliceutils/sliceutils.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,24 @@ func AnyToAnySlice(value any) maybe.Maybe[[]any] {
4949
return maybe.Just(slice)
5050
}
5151

52-
// AssertContains returns an error if the given slice of values does not contain
53-
// the give value, otherwise it returns nil. The returned error describes the
54-
// missing values.
52+
// AssertContains returns an error if the given target slice of values does not
53+
// contain the give value, otherwise it returns nil. The returned error
54+
// describes the missing values. For a non-asserting contains function,
55+
// use slices.Contains from the Go standard library.
5556
func AssertContains[T comparable](target []T, value T) error {
5657
if slices.Contains(target, value) {
5758
return nil
5859
}
5960
// We could call stringutils.ToQuoted here but that would create a cycle as
6061
// stringutils uses sliceutils. I believe sliceutils to be the more
61-
// foundational so it should reimplement this mapper.Mapper function rather
62-
// than stringutils reimplementing MapNoError.
63-
quotedValuesResult := Map(
64-
func(value T) result.Result[string] {
65-
return result.Ok(fmt.Sprintf("%v", value))
62+
// foundational so it should reimplement this mapper.MapperNoError function
63+
// rather than stringutils reimplementing MapNoError.
64+
quotedValues := MapNoError(
65+
func(value T) string {
66+
return fmt.Sprintf("%v", value)
6667
},
6768
target,
6869
)
69-
// The mapper above does not ever return an error, so we can call MustGet
70-
// without checking IsError first.
71-
quotedValues := quotedValuesResult.MustGet()
7270
sort.Strings(quotedValues)
7371
csv := strings.Join(quotedValues, `, `)
7472
return errors.New(`must be one of %s`, csv)

0 commit comments

Comments
 (0)