minor rework and filter dimensions
This commit is contained in:
55
cmd/cli/commands/channeliter.go
Normal file
55
cmd/cli/commands/channeliter.go
Normal file
@ -0,0 +1,55 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.loyso.art/frx/eway/internal/entity"
|
||||
)
|
||||
|
||||
type chanIter[T any] struct {
|
||||
in <-chan T
|
||||
err error
|
||||
next T
|
||||
}
|
||||
|
||||
var errChannelClosed = errors.New("channel closed")
|
||||
|
||||
func (i *chanIter[T]) Next() (ok bool) {
|
||||
if i.err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
i.next, ok = <-i.in
|
||||
if !ok {
|
||||
i.err = errChannelClosed
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (i *chanIter[T]) Get() T {
|
||||
return i.next
|
||||
}
|
||||
|
||||
func (i *chanIter[T]) Err() error {
|
||||
if errors.Is(i.err, errChannelClosed) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return i.err
|
||||
}
|
||||
|
||||
func (i *chanIter[T]) Close() {
|
||||
for range i.in {
|
||||
}
|
||||
}
|
||||
|
||||
func getItemsIter(ctx context.Context, r entity.GoodsItemRepository) *chanIter[entity.GoodsItem] {
|
||||
in, err := r.ListIter(ctx, 3)
|
||||
|
||||
return &chanIter[entity.GoodsItem]{
|
||||
in: in,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user