Skip to content

Commit

Permalink
Merge pull request #11 from ambroslins/overlappable-toServer
Browse files Browse the repository at this point in the history
Replace most ToServer instances with a single overlappable instance
  • Loading branch information
anton-k authored Oct 19, 2023
2 parents 594fff2 + 601f2b8 commit 2e8ab60
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
8 changes: 2 additions & 6 deletions mig-extra/src/Mig/Extra/Server/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module Mig.Extra.Server.Json (

-- * re-exports
module X,
) where
)
where

import Mig.Client (FromClient (..), ToClient (..))
import Mig.Core (
Expand Down Expand Up @@ -57,11 +58,6 @@ instance (ToSchema a, FromJSON a, ToRoute b) => ToRoute (Body a -> b) where
(toRouteFun :: ((Core.Body Json a -> b) -> ServerFun (Core.MonadOf b)))
(\(Core.Body a) -> f (Body a))

instance (ToSchema a, FromJSON a, ToRoute b) => ToServer (Body a -> b) where
toServer f =
(toServer :: ((Core.Body Json a -> b) -> Server (Core.MonadOf b)))
(\(Core.Body a) -> f (Body a))

instance (FromJSON a, ToSchema a, ToPlugin b) => ToPlugin (Body a -> b) where
toPluginInfo = toPluginInfo @(Core.Body Json a -> b)

Expand Down
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 (ExceptT, runExceptT)
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 2e8ab60

Please sign in to comment.