@@ -1,217 +1,138 @@
1−-- | Description: ways to build a reified api from a servant description.
2−--
3−-- arguably this could be more general and be abstracted away from even relying on servant
4−-- but that's future work.
51
62{-# LANGUAGE AllowAmbiguousTypes #-}
73{-# LANGUAGE DataKinds #-}
84{-# LANGUAGE DerivingStrategies #-}
9−{-# LANGUAGE FlexibleContexts #-}
105{-# LANGUAGE FlexibleInstances #-}
11−{-# LANGUAGE GADTs #-}
126{-# LANGUAGE GeneralizedNewtypeDeriving #-}
137{-# LANGUAGE LambdaCase #-}
148{-# LANGUAGE PolyKinds #-}
159{-# LANGUAGE ScopedTypeVariables #-}
10+{-# LANGUAGE StandaloneDeriving #-}
11+{-# LANGUAGE TupleSections #-}
1612{-# LANGUAGE TypeApplications #-}
1713{-# LANGUAGE TypeFamilies #-}
1814{-# LANGUAGE TypeOperators #-}
1915{-# LANGUAGE UndecidableInstances #-}
20−{-# LANGUAGE CPP #-}
16+
2117module Roboservant.Types.ReifiedApi where
2218
23−import Data.Dynamic (Dynamic)
24−import Control.Exception(Exception)
25−import Data.List.NonEmpty (NonEmpty)
26−import Data.Typeable (Typeable)
27−import GHC.Generics ((:*:)(..))
28−import Roboservant.Types.Internal
29−import Roboservant.Types.Breakdown
30−import Roboservant.Types.BuildFrom
31−import Data.Kind(Type)
19+
20+import Control.Monad.Except (runExceptT)
21+import Data.Dynamic (Dynamic, toDyn)
22+import Data.Function ((&))
23+import Data.Typeable (TypeRep, Typeable, typeRep)
24+import GHC.TypeLits (Symbol)
3225import Servant
3326import Servant.API.Modifiers(FoldRequired,FoldLenient)
34−import GHC.TypeLits (Symbol)
35−import qualified Data.Text as T
36−import qualified Data.Vinyl as V
37−import qualified Data.Vinyl.Curry as V
38−import qualified Type.Reflection as R
27+import Roboservant.Types.FlattenServer
28+
3929
4030newtype ApiOffset = ApiOffset Int
41− deriving (Eq, Show, Ord)
31+ deriving (Eq, Show)
4232 deriving newtype (Enum, Num)
4333
44−type TypedF = (:*:) R.TypeRep
45−
46−newtype Argument a = Argument
47− { getArgument :: Stash -> Maybe (StashValue a)
48− }
49−
50−data ReifiedEndpoint = forall as. (V.RecordToList as, V.RMap as) => ReifiedEndpoint
51− { reArguments :: V.Rec (TypedF Argument) as
52− , reEndpointFunc :: V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic,Int))))
53− }
54−
55−instance Show ReifiedEndpoint where
56− show _ = "lol"
34+type ReifiedEndpoint = ([TypeRep], TypeRep, Dynamic)
5735
58−class ( V.RecordToList (EndpointArgs endpoint)
59− , V.RMap (EndpointArgs endpoint)
60− ) => ToReifiedEndpoint (endpoint :: Type) where
61− type EndpointArgs endpoint :: [Type]
62− type EndpointRes endpoint :: Type
36+type ReifiedApi = [(ApiOffset, ReifiedEndpoint)]
6337
64− reifiedEndpointArguments :: V.Rec (TypedF Argument) (EndpointArgs endpoint)
6538
39+class ToReifiedApi (endpoints :: [*]) where
40+ toReifiedApi :: Bundled endpoints -> Proxy endpoints -> ReifiedApi
6641
67−tagType :: Typeable a => f a -> TypedF f a
68−tagType = (R.typeRep :*:)
69−
70−data InteractionError = InteractionError
71− { errorMessage :: T.Text
72− , fatalError :: Bool
73− }
74− deriving Show
75−instance Exception InteractionError
76−
42+class ToReifiedEndpoint (endpoint :: *) where
43+ toReifiedEndpoint :: Dynamic -> Proxy endpoint -> ReifiedEndpoint
7744
45+instance ToReifiedApi '[] where
46+ toReifiedApi NoEndpoints _ = []
7847
7948instance
80− (Typeable responseType, Breakdown responseType) =>
81− ToReifiedEndpoint (Verb method statusCode contentTypes responseType)
82− where
83− type EndpointArgs (Verb method statusCode contentTypes responseType) = '[]
84− type EndpointRes (Verb method statusCode contentTypes responseType) = responseType
85− reifiedEndpointArguments = V.RNil
86−
87−instance ToReifiedEndpoint (NoContentVerb method)
49+ (Typeable (Normal (ServerT endpoint Handler)), NormalizeFunction (ServerT endpoint Handler), ToReifiedEndpoint endpoint, ToReifiedApi endpoints, Typeable (ServerT endpoint Handler)) =>
50+ ToReifiedApi (endpoint : endpoints)
8851 where
89− type EndpointArgs (NoContentVerb method) = '[]
90− type EndpointRes (NoContentVerb method) = NoContent
91− reifiedEndpointArguments = V.RNil
52+ toReifiedApi (endpoint `AnEndpoint` endpoints) _ =
53+ (0,) (toReifiedEndpoint (toDyn (normalize endpoint)) (Proxy @endpoint))
54+ : map
55+ (\(n, x) -> (n + 1, x))
56+ (toReifiedApi endpoints (Proxy @endpoints))
57+
58+class NormalizeFunction m where
59+ type Normal m
60+ normalize :: m -> Normal m
61+
62+instance NormalizeFunction x => NormalizeFunction (r -> x) where
63+ type Normal (r -> x) = r -> Normal x
64+ normalize = fmap normalize
65+
66+instance Typeable x => NormalizeFunction (Handler x) where
67+ type Normal (Handler x) = IO (Either ServerError (TypeRep, Dynamic))
68+ normalize handler = (runExceptT . runHandler') handler >>= \case
69+ Left serverError -> pure (Left serverError)
70+ Right x -> pure (Right (typeRep (Proxy @x), toDyn x))
9271
9372instance
94− (ToReifiedEndpoint endpoint) =>
95− ToReifiedEndpoint ((x :: Symbol) :> endpoint)
73+ Typeable responseType =>
74+ ToReifiedEndpoint (Verb method statusCode contentTypes responseType)
9675 where
97− type EndpointArgs ((x :: Symbol) :> endpoint) = EndpointArgs endpoint
98− type EndpointRes ((x :: Symbol) :> endpoint) = EndpointRes endpoint
99− reifiedEndpointArguments = reifiedEndpointArguments @endpoint
76+ toReifiedEndpoint endpoint _ =
77+ ([], typeRep (Proxy @responseType), endpoint)
10078
10179instance
10280 (ToReifiedEndpoint endpoint) =>
103− ToReifiedEndpoint (RemoteHost :> endpoint)
81+ ToReifiedEndpoint ((x :: Symbol) :> endpoint)
10482 where
105− type EndpointArgs (RemoteHost :> endpoint) = EndpointArgs endpoint
106− type EndpointRes (RemoteHost :> endpoint) = EndpointRes endpoint
107− reifiedEndpointArguments = reifiedEndpointArguments @endpoint
108−
109−
83+ toReifiedEndpoint endpoint _ =
84+ toReifiedEndpoint endpoint (Proxy @endpoint)
11085
11186instance
11287 (ToReifiedEndpoint endpoint) =>
11388 ToReifiedEndpoint (Description s :> endpoint)
11489 where
115− type EndpointArgs (Description s :> endpoint) = EndpointArgs endpoint
116− type EndpointRes (Description s :> endpoint) = EndpointRes endpoint
117− reifiedEndpointArguments = reifiedEndpointArguments @endpoint
90+ toReifiedEndpoint endpoint _ =
91+ toReifiedEndpoint endpoint (Proxy @endpoint)
11892
11993instance
12094 (ToReifiedEndpoint endpoint) =>
12195 ToReifiedEndpoint (Summary s :> endpoint)
12296 where
123− type EndpointArgs (Summary s :> endpoint) = EndpointArgs endpoint
124− type EndpointRes (Summary s :> endpoint) = EndpointRes endpoint
125− reifiedEndpointArguments = reifiedEndpointArguments @endpoint
97+ toReifiedEndpoint endpoint _ =
98+ toReifiedEndpoint endpoint (Proxy @endpoint)
12699
127100instance
128− (Typeable requestType
129− ,BuildFrom requestType
130− ,ToReifiedEndpoint endpoint) =>
101+ (Typeable requestType, ToReifiedEndpoint endpoint) =>
131102 ToReifiedEndpoint (QueryFlag name :> endpoint)
132103 where
133− type EndpointArgs (QueryFlag name :> endpoint) = Bool ': EndpointArgs endpoint
134− type EndpointRes (QueryFlag name :> endpoint) = EndpointRes endpoint
135− reifiedEndpointArguments = tagType (Argument (buildFrom @Bool)) V.:& reifiedEndpointArguments @endpoint
136−
137−type IfLenient s mods t = If (FoldLenient mods) (Either s t) t
138−type IfRequired mods t = If (FoldRequired mods) t (Maybe t)
139−type IfRequiredLenient s mods t = IfRequired mods (IfLenient s mods t)
104+ toReifiedEndpoint endpoint _ =
105+ toReifiedEndpoint endpoint (Proxy @endpoint)
106+ & \(args, result, typeRepMap) -> (typeRep (Proxy @Bool) : args, result, typeRepMap)
140107
141108instance
142− ( BuildFrom (IfRequiredLenient T.Text mods paramType)
143− , ToReifiedEndpoint endpoint
144− ) =>
109+ (Typeable (If (FoldRequired mods) paramType (Maybe paramType)), ToReifiedEndpoint endpoint, SBoolI (FoldRequired mods)) =>
145110 ToReifiedEndpoint (QueryParam' mods name paramType :> endpoint)
146111 where
147− type EndpointArgs (QueryParam' mods name paramType :> endpoint) = IfRequiredLenient T.Text mods paramType ': EndpointArgs endpoint
148− type EndpointRes (QueryParam' mods name paramType :> endpoint) = EndpointRes endpoint
149− reifiedEndpointArguments =
150− tagType (Argument (buildFrom @(IfRequiredLenient T.Text mods paramType)))
151− V.:& reifiedEndpointArguments @endpoint
152−
112+ toReifiedEndpoint endpoint _ =
113+ toReifiedEndpoint endpoint (Proxy @endpoint)
114+ & \(args, result, typeRepMap) -> (typeRep (Proxy @(If (FoldRequired mods) paramType (Maybe paramType))) : args, result, typeRepMap)
153115
154116instance
155− ( BuildFrom paramType
156− , ToReifiedEndpoint endpoint
157− , Show paramType
158− , Eq paramType
159− ) =>
160− ToReifiedEndpoint (QueryParams name paramType :> endpoint)
161− where
162− type EndpointArgs (QueryParams name paramType :> endpoint) = [paramType] ': EndpointArgs endpoint
163− type EndpointRes (QueryParams name paramType :> endpoint) = EndpointRes endpoint
164− reifiedEndpointArguments =
165− tagType (Argument (buildFrom @[paramType]))
166− V.:& reifiedEndpointArguments @endpoint
167−
168−
169−
170−
171−instance
172− ( BuildFrom (IfRequiredLenient T.Text mods headerType)
173− , ToReifiedEndpoint endpoint
174− ) =>
117+ (Typeable (If (FoldRequired mods) headerType (Maybe headerType)), ToReifiedEndpoint endpoint, SBoolI (FoldRequired mods)) =>
175118 ToReifiedEndpoint (Header' mods headerName headerType :> endpoint)
176119 where
177− type EndpointArgs (Header' mods headerName headerType :> endpoint) = IfRequiredLenient T.Text mods headerType ': EndpointArgs endpoint
178− type EndpointRes (Header' mods headerName headerType :> endpoint) = EndpointRes endpoint
179− reifiedEndpointArguments =
180− tagType (Argument (buildFrom @(IfRequiredLenient T.Text mods headerType)))
181− V.:& reifiedEndpointArguments @endpoint
120+ toReifiedEndpoint endpoint _ =
121+ toReifiedEndpoint endpoint (Proxy @endpoint)
122+ & \(args, result, typeRepMap) -> (typeRep (Proxy @(If (FoldRequired mods) headerType (Maybe headerType))) : args, result, typeRepMap)
182123
183−#if MIN_VERSION_servant(0,17,0)
184124instance
185− ( BuildFrom (IfLenient String mods captureType)
186− , ToReifiedEndpoint endpoint) =>
125+ (Typeable captureType, ToReifiedEndpoint endpoint) =>
187126 ToReifiedEndpoint (Capture' mods name captureType :> endpoint)
188127 where
189− type EndpointArgs (Capture' mods name captureType :> endpoint) = IfLenient String mods captureType ': EndpointArgs endpoint
190− type EndpointRes (Capture' mods name captureType :> endpoint) = EndpointRes endpoint
191− reifiedEndpointArguments =
192− tagType (Argument (buildFrom @(IfLenient String mods captureType)))
193− V.:& reifiedEndpointArguments @endpoint
194−#else
195−instance
196− ( BuildFrom captureType
197− , ToReifiedEndpoint endpoint) =>
198− ToReifiedEndpoint (Capture' mods name captureType :> endpoint)
199− where
200− type EndpointArgs (Capture' mods name captureType :> endpoint) = captureType ': EndpointArgs endpoint
201− type EndpointRes (Capture' mods name captureType :> endpoint) = EndpointRes endpoint
202− reifiedEndpointArguments =
203− tagType (Argument (buildFrom @(captureType)))
204− V.:& reifiedEndpointArguments @endpoint
205−
206−#endif
128+ toReifiedEndpoint endpoint _ =
129+ toReifiedEndpoint endpoint (Proxy @endpoint)
130+ & \(args, result, typeRepMap) -> (typeRep (Proxy @captureType) : args, result, typeRepMap)
207131
208132instance
209− ( BuildFrom (IfLenient String mods requestType)
210− , ToReifiedEndpoint endpoint) =>
133+ (Typeable (If (FoldLenient mods) (Either String requestType) requestType), ToReifiedEndpoint endpoint, SBoolI (FoldLenient mods)) =>
211134 ToReifiedEndpoint (ReqBody' mods contentTypes requestType :> endpoint)
212135 where
213− type EndpointArgs (ReqBody' mods contentTypes requestType :> endpoint) = IfLenient String mods requestType ': EndpointArgs endpoint
214− type EndpointRes (ReqBody' mods contentTypes requestType :> endpoint) = EndpointRes endpoint
215− reifiedEndpointArguments =
216− tagType (Argument (buildFrom @(IfLenient String mods requestType)))
217− V.:& reifiedEndpointArguments @endpoint
136+ toReifiedEndpoint endpoint _ =
137+ toReifiedEndpoint endpoint (Proxy @endpoint)
138+ & \(args, result, typeRepMap) -> (typeRep (Proxy @(If (FoldLenient mods) (Either String requestType) requestType)) : args, result, typeRepMap)