Documentation ¶
Overview ¶
Package pagination defines a paginator able to return formatted responses enabling the API consumer to retrieve data in defined chunks
Index ¶
Examples ¶
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") // ErrMetadataMissing happens when there is no metadata with the request ErrMetadataMissing = status.Error(codes.InvalidArgument, "metadata missing") // ErrMetadataInvalid happens when a metadata key is invalid or missing ErrMetadataInvalid = func(key string, err error) error { return status.Error(codes.InvalidArgument, fmt.Sprintf("invalid or missing [%s]: %v", key, err)) } )
Functions ¶
Types ¶
type Request ¶
type Request struct {
PerPage, Page uint64
}
Request derives the requested pagination data given by the client.
func RequestFromContext ¶
RequestFromContext extracts the pagination request from the supplied context.
type Response ¶
type Response struct { PerPage uint64 `json:"per_page"` // The number of items per page. Page uint64 `json:"page"` // Which page are we on? Offset uint64 `json:"offset"` // The current offset to pass to the query. Total uint64 `json:"total"` // The total number of items LastPage uint64 `json:"last_page"` // The number of the last possible page. }
Response manages pagination of a data set.
func MakeResponse ¶
MakeResponse returns a new Response with the provided parameters set.
Example ¶
package main import ( "fmt" "github.com/LUSHDigital/core/pagination" ) func main() { preq := pagination.Request{ PerPage: 10, Page: 1, } presp := pagination.MakeResponse(preq, 100) fmt.Printf("%+v\n", presp) }
Output: {PerPage:10 Page:1 Offset:0 Total:100 LastPage:10}
Example (WithOffset) ¶
package main import ( "fmt" "github.com/LUSHDigital/core/pagination" ) func main() { preq := pagination.Request{ PerPage: 10, Page: 2, } presp := pagination.MakeResponse(preq, 100) fmt.Printf("%+v\n", presp) }
Output: {PerPage:10 Page:2 Offset:10 Total:100 LastPage:10}
Example (WithinResponse) ¶
package main import ( "encoding/json" "fmt" "net/http" "github.com/LUSHDigital/core/pagination" "github.com/LUSHDigital/core/response" ) func main() { preq := pagination.Request{ PerPage: 10, Page: 2, } presp := pagination.MakeResponse(preq, 100) resp := response.Response{ Code: http.StatusOK, Message: "some helpful message", Data: &response.Data{ Type: "some_data", Content: map[string]interface{}{"hello": "world"}, }, Pagination: &presp, } raw, _ := json.MarshalIndent(resp, "", "\t") fmt.Println(string(raw)) }
Output: { "code": 200, "message": "some helpful message", "data": { "some_data": { "hello": "world" } }, "pagination": { "per_page": 10, "page": 2, "offset": 10, "total": 100, "last_page": 10 } }
Click to show internal directories.
Click to hide internal directories.