Skip to content
/ lister Public

List request parser and pagination generator

License

Notifications You must be signed in to change notification settings

gomig/lister

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lister

Lister helps parsing list request (page, limit, sort, order, filters).

Requirements

RequestResolver

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).

Request Fields Signature

{
  "page": 1,
  "limit": 10,
  "sort": "name",
  "order": "asc",
  "search": "john",
  "filters": {
    "minAge": 25,
    "gender": "female",
    "permissions": ["acc", "report"]
  }
}

Create Lister

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))

Usage

Lister interface contains following methods:

SetPage

Set current page.

SetPage(page uint)

Page

Get current page.

Page() uint

SetLimits

Set valid limits list.

SetLimits(limits ...uint)

Limits

Get valid limits.

Limits() []uint

SetLimit

Set limit.

SetLimit(limit uint)

Limit

Get limit.

Limit() uint

SetSorts

Set valid sorts list.

SetSorts(sorts ...string)

Sorts

Get valid sorts.

Sorts() []string

SetSort

Set sort.

SetSort(sort string)

Sort

Get sort.

Sort() string

SetOrder

Set order (valid values are "asc", "desc", "1", "-1", 1 and -1).

SetOrder(order any)

Order

Get order.

Order() string

OrderNumeric

Return order in 1 and -1.

OrderNumeric() int8

SetSearch

Set search phrase.

SetSearch(search string)

Search

Get search phrase.

Search() string

SetFilters

Set filters list.

SetFilters(filters map[string]any)

Filters

Get filters list.

Filters() map[string]any

SetFilter

Set filter.

SetFilter(key string, value any)

Filter

Get filter.

Filter(key string) any

HasFilter

Check if filter exists.

HasFilter(key string) bool

CastFilter

Parse filter as caster.

CastFilter(key string) caster.Caster

SetMeta

Set meta data.

SetMeta(key string, value any)

Meta

Get meta.

Meta(key string) any

HasMeta

Check if meta exists.

HasMeta(key string) bool

CastMeta

Parse meta as caster.

CastMeta(key string) caster.Caster

MetaData

Get meta data list.

MetaData() map[string]any

SetTotal

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)

Total

Get total records count.

Total() uint64

From

Get from record position.

From() uint64

To

Get to record position.

To() uint64

Pages

Get total pages count.

Pages() uint

SQLSortOrder

Get sql order and limit command as string.

SQLSortOrder() string

PQSortOrder

Get sql order and limit command as string for postgresql.

PQSortOrder() string

Response

Get response for json, contains pagination information and meta data.

Response() map[string]any

ResponseWithData

Return response with data.

ResponseWithData(data any) map[string]any