Lister helps parsing list request (page, limit, sort, order, filters).
Request resolver is a function that parse lister fields from request (string, form, etc.). lister contains following resolver by default:
Note: You can write your own resolver by implementing func(lister Lister, data any) error
signature.
RecordResolver: this resolver take ListRecord struct
as input and parse to lister.
Base64Resolver: this resolver parse lister fields from Base64 encoded json string.
JsonStringResolver: this resolver parse lister fields from json string.
JsonMapperResolver: this resolver parse lister fields from json string and rename sort fields from passed map.
FiberFormResolver: this resolver parse lister fields from goFiber request context (json, form and xml supported).
{
"page": 1,
"limit": 10,
"sort": "name",
"order": "asc",
"search": "john",
"filters": {
"minAge": 25,
"gender": "female",
"permissions": ["acc", "report"]
}
}
import "github.com/gomig/lister"
import "fmt"
lst := lister.New()
lst.SetLimits(10, 25, 50, 100)
lst.SetSorts("_id", "name", "last_activity")
lister.JsonStringResolver(lst,`{"page": 2, "limit": 10}`)
lst.SetTotal(/* Get Total Record Count From Somewhere */)
// Do other operations, paginate and fetch record
fmt.Println(lst.ResponseWithData(myData))
Lister interface contains following methods:
Set current page.
SetPage(page uint)
Get current page.
Page() uint
Set valid limits list.
SetLimits(limits ...uint)
Get valid limits.
Limits() []uint
Set limit.
SetLimit(limit uint)
Get limit.
Limit() uint
Set valid sorts list.
SetSorts(sorts ...string)
Get valid sorts.
Sorts() []string
Set sort.
SetSort(sort string)
Get sort.
Sort() string
Set order (valid values are "asc"
, "desc"
, "1"
, "-1"
, 1
and -1
).
SetOrder(order any)
Get order.
Order() string
Return order in 1 and -1.
OrderNumeric() int8
Set search phrase.
SetSearch(search string)
Get search phrase.
Search() string
Set filters list.
SetFilters(filters map[string]any)
Get filters list.
Filters() map[string]any
Set filter.
SetFilter(key string, value any)
Get filter.
Filter(key string) any
Check if filter exists.
HasFilter(key string) bool
Parse filter as caster.
CastFilter(key string) caster.Caster
Set meta data.
SetMeta(key string, value any)
Get meta.
Meta(key string) any
Check if meta exists.
HasMeta(key string) bool
Parse meta as caster.
CastMeta(key string) caster.Caster
Get meta data list.
MetaData() map[string]any
Set total records count. You must pass total records count to this method for getting paginator information.
Caution: Call this method after setting all lister fields(page, limits, etc).
SetTotal(total uint64)
Get total records count.
Total() uint64
Get from record position.
From() uint64
Get to record position.
To() uint64
Get total pages count.
Pages() uint
Get sql order and limit command as string.
SQLSortOrder() string
Get sql order and limit command as string for postgresql.
PQSortOrder() string
Get response for json, contains pagination information and meta data.
Response() map[string]any
Return response with data.
ResponseWithData(data any) map[string]any