pagination

package
v1.69.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 9 Imported by: 8

Documentation

Overview

Package pagination provides a ways to iterate over collections. For that purposes, it defines iterators and paginators, which act as an abstraction over the process of iterating over an entire result set of a truncated API operation returning pages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbstractPaginator

type AbstractPaginator struct {
	// contains filtered or unexported fields
}

func NewAbstractPaginator

func NewAbstractPaginator(ctx context.Context, firstPage IStaticPage, fetchNextFunc func(context.Context, IStaticPage) (IStaticPage, error)) (p *AbstractPaginator, err error)

func (*AbstractPaginator) Close

func (a *AbstractPaginator) Close() error

func (*AbstractPaginator) FetchCurrentPage added in v1.23.0

func (a *AbstractPaginator) FetchCurrentPage() (page IStaticPage, err error)

func (*AbstractPaginator) FetchCurrentPageIterator added in v1.23.0

func (a *AbstractPaginator) FetchCurrentPageIterator() (it IIterator, err error)

func (*AbstractPaginator) FetchNextPage added in v1.23.0

func (a *AbstractPaginator) FetchNextPage(ctx context.Context, currentPage IStaticPage) (IStaticPage, error)

func (*AbstractPaginator) GetContext

func (a *AbstractPaginator) GetContext() context.Context

func (*AbstractPaginator) GetNext

func (a *AbstractPaginator) GetNext() (item interface{}, err error)

func (*AbstractPaginator) HasNext

func (a *AbstractPaginator) HasNext() bool

func (*AbstractPaginator) SetCurrentPage

func (a *AbstractPaginator) SetCurrentPage(page IStaticPage) (err error)

func (*AbstractPaginator) Stop

type AbstractStreamPaginator added in v1.23.0

type AbstractStreamPaginator struct {
	AbstractPaginator
	// contains filtered or unexported fields
}

func (*AbstractStreamPaginator) Close added in v1.23.0

func (s *AbstractStreamPaginator) Close() error

func (*AbstractStreamPaginator) DryUp added in v1.23.0

func (s *AbstractStreamPaginator) DryUp() error

func (*AbstractStreamPaginator) FetchFuturePage added in v1.23.0

func (s *AbstractStreamPaginator) FetchFuturePage(ctx context.Context, currentPage IStaticPageStream) (IStaticPageStream, error)

func (*AbstractStreamPaginator) GetNext added in v1.23.0

func (s *AbstractStreamPaginator) GetNext() (interface{}, error)

func (*AbstractStreamPaginator) HasNext added in v1.23.0

func (s *AbstractStreamPaginator) HasNext() bool

func (*AbstractStreamPaginator) IsRunningDry added in v1.23.0

func (s *AbstractStreamPaginator) IsRunningDry() bool

func (*AbstractStreamPaginator) Stop added in v1.23.0

type DynamicPagePaginator added in v1.23.0

type DynamicPagePaginator struct {
	AbstractPaginator
}

DynamicPagePaginator defines a paginator over dynamic pages i.e. pages from which it is possible to access the next one.

func (*DynamicPagePaginator) GetCurrentPage added in v1.23.0

func (d *DynamicPagePaginator) GetCurrentPage() (dynamicPage IPage, err error)

type DynamicPageStreamPaginator added in v1.23.0

type DynamicPageStreamPaginator struct {
	AbstractStreamPaginator
}

DynamicPageStreamPaginator defines a paginator over a stream of dynamic pages i.e. pages from which it is possible to access the next one.

func (*DynamicPageStreamPaginator) GetCurrentPage added in v1.23.0

func (d *DynamicPageStreamPaginator) GetCurrentPage() (dynamicPage IPage, err error)

type IGenericPaginator added in v1.23.0

type IGenericPaginator interface {
	io.Closer
	IIterator
	// Stop returns a stop function which stops the iteration.
	Stop() context.CancelFunc
}

