Documentation
¶
Overview ¶
Package helpers contains utility functions which are highly opionated and probably only applicable for specific use cases.
Such use cases include consistent response structure for RESTful APIs, a predefined paginated response structure that also maintains the same structure defined for non-paginated RESTful APIs.
Constants defined in this package are purely to maintain a pre-defined response structure.
Index ¶
- Constants
- func CountTotalPages(limit, totalItems int) int
- func GetSortingData(r *http.Request, defaultSortBy, defaultSortOrder string) (sortBy, sortOrder string)
- func NewValue(v any) (reflect.Value, error)
- func SendData(w http.ResponseWriter, data interface{})
- func SendError(w http.ResponseWriter, status int, message string, data interface{})
- func SendJSON(w http.ResponseWriter, status int, data any)
- func SendPage(w http.ResponseWriter, page Page)
- func ToJSON(data any) string
- type FilterParams
- type Page
- type PaginationParams
Constants ¶
const ( // MaxPageSize limits the maximum possible page size. MaxPageSize = 100 // DefaultPageSize sets the default page size. DefaultPageSize = 10 // PageKey is the search query key for PaginationParams.Page. PageKey = "pageNumber" // LimitKey is the search query key for PaginationParams.Limit. LimitKey = "itemsPerPage" // SearchKey is the search query key for PaginationParams.Search. SearchKey = "search" // SortByKey is the search query key for the PaginationParams.SortBy. SortByKey = "sortBy" // SortOrderKey is the search query key for the PaginationParams.SortOrder. SortOrderKey = "sortOrder" )
const ErrorMsg = "Sorry, something went wrong! Please try again later."
ErrorMsg is common error message sent to the client if something went wrong unexpectedly while processing the request
Variables ¶
This section is empty.
Functions ¶
func CountTotalPages ¶
CountTotalPages counts total number of pages from total item count and per page item count.
func GetSortingData ¶
func GetSortingData( r *http.Request, defaultSortBy, defaultSortOrder string, ) (sortBy, sortOrder string)
GetSortingData parses sorting related parameters from url search query. This should not be directly used as it only calls GetPaginationParams under the hood.
func SendData ¶
func SendData(w http.ResponseWriter, data interface{})
SendData writes JSON marshalled data to the response in pre-defined structure.
The response structure will be -
{
"status": true,
"message": "Success",
"data": data
}
func SendError ¶
func SendError(w http.ResponseWriter, status int, message string, data interface{})
SendError writes data with specified status code to the response with status set to false.
The response structure will be -
{
"status": false,
"message": message,
"data": data
}
func SendJSON ¶ added in v1.5.0
func SendJSON(w http.ResponseWriter, status int, data any)
SendJSON writes JSON data to w.
func SendPage ¶
func SendPage(w http.ResponseWriter, page Page)
SendPage writes a pagination response in the pre-defined response structure.
Pagination response structure -
{
"status": true,
"message": "Success",
"data": {
"pageNumber": number,
"itemsPerPage": number,
"totalItems": number,
"totalPages": number,
"items": []any
}
}
Types ¶
type FilterParams ¶
FilterParams is collection of query parameters found in the search queries of the incoming request. This will contain only the parameters which are not defined by the pagination constants like pageNumber, itemsPerPage, search etc.
type Page ¶
type Page struct {
// Items are the list items
Items interface{} `json:"items"`
// ItemsPerPage is the number of items per page
ItemsPerPage int `json:"itemsPerPage"`
// PageNumber is the current page number
PageNumber int `json:"pageNumber"`
// TotalItems is the total number of items found
TotalItems int `json:"totalItems"`
// TotalPages is the total number of pages available
TotalPages int `json:"totalPages"`
}
Page defines the data structure send for pagination response's data field.
type PaginationParams ¶
type PaginationParams struct {
// Page is the requested page number
Page int
// Limit is the number of items requested per page
Limit int
// Search is the common search term query
Search string
// SortBy is the name of the property which should be used for sorting the list
SortBy string
// SortOrder is the requested sort order.
// It's value can be application specific, for example asc, desc could be
// used to sort in ascending and descending order respectively, or any other
// value defined by the application.
SortOrder string
// Filters are the additional filter parameters requested.
// These are also application specific application may define any
// filter query for any REST API, for example, <url>?isActive=true
// defines a filter isActive with value true.
Filters FilterParams
}
PaginationParams container for pagination parameters collected from url search queries.
func GetPaginationParams ¶
func GetPaginationParams(r *http.Request, defaultSortBy, defaultSortOrder string) PaginationParams
GetPaginationParams parses the request's search query and constructs PaginationParams instance. There are a couple of pre-defined keys for pre-defined puposes, check the constants defined in this package. Any other key which is not a pre-defined key will be considered as an application specific filter query and added to the PaginationParams.FilterParams.