servant/servant-server/test/Servant/Server/UsingContextSpec/TestCombinators.hs

80 lines
2.5 KiB
Haskell
Raw Normal View History

2018-06-29 21:08:26 +02:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
2016-02-28 23:23:32 +01:00
{-# LANGUAGE MultiParamTypeClasses #-}
2018-06-29 21:08:26 +02:00
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
2016-02-28 23:23:32 +01:00
-- | These are custom combinators for Servant.Server.UsingContextSpec.
--
-- (For writing your own combinators you need to import Internal modules, for
-- just *using* combinators that require a Context, you don't. This module is
-- separate from Servant.Server.UsingContextSpec to test that the module imports
-- work out this way.)
module Servant.Server.UsingContextSpec.TestCombinators where
import GHC.TypeLits
import Servant
data ExtractFromContext
instance (HasContextEntry context String, HasServer subApi context) =>
HasServer (ExtractFromContext :> subApi) context where
type ServerT (ExtractFromContext :> subApi) m =
String -> ServerT subApi m
2017-10-01 18:20:09 +02:00
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy subApi) pc nt . s
2017-09-08 18:21:16 +02:00
2016-02-28 23:23:32 +01:00
route Proxy context delayed =
2016-04-18 12:07:23 +02:00
route subProxy context (fmap inject delayed)
2016-02-28 23:23:32 +01:00
where
subProxy :: Proxy subApi
subProxy = Proxy
2016-04-18 12:07:23 +02:00
inject f = f (getContextEntry context)
2016-02-28 23:23:32 +01:00
data InjectIntoContext
instance (HasServer subApi (String ': context)) =>
HasServer (InjectIntoContext :> subApi) context where
type ServerT (InjectIntoContext :> subApi) m =
ServerT subApi m
2017-10-01 18:20:09 +02:00
hoistServerWithContext _ _ nt s =
hoistServerWithContext (Proxy :: Proxy subApi) (Proxy :: Proxy (String ': context)) nt s
2017-09-08 18:21:16 +02:00
2016-02-28 23:23:32 +01:00
route Proxy context delayed =
route subProxy newContext delayed
where
subProxy :: Proxy subApi
subProxy = Proxy
newContext = ("injected" :: String) :. context
data NamedContextWithBirdface (name :: Symbol) (subContext :: [*])
instance (HasContextEntry context (NamedContext name subContext), HasServer subApi subContext) =>
HasServer (NamedContextWithBirdface name subContext :> subApi) context where
type ServerT (NamedContextWithBirdface name subContext :> subApi) m =
ServerT subApi m
2017-10-01 18:20:09 +02:00
hoistServerWithContext _ _ nt s =
hoistServerWithContext (Proxy :: Proxy subApi) (Proxy :: Proxy subContext) nt s
2017-09-08 18:21:16 +02:00
2016-02-28 23:23:32 +01:00
route Proxy context delayed =
route subProxy subContext delayed
where
subProxy :: Proxy subApi
subProxy = Proxy
subContext :: Context subContext
subContext = descendIntoNamedContext (Proxy :: Proxy name) context