Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove overlapping instance HasServer (Verb … (Headers x a)) … #1543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions servant-server/src/Servant/Server/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,42 @@ noContentRouter method status action = leafRouter route'
env request respond $ \ _output ->
Route $ responseLBS status [] ""

instance {-# OVERLAPPABLE #-}
( AllCTRender ctypes a, ReflectMethod method, KnownNat status
) => HasServer (Verb method status ctypes a) context where
newtype Naked a = Naked a

type ServerT (Verb method status ctypes a) m = m a
hoistServerWithContext _ _ nt s = nt s
type family Wrap a where
Wrap (Headers x a) = Headers x a
Wrap a = Naked a

route Proxy _ = methodRouter ([],) method (Proxy :: Proxy ctypes) status
where method = reflectMethod (Proxy :: Proxy method)
status = statusFromNat (Proxy :: Proxy status)
class ExtractHeadersResponse orig wrapped where
type HandlerResponse orig wrapped :: *
type ExtractedValue orig wrapped :: *

instance {-# OVERLAPPING #-}
( AllCTRender ctypes a, ReflectMethod method, KnownNat status
, GetHeaders (Headers h a)
) => HasServer (Verb method status ctypes (Headers h a)) context where
extractHeadersResponse :: HandlerResponse orig wrapped -> (([(HeaderName, B.ByteString)]), ExtractedValue orig wrapped)

type ServerT (Verb method status ctypes (Headers h a)) m = m (Headers h a)
instance ExtractHeadersResponse a (Naked a) where
type HandlerResponse a (Naked a) = a
type ExtractedValue a (Naked a) = a

extractHeadersResponse :: a -> (([(HeaderName, B.ByteString)]), a)
extractHeadersResponse x = ([], x)

instance GetHeaders (Headers x a) => ExtractHeadersResponse (Headers x a) (Headers x a) where
type HandlerResponse (Headers x a) (Headers x a) = Headers x a
type ExtractedValue (Headers x a) (Headers x a) = a

extractHeadersResponse :: Headers x a -> ([(HeaderName, B.ByteString)], a)
extractHeadersResponse x = (getHeaders x, getResponse x)

instance ( AllCTRender ctypes (ExtractedValue a (Wrap a))
, ReflectMethod method, KnownNat status
, ExtractHeadersResponse a (Wrap a)
, a ~ HandlerResponse a (Wrap a)
) => HasServer (Verb method status ctypes a) context where

type ServerT (Verb method status ctypes a) m = m a
hoistServerWithContext _ _ nt s = nt s

route Proxy _ = methodRouter (\x -> (getHeaders x, getResponse x)) method (Proxy :: Proxy ctypes) status
route Proxy _ = methodRouter (extractHeadersResponse @a @(Wrap a)) method (Proxy :: Proxy ctypes) status
where method = reflectMethod (Proxy :: Proxy method)
status = statusFromNat (Proxy :: Proxy status)

Expand Down