paginator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 2 Imported by: 0

README

paginator

Simple implementation pagination mechanics for Golang

example for slice data source
package main

import (
	"context"
	"log"

	"github.com/Mikhalevich/paginator"
	"github.com/Mikhalevich/paginator/queryerslice"
)

const (
	dataLen  = 101
	pageSize = 10
)

func main() {
	var (
		ctx   = context.Background()
		pagin = paginator.New(NewSliceProvider(), pageSize)
		page  *paginator.Page[int]
	)

	page, _ = pagin.Page(ctx, 1)

	printPage(page)

	for page.HasNext() {
		page, _ = pagin.Page(ctx, page.Next())
		printPage(page)
	}
}

func printPage(page *paginator.Page[int]) {
	log.Printf("bottom index: %d top index: %d, page number: %d total pages: %d page data: %v",
		page.BottomIndex, page.TopIndex, page.PageNumber, page.PageTotalCount, page.Data)
}

func NewSliceProvider() *queryerslice.QueryerSlice[int] {
	data := make([]int, 0, dataLen)
	for i := range dataLen {
		data = append(data, i+1)
	}

	return queryerslice.New(data)
}

License

Paginator is released under the MIT License.

Documentation

Overview

Package paginator implements simple pagination mechanics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Page

type Page[T any] struct {
	Data           []T
	BottomIndex    int
	TopIndex       int
	PageSize       int
	PageNumber     int
	PageTotalCount int
}

Page represents single page information.

func (*Page[T]) HasNext

func (p *Page[T]) HasNext() bool

HasNext returns true if next page is available.

func (*Page[T]) HasPrevious

func (p *Page[T]) HasPrevious() bool

HasPrevious returns true if previous page is available.

func (*Page[T]) Next

func (p *Page[T]) Next() int

Next returns next page number.

func (*Page[T]) Previous

func (p *Page[T]) Previous() int

Previous returns previous page number.

type Paginator

type Paginator[T any] struct {
	// contains filtered or unexported fields
}

Paginator structure.

func New

func New[T any](queryer Queryer[T], pageSize int) *Paginator[T]

New construct paginator.

func (*Paginator[T]) Page

func (p *Paginator[T]) Page(ctx context.Context, page int) (*Page[T], error)

Page returns information about page by it's number.

type Queryer

type Queryer[T any] interface {
	Query(ctx context.Context, offset int, limit int) ([]T, error)
	Count(ctx context.Context) (int, error)
}

Queryer interface for external implementation for paginator usage.

Directories

Path Synopsis
examples
slicepaginator command
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.
Package queryercache provides cache for paginator Queryer interface.
Package queryercache provides cache for paginator Queryer interface.
metrics
Package metrics provides metric support for QueryerCache.
Package metrics provides metric support for QueryerCache.
Package queryerslice provides slice implementation for paginator Queryer interface.
Package queryerslice provides slice implementation for paginator Queryer interface.

Jump to

Keyboard shortcuts

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