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

Replace most ToServer instances with a single overlappable instance #11

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
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
Loading