pagination

package
v0.0.386 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: Apache-2.0 Imports: 5 Imported by: 17

README

pagination

CircleCI

A simple helper for dealing with pagination.

go get github.com/ory/pagination

Example

package main

import (
	"github.com/ory/pagination"
    "net/http"
    "net/url"
    "fmt"
)

func main() {
	u, _ := url.Parse("http://localhost/foo?offset=0&limit=10")
    limit, offset := pagination.Parse(&http.Request{URL: u}, 5, 5, 10)

    items := []string{"a", "b", "c", "d"}
    start, end := pagination.Index(limit, offset, len(items))
    fmt.Printf("Got items: %v", items[start:end])
}

Documentation

Overview

Package pagination provides helpers for dealing with pagination.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Header(w http.ResponseWriter, u *url.URL, total int, limit, offset int)

Header adds an http header for pagination using a responsewriter where backwards compatibility is required. The header will contain links any combination of the first, last, next, or previous (prev) pages in a paginated list (given a limit and an offset, and optionally a total). If total is not set, then no "last" page will be calculated. If no limit is provided, then it will default to 1.

func Index

func Index(limit, offset, length int) (start, end int)

Index uses limit, offset, and a slice's length to compute start and end indices for said slice.

func MaxItemsPerPage added in v0.0.307

func MaxItemsPerPage(max, is int) int

MaxItemsPerPage is used to prevent DoS attacks against large lists by limiting the items per page to 500.

func Parse

func Parse(r *http.Request, defaultLimit, defaultOffset, maxLimit int) (int, int)

Parse parses limit and offset from *http.Request with given limits and defaults.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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