Servant.API.Modifiers

servant-0.20.2: A family of combinators for defining webservices APIs

Safe Haskell | None
Language | Haskell2010

Servant.API.Modifiers

Contents

- Required / optional argument
- Lenient / strict parsing
- Utilities

Synopsis

- data Required
- data Optional
- type FoldRequired (mods :: [Type]) = FoldRequired' 'False mods
- type family FoldRequired' (acc :: Bool) (mods :: [Type]) :: Bool where ...
- data Lenient
- data Strict
- type FoldLenient (mods :: [Type]) = FoldLenient' 'False mods
- type family FoldLenient' (acc :: Bool) (mods :: [Type]) :: Bool where ...
- type RequiredArgument (mods :: [Type]) a = If (FoldRequired mods) a (Maybe a)
- foldRequiredArgument :: forall (mods :: [Type]) a r. SBoolI (FoldRequired mods) => Proxy mods -> (a -> r) -> (Maybe a -> r) -> RequiredArgument mods a -> r
- unfoldRequiredArgument :: forall (mods :: [Type]) m a. (Monad m, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) => Proxy mods -> m (RequiredArgument mods a) -> (Text -> m (RequiredArgument mods a)) -> Maybe (Either Text a) -> m (RequiredArgument mods a)
- type RequestArgument (mods :: [Type]) a = If (FoldRequired mods) (If (FoldLenient mods) (Either Text a) a) (Maybe (If (FoldLenient mods) (Either Text a) a))
- unfoldRequestArgument :: forall (mods :: [Type]) m a. (Monad m, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) => Proxy mods -> m (RequestArgument mods a) -> (Text -> m (RequestArgument mods a)) -> Maybe (Either Text a) -> m (RequestArgument mods a)

# Required / optional argument

data Required #

Required argument. Not wrapped.

data Optional #

Optional argument. Wrapped in `Maybe`.

type FoldRequired (mods :: [Type]) = FoldRequired' 'False mods #

Fold modifier list to decide whether argument is required.

    >>> :kind! FoldRequired '[Required, Description "something"]
    FoldRequired '[Required, Description "something"] :: Bool
    = 'True

    >>> :kind! FoldRequired '[Required, Optional]
    FoldRequired '[Required, Optional] :: Bool
    = 'False

    >>> :kind! FoldRequired '[]
    FoldRequired '[] :: Bool
    = 'False

type family FoldRequired' (acc :: Bool) (mods :: [Type]) :: Bool where ... #

Implementation of `FoldRequired`.

Equations

FoldRequired' acc ('[] :: [Type]) = acc |
FoldRequired' acc (Required ': mods) = FoldRequired' 'True mods |
FoldRequired' acc (Optional ': mods) = FoldRequired' 'False mods |
FoldRequired' acc (mod ': mods) = FoldRequired' acc mods |

# Lenient / strict parsing

data Lenient #

Leniently parsed argument, i.e. parsing never fail. Wrapped in ``Either` `Text``.

data Strict #

Strictly parsed argument. Not wrapped.

type FoldLenient (mods :: [Type]) = FoldLenient' 'False mods #

Fold modifier list to decide whether argument should be parsed strictly or leniently.

    >>> :kind! FoldLenient '[]
    FoldLenient '[] :: Bool
    = 'False

type family FoldLenient' (acc :: Bool) (mods :: [Type]) :: Bool where ... #

Implementation of `FoldLenient`.

Equations

FoldLenient' acc ('[] :: [Type]) = acc |
FoldLenient' acc (Lenient ': mods) = FoldLenient' 'True mods |
FoldLenient' acc (Strict ': mods) = FoldLenient' 'False mods |
FoldLenient' acc (mod ': mods) = FoldLenient' acc mods |

# Utilities

type RequiredArgument (mods :: [Type]) a = If (FoldRequired mods) a (Maybe a) #

Helper type alias.

- `Required` ↦ `a`
- `Optional` ↦ ``Maybe` a`

foldRequiredArgument #

Arguments

:: forall (mods :: [Type]) a r. SBoolI (FoldRequired mods) |
=> Proxy mods |
-> (a -> r) | `Required`
-> (Maybe a -> r) | `Optional`
-> RequiredArgument mods a |
-> r |

Fold a `RequiredAgument` into a value

unfoldRequiredArgument #

Arguments

:: forall (mods :: [Type]) m a. (Monad m, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) |
=> Proxy mods |
-> m (RequiredArgument mods a) | error when argument is required
-> (Text -> m (RequiredArgument mods a)) | error when argument is strictly parsed
-> Maybe (Either Text a) | value
-> m (RequiredArgument mods a) |

Unfold a value into a `RequiredArgument`.

type RequestArgument (mods :: [Type]) a = If (FoldRequired mods) (If (FoldLenient mods) (Either Text a) a) (Maybe (If (FoldLenient mods) (Either Text a) a)) #

Helper type alias.

By default argument is `Optional` and `Strict`.

- `Required`, `Strict` ↦ `a`
- `Required`, `Lenient` ↦ ``Either` `Text` a`
- `Optional`, `Strict` ↦ ``Maybe` a`
- `Optional`, `Lenient` ↦ ``Maybe` (`Either` `Text` a)`

unfoldRequestArgument #

Arguments

:: forall (mods :: [Type]) m a. (Monad m, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) |
=> Proxy mods |
-> m (RequestArgument mods a) | error when argument is required
-> (Text -> m (RequestArgument mods a)) | error when argument is strictly parsed
-> Maybe (Either Text a) | value
-> m (RequestArgument mods a) |

Unfold a value into a `RequestArgument`.