pagination

package
v3.5.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Package pagination defines a paginator able to return formatted responses enabling the API consumer to retrieve data in defined chunks

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCalculateOffset is used when the pagination offset could not be calculated.
	ErrCalculateOffset = errors.New("cannot calculate offset: insufficient data")

	// ErrCalculateLastPage is used when the pagination last page could not be calculated.
	ErrCalculateLastPage = errors.New("cannot calculate last page: insufficient data")
)

Functions

This section is empty.

Types

type Paginator

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

Paginator manages pagination of a data set.

func NewPaginator

func NewPaginator(perPage, page, total int) (paginator *Paginator, err error)

NewPaginator returns a new Paginator instance with the provided parameters set and returns an error if it fails.

func (*Paginator) GetLastPage

func (p *Paginator) GetLastPage() int

GetLastPage returns the last possible page number.

func (*Paginator) GetOffset

func (p *Paginator) GetOffset() int

GetOffset returns the current offset of the paginator.

func (*Paginator) GetPage

func (p *Paginator) GetPage() int

GetPage returns the current page index

func (*Paginator) GetPerPage

func (p *Paginator) GetPerPage() int

GetPerPage returns the number of items per page.

func (*Paginator) GetTotal

func (p *Paginator) GetTotal() int

GetTotal returns the total number of items in the paginator.

func (*Paginator) PrepareResponse

func (p *Paginator) PrepareResponse() *Response

PrepareResponse returns a prepared pagination response.

func (*Paginator) SetPage

func (p *Paginator) SetPage(page int) error

SetPage sets the page field to the provided value and returns an error if anything fails

func (*Paginator) SetPerPage

func (p *Paginator) SetPerPage(perPage int) error

SetPerPage defines how many items per page the paginator will return, based on the supplied parameter, and will return an error if anything fails.

func (*Paginator) SetTotal

func (p *Paginator) SetTotal(total int) error

SetTotal sets the total number of items in the paginator to the provided value and returns an error if it fails.

type Response

type Response struct {
	Total       int  `json:"total"`        // The total number of items.
	PerPage     int  `json:"per_page"`     //  Number of items displayed per page.
	CurrentPage int  `json:"current_page"` // The current page number.
	LastPage    int  `json:"last_page"`    // The number of the last possible page.
	NextPage    *int `json:"next_page"`    // The number of the next page (if possible).
	PrevPage    *int `json:"prev_page"`    // The number of the previous page (if possible).
}

Response represents a pagination response.

Jump to

Keyboard shortcuts

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