pagination

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 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 (*AbstractPaginator) Close

func (a *AbstractPaginator) Close() error

func (*AbstractPaginator) GetContext

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

func (*AbstractPaginator) GetCurrentPage

func (a *AbstractPaginator) GetCurrentPage() (page IPage, err error)

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 IPage) (err error)

func (*AbstractPaginator) Stop

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 {
	// HasNext states whether more pages are accessible.
	HasNext() bool
	// GetNext returns the next page.
	GetNext(ctx context.Context) (IPage, error)
	// GetItemIterator returns an iterator over the page's items.
	GetItemIterator() (IIterator, error)
	// GetItemCount returns the number of items in this page
	GetItemCount() (int64, error)
}

IPage defines a generic page for a collection.

type IPaginator

type IPaginator interface {
	io.Closer
	IIterator
	// Stop returns a stop function which stops the iteration.
	Stop() context.CancelFunc
	// GetCurrentPage returns the current page.
	GetCurrentPage() (IPage, error)
}

IPaginator is an iterator over multiple pages

func NewAbstractPaginator

func NewAbstractPaginator(ctx context.Context, firstPage IPage) IPaginator

func NewCollectionPaginator

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

NewCollectionPaginator creates a paginator over a collection.

type IStream

type IStream interface {
	IPage
	// HasFuture states whether there may be future items.
	HasFuture() bool
	// GetFuture returns the future page.
	GetFuture(ctx context.Context) (IPage, error)
}

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

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 {
	IPaginator
	// 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
}

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

func NewStreamPaginator

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

NewStreamPaginator creates a paginator over a stream. 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 IPage, err error)

func (*MockPage) GetItemCount

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

func (*MockPage) GetItemIterator

func (m *MockPage) GetItemIterator() (iterator IIterator, err 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 StreamPaginator

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

func (*StreamPaginator) Close

func (s *StreamPaginator) Close() error

func (*StreamPaginator) DryUp

func (s *StreamPaginator) DryUp() error

func (*StreamPaginator) GetCurrentPage

func (s *StreamPaginator) GetCurrentPage() (IPage, error)

func (*StreamPaginator) GetNext

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

func (*StreamPaginator) HasNext

func (s *StreamPaginator) HasNext() bool

func (*StreamPaginator) IsRunningDry

func (s *StreamPaginator) IsRunningDry() bool

func (*StreamPaginator) Stop

Jump to

Keyboard shortcuts

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