Skip to content

Commit

Permalink
Replace most ToServer instances with a single overlappable instance
Browse files Browse the repository at this point in the history
We can construct a server from every route. However this requires
`UndecidableInstances` and a `OVERLAPPABLE` pragma.
  • Loading branch information
ambroslins committed Oct 17, 2023
1 parent 14e7dc3 commit efbb33c
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions mig/src/Mig/Core/Class/Server.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE UndecidableInstances #-}

-- | To server class
module Mig.Core.Class.Server (
(/.),
Expand All @@ -11,18 +13,13 @@ module Mig.Core.Class.Server (
import Control.Monad.Except
import Control.Monad.Reader
import Data.Kind
import Data.OpenApi (ToParamSchema, ToSchema)
import Data.Text (Text)
import GHC.TypeLits
import Mig.Core.Api qualified as Api
import Mig.Core.Class.MediaType (FromReqBody (..))
import Mig.Core.Class.Monad
import Mig.Core.Class.Response (IsResp)
import Mig.Core.Class.Route
import Mig.Core.Server (Server (..), mapServerFun)
import Mig.Core.ServerFun (ServerFun)
import Mig.Core.Types
import Web.HttpApiData

infixr 4 /.

Expand Down Expand Up @@ -66,28 +63,8 @@ instance ToServer (Server m) where
instance (ToServer a) => ToServer [a] where
toServer = foldMap toServer

-- outputs
instance (MonadIO m, IsResp a, IsMethod method) => ToServer (Send method m a) where
toServer a = Server $ Api.HandleRoute (toRoute a)

-- inputs

instance (ToSchema a, FromReqBody media a, ToRoute b) => ToServer (Body media a -> b) where
toServer a = Server $ Api.HandleRoute (toRoute a)

instance (FromHttpApiData a, ToParamSchema a, ToRoute b, KnownSymbol sym) => ToServer (Query sym a -> b) where
toServer a = Server $ Api.HandleRoute (toRoute a)

instance (FromHttpApiData a, ToParamSchema a, ToRoute b, KnownSymbol sym) => ToServer (Optional sym a -> b) where
toServer a = Server $ Api.HandleRoute (toRoute a)

instance (FromHttpApiData a, ToParamSchema a, ToRoute b, KnownSymbol sym) => ToServer (Capture sym a -> b) where
toServer a = Server $ Api.HandleRoute (toRoute a)

instance (FromHttpApiData a, ToParamSchema a, ToRoute b, KnownSymbol sym) => ToServer (Header sym a -> b) where
toServer a = Server $ Api.HandleRoute (toRoute a)

instance (ToRoute b) => ToServer (PathInfo -> b) where
-- routes
instance {-# OVERLAPPABLE #-} (ToRoute a) => ToServer a where
toServer a = Server $ Api.HandleRoute (toRoute a)

-------------------------------------------------------------------------------------
Expand Down

0 comments on commit efbb33c

Please sign in to comment.