Safe Haskell | None |
---|---|
Language | Haskell2010 |
Provides combinator for lexicographical sorting.
Synopsis
- data SortingParams (provided :: [TyNamedParam *]) (base :: [TyNamedParam (SortingOrderType *)])
- data SortingSpec (provided :: [TyNamedParam *]) (base :: [TyNamedParam (SortingOrderType *)])
- data SortingOrderType k
- type family SortingParamProvidedOf a :: [TyNamedParam *]
- type family SortingParamBaseOf a :: [TyNamedParam (SortingOrderType *)]
- type SortingParamsOf a = SortingParams (SortingParamProvidedOf a) (SortingParamBaseOf a)
- type SortingSpecOf a = SortingSpec (SortingParamProvidedOf a) (SortingParamBaseOf a)
- data SortingRequestItem (provided :: [TyNamedParam *])
- asc :: forall name provided. (KnownSymbol name, KnownTypeName provided name provided) => NameLabel name -> SortingRequestItem provided
- desc :: forall name provided. (KnownSymbol name, KnownTypeName provided name provided) => NameLabel name -> SortingRequestItem provided
- mkSortingSpec :: ReifySortingItems base => [SortingRequestItem provided] -> SortingSpec provided base
- noSorting :: ReifySortingItems base => SortingSpec provided base
- class ReifySortingItems (items :: [TyNamedParam (SortingOrderType *)])
- type (?:) = 'TyNamedParam
General
data SortingParams (provided :: [TyNamedParam *]) (base :: [TyNamedParam (SortingOrderType *)]) Source #
Servant API combinator which allows to accept sorting parameters as a query parameter.
Example: with the following combinator
SortingParams ["time" ?: Timestamp, "name" ?: Text] '[]
the endpoint can parse "sortBy=-time,+name" or "sortBy=desc(time),asc(name)" formats, which would mean sorting by mentioned fields lexicographically. All sorting subparameters are optional, as well as entire "sortBy" parameter.
The second type-level list stands for the base sorting order, it will be applied in the end disregard the user's input. It is highly recommended to specify the base sorting that unambigously orders the result(for example - by the primary key of the database), otherwise pagination may behave unexpectedly for the client when it specifies no sorting.
If you want the base sorting order to be overridable by the user, you can put the respective fields in both lists. For example, this combinator:
SortingParams '["time" ?: Timestamp] ["id" ?: '(Id, 'Descendant), "time" ?: '(Timestamp, 'Ascendant)]
will sort results lexicographically by (Down id, time)
, but if the client
specifies sorting by time, you will get sorting by (time, Down id)
as the
trailing "time"
will not affect anything.
It is preferred to put a base sorting at least by ID
, this way results will be
more deterministic.
Your handler will be provided with SortingSpec
argument which can later be passed
in an appropriate function to perform sorting.
Instances
data SortingSpec (provided :: [TyNamedParam *]) (base :: [TyNamedParam (SortingOrderType *)]) Source #
What is passed to an endpoint, contains all sorting parameters provided by a user.
Instances
data SortingOrderType k Source #
Order of sorting for type-level.
Its constructors accept the type of thing we order by, e.g. Asc Id
.
Instances
ReifySortingItems ('[] :: [TyNamedParam (SortingOrderType Type)]) Source # | |
Defined in Servant.Util.Combinators.Sorting.Base | |
(ReifySortingOrder order, KnownSymbol name, ReifySortingItems items) => ReifySortingItems ('TyNamedParam name (order field) ': items) Source # | |
Defined in Servant.Util.Combinators.Sorting.Base |
Shortcuts
type family SortingParamProvidedOf a :: [TyNamedParam *] Source #
For a given return type of an endpoint get corresponding sorting params that can be specified by user. This mapping is sensible, since we usually allow to sort only on fields appearing in endpoint's response.
Instances
type SortingParamProvidedOf NoContent Source # | |
Defined in Servant.Util.Combinators.Sorting.Base |
type family SortingParamBaseOf a :: [TyNamedParam (SortingOrderType *)] Source #
For a given return type of an endpoint get corresponding base sorting params.
Instances
type SortingParamBaseOf NoContent Source # | |
Defined in Servant.Util.Combinators.Sorting.Base |
type SortingParamsOf a = SortingParams (SortingParamProvidedOf a) (SortingParamBaseOf a) Source #
This you will most probably want to specify in API.
type SortingSpecOf a = SortingSpec (SortingParamProvidedOf a) (SortingParamBaseOf a) Source #
This you will most probably want to specify in an endpoint implementation.
Manual construction
data SortingRequestItem (provided :: [TyNamedParam *]) Source #
Helper for defining custom SortingSpec
s,
contains SortingItem
corresponding to one of parameter in provided
list.
Instances
Show (SortingRequestItem provided) Source # | |
Defined in Servant.Util.Combinators.Sorting.Construction showsPrec :: Int -> SortingRequestItem provided -> ShowS # show :: SortingRequestItem provided -> String # showList :: [SortingRequestItem provided] -> ShowS # |
asc :: forall name provided. (KnownSymbol name, KnownTypeName provided name provided) => NameLabel name -> SortingRequestItem provided Source #
Ascendant sorting on a field with given name.
desc :: forall name provided. (KnownSymbol name, KnownTypeName provided name provided) => NameLabel name -> SortingRequestItem provided Source #
Ascendant sorting on a field with given name.
mkSortingSpec :: ReifySortingItems base => [SortingRequestItem provided] -> SortingSpec provided base Source #
Make a sorting specification. Specified list should contain sorting on distinct fields; we do not enforce this at type-level for convenience.
Example:
-- sortingSpec :: SortingSpec ["id" ?: Int, "desc" ?: Text] sortingSpec = mkSortingSpec [asc #id]
noSorting :: ReifySortingItems base => SortingSpec provided base Source #
Do not specify ordering.
class ReifySortingItems (items :: [TyNamedParam (SortingOrderType *)]) Source #
Requires given type-level items to be valid specification of sorting.
Instances
ReifySortingItems ('[] :: [TyNamedParam (SortingOrderType Type)]) Source # | |
Defined in Servant.Util.Combinators.Sorting.Base | |
(ReifySortingOrder order, KnownSymbol name, ReifySortingItems items) => ReifySortingItems ('TyNamedParam name (order field) ': items) Source # | |
Defined in Servant.Util.Combinators.Sorting.Base |
Re-exports
type (?:) = 'TyNamedParam Source #
Convenient type alias for TyNamedParam
.