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() uintSet valid limits list.
SetLimits(limits ...uint)Get valid limits.
Limits() []uintSet limit.
SetLimit(limit uint)Get limit.
Limit() uintSet valid sorts list.
SetSorts(sorts ...string)Get valid sorts.
Sorts() []stringSet sort.
SetSort(sort string)Get sort.
Sort() stringSet order (valid values are "asc", "desc", "1", "-1", 1 and -1).
SetOrder(order any)Get order.
Order() stringReturn order in 1 and -1.
OrderNumeric() int8Set search phrase.
SetSearch(search string)Get search phrase.
Search() stringSet filters list.
SetFilters(filters map[string]any)Get filters list.
Filters() map[string]anySet filter.
SetFilter(key string, value any)Get filter.
Filter(key string) anyCheck if filter exists.
HasFilter(key string) boolParse filter as caster.
CastFilter(key string) caster.CasterSet meta data.
SetMeta(key string, value any)Get meta.
Meta(key string) anyCheck if meta exists.
HasMeta(key string) boolParse meta as caster.
CastMeta(key string) caster.CasterGet meta data list.
MetaData() map[string]anySet 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() uint64Get from record position.
From() uint64Get to record position.
To() uint64Get total pages count.
Pages() uintGet sql order and limit command as string.
SQLSortOrder() stringGet sql order and limit command as string for postgresql.
PQSortOrder() stringGet response for json, contains pagination information and meta data.
Response() map[string]anyReturn response with data.
ResponseWithData(data any) map[string]any