Skip to content

Commit

Permalink
Remove overlapping instance for HasServer (Verb … (Headers x a))
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaël Deest committed Feb 22, 2022
1 parent 7ef9730 commit f9994cf
Showing 1 changed file with 30 additions and 14 deletions.
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

0 comments on commit f9994cf

Please sign in to comment.