{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Servant.API.Generic (
GenericMode (..),
GenericServant,
ToServant,
toServant,
fromServant,
AsApi,
ToServantApi,
genericApi,
GServantProduct,
Generic (Rep),
) where
import Data.Kind
(Type)
import Data.Proxy
(Proxy (..))
import GHC.Generics
((:*:) (..), Generic (..), K1 (..), M1 (..))
import Servant.API.Alternative
type GenericServant routes mode = (GenericMode mode, Generic (routes mode), GServantProduct (Rep (routes mode)))
class GenericMode mode where
type mode :- api :: Type
infixl 0 :-
type ToServant routes mode = GToServant (Rep (routes mode))
type ToServantApi routes = ToServant routes AsApi
toServant
:: GenericServant routes mode
=> routes mode -> ToServant routes mode
toServant :: forall {k} (routes :: k -> Type) (mode :: k).
GenericServant routes mode =>
routes mode -> ToServant routes mode
toServant = Rep (routes mode) Any -> GToServant (Rep (routes mode))
forall p. Rep (routes mode) p -> GToServant (Rep (routes mode))
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
f p -> GToServant f
gtoServant (Rep (routes mode) Any -> GToServant (Rep (routes mode)))
-> (routes mode -> Rep (routes mode) Any)
-> routes mode
-> GToServant (Rep (routes mode))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. routes mode -> Rep (routes mode) Any
forall x. routes mode -> Rep (routes mode) x
forall a x. Generic a => a -> Rep a x
from
fromServant
:: GenericServant routes mode
=> ToServant routes mode -> routes mode
fromServant :: forall {k} (routes :: k -> Type) (mode :: k).
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant = Rep (routes mode) Any -> routes mode
forall a x. Generic a => Rep a x -> a
forall x. Rep (routes mode) x -> routes mode
to (Rep (routes mode) Any -> routes mode)
-> (GToServant (Rep (routes mode)) -> Rep (routes mode) Any)
-> GToServant (Rep (routes mode))
-> routes mode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GToServant (Rep (routes mode)) -> Rep (routes mode) Any
forall p. GToServant (Rep (routes mode)) -> Rep (routes mode) p
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
GToServant f -> f p
gfromServant
data AsApi
instance GenericMode AsApi where
type AsApi :- api = api
genericApi
:: GenericServant routes AsApi
=> Proxy routes
-> Proxy (ToServantApi routes)
genericApi :: forall (routes :: Type -> Type).
GenericServant routes AsApi =>
Proxy routes -> Proxy (ToServantApi routes)
genericApi Proxy routes
_ = Proxy (GToServant (Rep (routes AsApi)))
forall {k} (t :: k). Proxy t
Proxy
class GServantProduct f where
type GToServant f
gtoServant :: f p -> GToServant f
gfromServant :: GToServant f -> f p
instance GServantProduct f => GServantProduct (M1 i c f) where
type GToServant (M1 i c f) = GToServant f
gtoServant :: forall (p :: k). M1 i c f p -> GToServant (M1 i c f)
gtoServant = f p -> GToServant f
forall (p :: k). f p -> GToServant f
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
f p -> GToServant f
gtoServant (f p -> GToServant f)
-> (M1 i c f p -> f p) -> M1 i c f p -> GToServant f
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f p -> f p
forall k i (c :: Meta) (f :: k -> Type) (p :: k). M1 i c f p -> f p
unM1
gfromServant :: forall (p :: k). GToServant (M1 i c f) -> M1 i c f p
gfromServant = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 (f p -> M1 i c f p)
-> (GToServant f -> f p) -> GToServant f -> M1 i c f p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GToServant f -> f p
forall (p :: k). GToServant f -> f p
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
GToServant f -> f p
gfromServant
instance (GServantProduct l, GServantProduct r) => GServantProduct (l :*: r) where
type GToServant (l :*: r) = GToServant l :<|> GToServant r
gtoServant :: forall (p :: k). (:*:) l r p -> GToServant (l :*: r)
gtoServant (l p
l :*: r p
r) = l p -> GToServant l
forall (p :: k). l p -> GToServant l
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
f p -> GToServant f
gtoServant l p
l GToServant l -> GToServant r -> GToServant l :<|> GToServant r
forall a b. a -> b -> a :<|> b
:<|> r p -> GToServant r
forall (p :: k). r p -> GToServant r
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
f p -> GToServant f
gtoServant r p
r
gfromServant :: forall (p :: k). GToServant (l :*: r) -> (:*:) l r p
gfromServant (GToServant l
l :<|> GToServant r
r) = GToServant l -> l p
forall (p :: k). GToServant l -> l p
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
GToServant f -> f p
gfromServant GToServant l
l l p -> r p -> (:*:) l r p
forall k (f :: k -> Type) (g :: k -> Type) (p :: k).
f p -> g p -> (:*:) f g p
:*: GToServant r -> r p
forall (p :: k). GToServant r -> r p
forall {k} (f :: k -> Type) (p :: k).
GServantProduct f =>
GToServant f -> f p
gfromServant GToServant r
r
instance GServantProduct (K1 i c) where
type GToServant (K1 i c) = c
gtoServant :: forall (p :: k). K1 i c p -> GToServant (K1 i c)
gtoServant = K1 i c p -> c
K1 i c p -> GToServant (K1 i c)
forall k i c (p :: k). K1 i c p -> c
unK1
gfromServant :: forall (p :: k). GToServant (K1 i c) -> K1 i c p
gfromServant = c -> K1 i c p
GToServant (K1 i c) -> K1 i c p
forall k i c (p :: k). c -> K1 i c p
K1