24 lines
paginator/paginator.go
Converts a page request into a zero-based query offset.
// Package paginator calculates query offsets for paginated API responses.
package paginator
 
// PageRequest carries the pagination parameters for a list query.
// Page is 1-indexed: page 1 returns the first PageSize records at offset 0.
type PageRequest struct {
	Page     int // 1-indexed; must be >= 1
	PageSize int // maximum records per page
}
 
// Offset returns the zero-based starting offset for a database query.
// Page 1 must return offset 0; page 2 returns PageSize; and so on.
// Parameters: pr — the pagination request.
// Returns: the zero-based record offset.
func Offset(pr PageRequest) int {
	return pr.Page * pr.PageSize
}
 
// HasNextPage reports whether a following page may exist.
// Returns true when count equals pageSize, indicating another page may be non-empty.
// Parameters: count — records returned in the current page; pageSize — records per page.
func HasNextPage(count, pageSize int) bool {
	return count == pageSize
}