pagination

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2020 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIteratorFinished = errors.New("iterator has reached end")

ErrIteratorFinished indicates that Next was called on a finished iterator

Functions

This section is empty.

Types

type Entity

type Entity interface{}

Entity is a generic type which can be operated on by Iterator and Writer

type FetchFn

type FetchFn func(PageToken) (Page, error)

FetchFn fetches Page from PageToken. Once a page with nil NextToken is returned no more pages will be fetched.

type Iterator

type Iterator interface {
	Next() (Entity, error)
	HasNext() bool
}

Iterator is used to get entities from a collection of pages. When HasNext returns true it is guaranteed that Next will not return an error. Once iterator returns an error it will never make progress again and will always return that same error. Iterator is not thread safe and does not make defensive in or out copies.

func NewIterator

func NewIterator(
	startingPageToken PageToken,
	fetchFn FetchFn,
) Iterator

NewIterator constructs a new Iterator

type Page

type Page struct {
	NextToken    PageToken
	CurrentToken PageToken
	Entities     []Entity
}

Page contains a PageToken which identifies the current page, a PageToken which identifies the next page and a list of Entity.

type PageToken

type PageToken interface{}

PageToken identifies a page

type ShouldFlushFn

type ShouldFlushFn func(Page) bool

ShouldFlushFn returns true if given page should be flushed false otherwise.

type WriteFn

type WriteFn func(Page) (PageToken, error)

WriteFn writes given Page to underlying sink. The Pages's NextToken will always be nil, its the responsibility of WriteFn to construct and return the next PageToken, or return an error on failure.

type Writer

type Writer interface {
	Add(Entity) error
	Flush() error
	FlushIfNotEmpty() error
	FlushedPages() []PageToken
	FirstFlushedPage() PageToken
	LastFlushedPage() PageToken
}

Writer is used to buffer and write entities to underlying store.

func NewWriter

func NewWriter(
	writeFn WriteFn,
	shouldFlushFn ShouldFlushFn,
	startingPage PageToken,
) Writer

NewWriter constructs a new Writer

Jump to

Keyboard shortcuts

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