IGenericPaginator defines a generic paginator.

type IGenericStreamPaginator added in v1.23.0

type IGenericStreamPaginator interface {
	IGenericPaginator
	// DryUp indicates to the stream that it will soon run out.
	DryUp() error
	// IsRunningDry indicates whether the stream is about to run out.
	IsRunningDry() bool
}

IGenericStreamPaginator is an iterator over a stream. A stream is a collection without any know ending.

type IIterator

type IIterator interface {
	// HasNext returns whether there are more items available or not.
	HasNext() bool
	// GetNext returns the next item.
	GetNext() (interface{}, error)
}

IIterator defines an iterator over a collection of items.

func NewMockPageIterator

func NewMockPageIterator(page *MockPage) (IIterator, error)

type IPage

type IPage interface {
	IStaticPage
	// GetNext returns the next page.
	GetNext(ctx context.Context) (IPage, error)
}

IPage defines a page with the ability to access next pages.

type IPaginator

type IPaginator interface {
	IGenericPaginator
	// GetCurrentPage returns the current page.
	GetCurrentPage() (IPage, error)
}

IPaginator is an iterator over multiple pages

func NewCollectionPaginator

func NewCollectionPaginator(ctx context.Context, fetchFirstPageFunc func(context.Context) (IPage, error)) (paginator IPaginator, err error)

NewCollectionPaginator creates a paginator over a collection of dynamic pages.

type IPaginatorAndPageFetcher added in v1.23.0

type IPaginatorAndPageFetcher interface {
	IGenericPaginator
	// GetCurrentPage returns the current page.
	GetCurrentPage() (IStaticPage, error)
	// FetchNextPage fetches the next page.
	FetchNextPage(ctx context.Context, currentPage IStaticPage) (IStaticPage, error)
}

IPaginatorAndPageFetcher is a paginator dealing with static pages

func NewStaticPagePaginator added in v1.23.0

func NewStaticPagePaginator(ctx context.Context, fetchFirstPageFunc func(context.Context) (IStaticPage, error), fetchNextPageFunc func(context.Context, IStaticPage) (IStaticPage, error)) (paginator IPaginatorAndPageFetcher, err error)

NewStaticPagePaginator creates a paginator over a collection but only dealing with static pages i.e. pages from which it is not possible to access the next one and the paginator must define how to fetch them using the fetchNextPageFunc function.

type IStaticPage added in v1.23.0

type IStaticPage interface {
	// HasNext states whether more pages are accessible.
	HasNext() bool
	// GetItemIterator returns a new iterator over the page's items.
	GetItemIterator() (IIterator, error)
	// GetItemCount returns the number of items in this page
	GetItemCount() (int64, error)
}

IStaticPage defines a generic page for a collection.

type IStaticPageStream added in v1.23.0

type IStaticPageStream interface {
	IStaticPage
	// HasFuture states whether there may be future items.
	HasFuture() bool
}

IStaticPageStream defines a page for a collection which does not have any known ending.

type IStream

type IStream interface {
	IPage
	IStaticPageStream
	// GetFuture returns the future page.
	GetFuture(ctx context.Context) (IStream, error)
}

IStream defines a stream with the ability to access future pages.

func GenerateEmptyPage

func GenerateEmptyPage() IStream

func GenerateMockCollection

func GenerateMockCollection() (firstPage IStream, itemTotal int64, err error)

func GenerateMockPage

func GenerateMockPage() (IStream, error)

func GenerateMockStream

func GenerateMockStream() (firstPage IStream, itemTotal int64, err error)

type IStreamPaginator

type IStreamPaginator interface {
	IGenericStreamPaginator
	IPaginator
}

IStreamPaginator is stream paginator over dynamic pages.

func NewStreamPaginator

func NewStreamPaginator(ctx context.Context, runOutTimeOut, backoff time.Duration, fetchFirstPageFunc func(context.Context) (IStream, error)) (paginator IStreamPaginator, err error)

