listing

package
v0.30.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: Apache-2.0 Imports: 2 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMoreItems = errors.New("no more items")

Functions

func ToSlice

func ToSlice[T any](ctx context.Context, it Iterator[T]) ([]T, error)

ToSlice returns all items from the iterator as a slice. If there was an error fetching items at any time, it returns that error.

func ToSliceN

func ToSliceN[T any, Limit ~int | ~int64](ctx context.Context, it Iterator[T], n Limit) ([]T, error)

ToSliceN returns the first N items from the iterator as a slice. If there was an error fetching items at any time, it returns that error. If n == 0, it returns all items.

Types

type DeduplicatingIterator

type DeduplicatingIterator[T any, Id comparable] struct {
	// contains filtered or unexported fields
}

func NewDedupeIterator

func NewDedupeIterator[T any, Id comparable](it Iterator[T], getId func(T) Id) *DeduplicatingIterator[T, Id]

func (*DeduplicatingIterator[T, Id]) HasNext

func (i *DeduplicatingIterator[T, Id]) HasNext(ctx context.Context) bool

func (*DeduplicatingIterator[T, Id]) Next

func (i *DeduplicatingIterator[T, Id]) Next(ctx context.Context) (T, error)

type Iterator

type Iterator[T any] interface {
	// HasNext returns true if there are more items to iterate over. HasNext
	// will also return true if the iterator needs to fetch the next page of
	// items from the underlying source and the request fails, even if there
	// are no more items to iterate over. In this case, Next will return the
	// error.
	HasNext(context.Context) bool

	// Next returns the next item in the iterator. If there are no more items
	// to iterate over, it returns ErrNoMoreItems. If there was an error
	// fetching the next page of items, it returns that error. Once Next
	// returns ErrNoMoreItems or an error, it will continue to return that
	// error.
	Next(context.Context) (T, error)
}

Iterator[T] is an iterator over items of type T. It is similar to java.util.Iterator. It is not thread-safe.

type PaginatingIterator

type PaginatingIterator[Req, Resp, T any] struct {
	// contains filtered or unexported fields
}

Use struct{} for Req for iterators that don't need any request structure.

func NewIterator

func NewIterator[Req, Resp, T any](
	nextReq *Req,
	getNextPage func(context.Context, Req) (Resp, error),
	getItems func(Resp) []T,
	getNextReq func(Resp) *Req,
) *PaginatingIterator[Req, Resp, T]

Returns a new iterator. The iterator will fetch the next page of items lazily, when needed. nextReq is the request to be used to fetch the initial page. If nil, then no page will be fetched. getNextPage fetches the next page of items, returning the deserialized response and error. getItems selects the items being iterated over from the response. getNextReq is used to get the next request to be used in the next page. If the returned value is nil, then there is no next page to fetch.

func (*PaginatingIterator[Req, Resp, T]) HasNext

func (i *PaginatingIterator[Req, Resp, T]) HasNext(ctx context.Context) bool

func (*PaginatingIterator[Req, Resp, T]) Next

func (i *PaginatingIterator[Req, Resp, T]) Next(ctx context.Context) (T, error)

type SliceIterator added in v0.29.1

type SliceIterator[T any] []T

A simple iterator over a slice.

func (*SliceIterator[T]) HasNext added in v0.29.1

func (s *SliceIterator[T]) HasNext(_ context.Context) bool

func (*SliceIterator[T]) Next added in v0.29.1

func (s *SliceIterator[T]) Next(_ context.Context) (T, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL