FilterResults fixed
This commit is contained in:
parent
a864cb930d
commit
e9fdfd6e42
21
result.go
21
result.go
@ -49,6 +49,17 @@ func (r Result[T]) Get() (T, error) {
|
|||||||
return *r.value, r.err
|
return *r.value, r.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r Result[T]) GetSafe() T {
|
||||||
|
if r.err != nil {
|
||||||
|
panic(r.err)
|
||||||
|
}
|
||||||
|
return *r.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Result[T]) Err() error {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
|
||||||
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
||||||
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
|
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
|
||||||
|
|
||||||
@ -76,13 +87,11 @@ func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <
|
|||||||
defer close(fail)
|
defer close(fail)
|
||||||
|
|
||||||
for r := range source {
|
for r := range source {
|
||||||
v, err := r.Get()
|
if r.Fail() {
|
||||||
|
fail <- r.Err()
|
||||||
if err == nil {
|
continue
|
||||||
succ <- v
|
|
||||||
} else {
|
|
||||||
fail <- err
|
|
||||||
}
|
}
|
||||||
|
succ <- r.GetSafe()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user