NewStreamPaginator creates a paginator over a stream of dynamic pages i.e. pages can access next and future pages. runOutTimeOut corresponds to the grace period between the stream being marked as running dry and the iteration actually ending backoff corresponds to the backoff time between page iteration.

type IStreamPaginatorAndPageFetcher added in v1.23.0

type IStreamPaginatorAndPageFetcher interface {
	IGenericStreamPaginator
	IPaginatorAndPageFetcher
	// FetchFuturePage returns the future page.
	FetchFuturePage(ctx context.Context, currentPage IStaticPageStream) (IStaticPageStream, error)
}

IStreamPaginatorAndPageFetcher is a stream paginator over static pages.

func NewStaticPageStreamPaginator added in v1.23.0

func NewStaticPageStreamPaginator(ctx context.Context, runOutTimeOut, backoff time.Duration, fetchFirstPageFunc func(context.Context) (IStaticPageStream, error), fetchNextPageFunc func(context.Context, IStaticPage) (IStaticPage, error), fetchFutureFunc func(context.Context, IStaticPageStream) (IStaticPageStream, error)) (paginator IStreamPaginatorAndPageFetcher, err error)

NewStaticPageStreamPaginator creates a paginator over a stream but the pages are static i.e. they cannot access future and next pages from themselves. runOutTimeOut corresponds to the grace period between the stream being marked as running dry and the iteration actually ending backoff corresponds to the backoff time between page iteration.

type MockItem

type MockItem struct {
	Index  int
	ID     string
	Value1 string
	Value2 string
}

func GenerateMockItem

func GenerateMockItem() *MockItem

type MockPage

type MockPage struct {
	// contains filtered or unexported fields
}

func (*MockPage) AppendItem

func (m *MockPage) AppendItem(i *MockItem) error

func (*MockPage) GetFuture

func (m *MockPage) GetFuture(ctx context.Context) (future IStream, err error)

func (*MockPage) GetItemCount

func (m *MockPage) GetItemCount() (int64, error)

func (*MockPage) GetItemIterator

func (m *MockPage) GetItemIterator() (IIterator, error)

func (*MockPage) GetNext

func (m *MockPage) GetNext(ctx context.Context) (page IPage, err error)

func (*MockPage) HasFuture

func (m *MockPage) HasFuture() bool

func (*MockPage) HasNext

func (m *MockPage) HasNext() bool

func (*MockPage) SetFuture

func (m *MockPage) SetFuture(future IStream) error

func (*MockPage) SetIndexes

func (m *MockPage) SetIndexes(firstIndex int)

func (*MockPage) SetNext

func (m *MockPage) SetNext(next IStream) error

type MockPageIterator

type MockPageIterator struct {
	// contains filtered or unexported fields
}

func (*MockPageIterator) GetNext

func (m *MockPageIterator) GetNext() (item interface{}, err error)

func (*MockPageIterator) HasNext

func (m *MockPageIterator) HasNext() bool

type StaticPagePaginator added in v1.23.0

type StaticPagePaginator struct {
	AbstractPaginator
}

StaticPagePaginator defines a paginator over static pages i.e. pages from which it is not possible to access the next one and the paginator must define how to fetch it.

func (*StaticPagePaginator) GetCurrentPage added in v1.23.0

func (d *StaticPagePaginator) GetCurrentPage() (IStaticPage, error)

type StaticPageStreamPaginator added in v1.23.0

type StaticPageStreamPaginator struct {
	AbstractStreamPaginator
}

StaticPageStreamPaginator defines a paginator over a stream of static pages i.e. pages from which it is not possible to access the next nor the future pages.

func (*StaticPageStreamPaginator) GetCurrentPage added in v1.23.0

func (d *StaticPageStreamPaginator) GetCurrentPage() (IStaticPage, error)

Jump to

Keyboard shortcuts

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