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.
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.
CurrentPage uint64 `json:"current_page"` // The current page number.
NextPage *uint64 `json:"next_page"` // The number of the next page (if possible).
PrevPage *uint64 `json:"prev_page"` // The number of the previous page (if possible).
}
Response manages pagination of a data set.
func MakeResponse ¶
MakeResponse returns a new Response with the provided parameters set.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/LUSHDigital/core/pagination"
)
func main() {
preq := pagination.Request{
PerPage: 10,
Page: 1,
}
presp := pagination.MakeResponse(preq, 100)
raw, _ := json.Marshal(presp)
fmt.Println(string(raw))
}
Output: {"per_page":10,"offset":0,"total":100,"last_page":10,"current_page":1,"next_page":2,"prev_page":null}
Example (WithOffset) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/LUSHDigital/core/pagination"
)
func main() {
preq := pagination.Request{
PerPage: 10,
Page: 2,
}
presp := pagination.MakeResponse(preq, 100)
raw, _ := json.Marshal(presp)
fmt.Println(string(raw))
}
Output: {"per_page":10,"offset":10,"total":100,"last_page":10,"current_page":2,"next_page":3,"prev_page":1}
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, "offset": 10, "total": 100, "last_page": 10, "current_page": 2, "next_page": 3, "prev_page": 1 } }
Click to show internal directories.
Click to hide internal directories.