Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/gemcook/pagination-go

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gemcook/pagination-go

  • v0.2.1
  • Source
  • Go
  • Socket score

Version published
Created
Source

pagination-go

CircleCI Coverage Status

This is a helper library which perfectly matches for server-side implementation of @gemcook/pagination

Installation

go get -u github.com/gemcook/pagination-go

If you use dep

dep ensure -add github.com/gemcook/pagination-go

Usage

fetcher interface

Your fetching object must implement fetcher interface.

type PageFetcher interface {
	Count(cond interface{}) (int, error)
	FetchPage(cond interface{}, input *PageFetchInput, result *PageFetchResult) error
}

type PageFetchInput struct {
	Limit  int
	Offset int
	Orders []*Order
}

parse Function

Package pagination provides ParseQuery and ParseMap functions that parses Query Parameters from request URL. Those query parameters below will be parsed.

query parameterMapped fieldrequiredexpected valuedefault value
limitLimitnopositive integer10
pagePagenopositive integer (1~)1
paginationEnablednobooleantrue
Query String from URL
// RequestURI: https://example.com/fruits?limit=10&page=1&price_range=100,300&sort=+price&pagination=true
p := pagination.ParseQuery(r.URL.RequestURI())

fmt.Println("limit =", p.Limit)
fmt.Println("page =", p.Page)
fmt.Println("pagination =", p.Enabled)
Query Parameters from AWS API Gateway - Lambda
import "github.com/aws/aws-lambda-go/events"

func Handler(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	// event.QueryStringParameters
	// map[string]string{"limit": "10", "page": "1", "pagination": "false"}

	p := pagination.ParseMap(event.QueryStringParameters)
	fmt.Println("limit =", p.Limit)
	fmt.Println("page =", p.Page)
	fmt.Println("pagination =", p.Enabled)
}

fetching condition [OPTIONAL]

Tell pagination the condition to filter resources. Then use cond interface{} in Count and FetchPage function. Use type assertion for cond to restore your fetching condition object.

Orders [OPTIONAL]

Optionally, pagination takes orders. Use pagination.ParseQuery or pagination.ParseMap to parse sort parameter in query string. Then, just pass Query.Sort to Setting.Orders.

Those query parameters below will be parsed.

query parameterMapped fieldrequiredexpected valuedefault value
sortSortno+column_name for ascending sort.
-column_name for descending sort.
nil

Example


import (
    "http/net"
    "encoding/json"
    "strconv"

    "github.com/gemcook/pagination-go"
)

type fruitFetcher struct{}

type FruitCondition struct{
    PriceLowerLimit int
    PriceHigherLimit int
}

func ParseFruitCondition(uri string) *FruitCondition {
    // parse uri and initialize struct
}

func handler(w http.ResponseWriter, r *http.Request) {
	// RequestURI: https://example.com/fruits?limit=10&page=1&price_range=100,300&sort=+price
	p := pagination.ParseQuery(r.URL.RequestURI())
	cond := parseFruitCondition(r.URL.RequestURI())
	fetcher := newFruitFetcher()

	totalCount, totalPages, res, err := pagination.Fetch(fetcher, &pagination.Setting{
		Limit:      p.Limit,
		Page:       p.Page,
		Cond:       cond,
		Orders:     p.Sort,
	})

	if err != nil {
		w.Header().Set("Content-Type", "text/html; charset=utf8")
		w.WriteHeader(400)
		fmt.Fprintf(w, "something wrong: %v", err)
		return
	}

	w.Header().Set("X-Total-Count", strconv.Itoa(totalCount))
	w.Header().Set("X-Total-Pages", strconv.Itoa(totalPages))
	w.Header().Set("Access-Control-Expose-Headers", "X-Total-Count,X-Total-Pages")
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(200)
	resJSON, _ := json.Marshal(res)
	w.Write(resJSON)
}

For full source code, see example/server.go.

Run example.

cd example
go run server.go

Then open http://localhost:8080/fruits?limit=2&page=1&price_range=100,300&sort=+price

FAQs

Package last updated on 10 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc