git.iko.soy/typeable/servant
on master help sign in
Added lang parameter.
commit69f09f26222014124d0c40a31faf1cfab317fa64 authorMaksymilian Owsianny <Maksymilian.Owsianny@gmail.com> dateSun 29 Nov 2015, 04:53 (11 years ago) parent0b37222
3 files changed+94−85
servant-foreign/src/Servant/Foreign/Internal.hs+73−69 servant-js/src/Servant/JS.hs+11−7 servant-js/test/Servant/JSSpec/CustomHeaders.hs+10−9
servant-foreign/src/Servant/Foreign/Internal.hs+73−69
@@ -122,144 +122,148 @@ type family Elem (a :: *) (ls::[*]) :: Constraint where
122122 Elem a (a ': list) = ()
123123 Elem a (b ': list) = Elem a list
124124
125−class HasForeignType a where
126− typeFor :: Proxy a -> ForeignType
125+class HasForeignType lang a where
126+ typeFor :: Proxy lang -> Proxy a -> ForeignType
127127
128−class HasForeign (layout :: *) where
128+class HasForeign lang (layout :: *) where
129129 type Foreign layout :: *
130− foreignFor :: Proxy layout -> Req -> Foreign layout
130+ foreignFor :: Proxy lang -> Proxy layout -> Req -> Foreign layout
131131
132−instance (HasForeign a, HasForeign b)
133− => HasForeign (a :<|> b) where
132+instance (HasForeign lang a, HasForeign lang b)
133+ => HasForeign lang (a :<|> b) where
134134 type Foreign (a :<|> b) = Foreign a :<|> Foreign b
135135
136− foreignFor Proxy req =
137− foreignFor (Proxy :: Proxy a) req
138− :<|> foreignFor (Proxy :: Proxy b) req
136+ foreignFor lang Proxy req =
137+ foreignFor lang (Proxy :: Proxy a) req
138+ :<|> foreignFor lang (Proxy :: Proxy b) req
139139
140−instance (KnownSymbol sym, HasForeignType a, HasForeign sublayout)
141− => HasForeign (Capture sym a :> sublayout) where
140+instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
141+ => HasForeign lang (Capture sym a :> sublayout) where
142142 type Foreign (Capture sym a :> sublayout) = Foreign sublayout
143143
144− foreignFor Proxy req =
145− foreignFor (Proxy :: Proxy sublayout) $
144+ foreignFor lang Proxy req =
145+ foreignFor lang (Proxy :: Proxy sublayout) $
146146 req & reqUrl.path <>~ [Segment (Cap arg)]
147147 & funcName %~ (++ ["by", str])
148148
149149 where
150150 str = pack . symbolVal $ (Proxy :: Proxy sym)
151− arg = (str, typeFor (Proxy :: Proxy a))
151+ arg = (str, typeFor lang (Proxy :: Proxy a))
152152
153−instance (Elem JSON list, HasForeignType a) => HasForeign (Delete list a) where
153+instance (Elem JSON list, HasForeignType lang a)
154+ => HasForeign lang (Delete list a) where
154155 type Foreign (Delete list a) = Req
155156
156− foreignFor Proxy req =
157+ foreignFor lang Proxy req =
157158 req & funcName %~ ("delete" :)
158159 & reqMethod .~ "DELETE"
159160 & reqReturnType .~ retType
160161 where
161− retType = typeFor (Proxy :: Proxy a)
162+ retType = typeFor lang (Proxy :: Proxy a)
162163
163−instance (Elem JSON list, HasForeignType a) => HasForeign (Get list a) where
164+instance (Elem JSON list, HasForeignType lang a)
165+ => HasForeign lang (Get list a) where
164166 type Foreign (Get list a) = Req
165167
166− foreignFor Proxy req =
168+ foreignFor lang Proxy req =
167169 req & funcName %~ ("get" :)
168170 & reqMethod .~ "GET"
169171 & reqReturnType .~ retType
170172 where
171− retType = typeFor (Proxy :: Proxy a)
173+ retType = typeFor lang (Proxy :: Proxy a)
172174
173−instance (KnownSymbol sym, HasForeignType a, HasForeign sublayout)
174− => HasForeign (Header sym a :> sublayout) where
175+instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
176+ => HasForeign lang (Header sym a :> sublayout) where
175177 type Foreign (Header sym a :> sublayout) = Foreign sublayout
176178
177− foreignFor Proxy req =
178− foreignFor subP $ req
179+ foreignFor lang Proxy req =
180+ foreignFor lang subP $ req
179181 & reqHeaders <>~ [HeaderArg arg]
180182
181183 where
182184 hname = pack . symbolVal $ (Proxy :: Proxy sym)
183− arg = (hname, typeFor (Proxy :: Proxy a))
185+ arg = (hname, typeFor lang (Proxy :: Proxy a))
184186 subP = Proxy :: Proxy sublayout
185187
186−instance (Elem JSON list, HasForeignType a) => HasForeign (Post list a) where
188+instance (Elem JSON list, HasForeignType lang a)
189+ => HasForeign lang (Post list a) where
187190 type Foreign (Post list a) = Req
188191
189− foreignFor Proxy req =
192+ foreignFor lang Proxy req =
190193 req & funcName %~ ("post" :)
191194 & reqMethod .~ "POST"
192195 & reqReturnType .~ retType
193196 where
194− retType = typeFor (Proxy :: Proxy a)
197+ retType = typeFor lang (Proxy :: Proxy a)
195198
196−instance (Elem JSON list, HasForeignType a) => HasForeign (Put list a) where
199+instance (Elem JSON list, HasForeignType lang a)
200+ => HasForeign lang (Put list a) where
197201 type Foreign (Put list a) = Req
198202
199− foreignFor Proxy req =
203+ foreignFor lang Proxy req =
200204 req & funcName %~ ("put" :)
201205 & reqMethod .~ "PUT"
202206 & reqReturnType .~ retType
203207 where
204− retType = typeFor (Proxy :: Proxy a)
208+ retType = typeFor lang (Proxy :: Proxy a)
205209
206−instance (KnownSymbol sym, HasForeignType a, HasForeign sublayout)
207− => HasForeign (QueryParam sym a :> sublayout) where
210+instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
211+ => HasForeign lang (QueryParam sym a :> sublayout) where
208212 type Foreign (QueryParam sym a :> sublayout) = Foreign sublayout
209213
210− foreignFor Proxy req =
211− foreignFor (Proxy :: Proxy sublayout) $
214+ foreignFor lang Proxy req =
215+ foreignFor lang (Proxy :: Proxy sublayout) $
212216 req & reqUrl.queryStr <>~ [QueryArg arg Normal]
213217
214218 where
215219 str = pack . symbolVal $ (Proxy :: Proxy sym)
216− arg = (str, typeFor (Proxy :: Proxy a))
220+ arg = (str, typeFor lang (Proxy :: Proxy a))
217221
218−instance (KnownSymbol sym, HasForeignType a, HasForeign sublayout)
219− => HasForeign (QueryParams sym a :> sublayout) where
222+instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
223+ => HasForeign lang (QueryParams sym a :> sublayout) where
220224 type Foreign (QueryParams sym a :> sublayout) = Foreign sublayout
221225
222− foreignFor Proxy req =
223− foreignFor (Proxy :: Proxy sublayout) $
226+ foreignFor lang Proxy req =
227+ foreignFor lang (Proxy :: Proxy sublayout) $
224228 req & reqUrl.queryStr <>~ [QueryArg arg List]
225229
226230 where
227231 str = pack . symbolVal $ (Proxy :: Proxy sym)
228− arg = (str, typeFor (Proxy :: Proxy a))
232+ arg = (str, typeFor lang (Proxy :: Proxy a))
229233
230−instance (KnownSymbol sym, HasForeignType a, a ~ Bool, HasForeign sublayout)
231− => HasForeign (QueryFlag sym :> sublayout) where
234+instance (KnownSymbol sym, HasForeignType lang a, a ~ Bool, HasForeign lang sublayout)
235+ => HasForeign lang (QueryFlag sym :> sublayout) where
232236 type Foreign (QueryFlag sym :> sublayout) = Foreign sublayout
233237
234− foreignFor Proxy req =
235− foreignFor (Proxy :: Proxy sublayout) $
238+ foreignFor lang Proxy req =
239+ foreignFor lang (Proxy :: Proxy sublayout) $
236240 req & reqUrl.queryStr <>~ [QueryArg arg Flag]
237241
238242 where
239243 str = pack . symbolVal $ (Proxy :: Proxy sym)
240− arg = (str, typeFor (Proxy :: Proxy a))
244+ arg = (str, typeFor lang (Proxy :: Proxy a))
241245
242−instance HasForeign Raw where
246+instance HasForeign lang Raw where
243247 type Foreign Raw = Method -> Req
244248
245− foreignFor Proxy req method =
249+ foreignFor _ Proxy req method =
246250 req & funcName %~ ((toLower method) :)
247251 & reqMethod .~ method
248252
249−instance (Elem JSON list, HasForeignType a, HasForeign sublayout)
250− => HasForeign (ReqBody list a :> sublayout) where
253+instance (Elem JSON list, HasForeignType lang a, HasForeign lang sublayout)
254+ => HasForeign lang (ReqBody list a :> sublayout) where
251255 type Foreign (ReqBody list a :> sublayout) = Foreign sublayout
252256
253− foreignFor Proxy req =
254− foreignFor (Proxy :: Proxy sublayout) $
255− req & reqBody .~ (Just $ typeFor (Proxy :: Proxy a))
257+ foreignFor lang Proxy req =
258+ foreignFor lang (Proxy :: Proxy sublayout) $
259+ req & reqBody .~ (Just $ typeFor lang (Proxy :: Proxy a))
256260
257−instance (KnownSymbol path, HasForeign sublayout)
258− => HasForeign (path :> sublayout) where
261+instance (KnownSymbol path, HasForeign lang sublayout)
262+ => HasForeign lang (path :> sublayout) where
259263 type Foreign (path :> sublayout) = Foreign sublayout
260264
261− foreignFor Proxy req =
262− foreignFor (Proxy :: Proxy sublayout) $
265+ foreignFor lang Proxy req =
266+ foreignFor lang (Proxy :: Proxy sublayout) $
263267 req & reqUrl.path <>~ [Segment (Static str)]
264268 & funcName %~ (++ [str])
265269
@@ -267,26 +271,26 @@ instance (KnownSymbol path, HasForeign sublayout)
267271 str = Data.Text.map (\c -> if c == '.' then '_' else c)
268272 . pack . symbolVal $ (Proxy :: Proxy path)
269273
270−instance HasForeign sublayout => HasForeign (RemoteHost :> sublayout) where
274+instance HasForeign lang sublayout => HasForeign lang (RemoteHost :> sublayout) where
271275 type Foreign (RemoteHost :> sublayout) = Foreign sublayout
272276
273− foreignFor Proxy req =
274− foreignFor (Proxy :: Proxy sublayout) req
277+ foreignFor lang Proxy req =
278+ foreignFor lang (Proxy :: Proxy sublayout) req
275279
276−instance HasForeign sublayout => HasForeign (IsSecure :> sublayout) where
280+instance HasForeign lang sublayout => HasForeign lang (IsSecure :> sublayout) where
277281 type Foreign (IsSecure :> sublayout) = Foreign sublayout
278282
279− foreignFor Proxy req =
280− foreignFor (Proxy :: Proxy sublayout) req
283+ foreignFor lang Proxy req =
284+ foreignFor lang (Proxy :: Proxy sublayout) req
281285
282−instance HasForeign sublayout => HasForeign (Vault :> sublayout) where
286+instance HasForeign lang sublayout => HasForeign lang (Vault :> sublayout) where
283287 type Foreign (Vault :> sublayout) = Foreign sublayout
284288
285− foreignFor Proxy req =
286− foreignFor (Proxy :: Proxy sublayout) req
289+ foreignFor lang Proxy req =
290+ foreignFor lang (Proxy :: Proxy sublayout) req
287291
288−instance HasForeign sublayout => HasForeign (HttpVersion :> sublayout) where
292+instance HasForeign lang sublayout => HasForeign lang (HttpVersion :> sublayout) where
289293 type Foreign (HttpVersion :> sublayout) = Foreign sublayout
290294
291− foreignFor Proxy req =
292− foreignFor (Proxy :: Proxy sublayout) req
295+ foreignFor lang Proxy req =
296+ foreignFor lang (Proxy :: Proxy sublayout) req
servant-js/src/Servant/JS.hs+11−7
@@ -2,6 +2,7 @@
22{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE FlexibleInstances #-}
44{-# LANGUAGE TypeOperators #-}
5+{-# LANGUAGE MultiParamTypeClasses #-}
56-----------------------------------------------------------------------------
67-- |
78-- Module : Servant.JS
@@ -122,16 +123,19 @@ import Servant.JS.Internal
122123import Servant.JS.JQuery
123124import Servant.JS.Vanilla
124125
126+-- Dummy type specifying target language
127+data LangJS
128+
125129-- | Generate the data necessary to generate javascript code
126130-- for all the endpoints of an API, as ':<|>'-separated values
127131-- of type 'AjaxReq'.
128−javascript :: HasForeign layout => Proxy layout -> Foreign layout
129−javascript p = foreignFor p defReq
132+javascript :: HasForeign LangJS layout => Proxy layout -> Foreign layout
133+javascript p = foreignFor (Proxy :: Proxy LangJS) p defReq
130134
131135-- | Directly generate all the javascript functions for your API
132136-- from a 'Proxy' for your API type. You can then write it to
133137-- a file or integrate it in a page, for example.
134−jsForAPI :: (HasForeign api, GenerateList (Foreign api))
138+jsForAPI :: (HasForeign LangJS api, GenerateList (Foreign api))
135139 => Proxy api -- ^ proxy for your API type
136140 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
137141 -> Text -- ^ a text that you can embed in your pages or write to a file
@@ -140,7 +144,7 @@ jsForAPI p gen = gen (listFromAPI p)
140144-- | Directly generate all the javascript functions for your API
141145-- from a 'Proxy' for your API type using the given generator
142146-- and write the resulting code to a file at the given path.
143−writeJSForAPI :: (HasForeign api, GenerateList (Foreign api))
147+writeJSForAPI :: (HasForeign LangJS api, GenerateList (Foreign api))
144148 => Proxy api -- ^ proxy for your API type
145149 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
146150 -> FilePath -- ^ path to the file you want to write the resulting javascript code into
@@ -148,8 +152,8 @@ writeJSForAPI :: (HasForeign api, GenerateList (Foreign api))
148152writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
149153
150154-- A catch all instance since JavaScript has no types.
151−instance HasForeignType a where
152− typeFor _ = empty
155+instance HasForeignType LangJS a where
156+ typeFor _ _ = empty
153157
154158-- | Utility class used by 'jsForAPI' which computes
155159-- the data needed to generate a function for each endpoint
@@ -165,6 +169,6 @@ instance (GenerateList start, GenerateList rest) => GenerateList (start :<|> res
165169
166170-- | Generate the necessary data for JS codegen as a list, each 'AjaxReq'
167171-- describing one endpoint from your API type.
168−listFromAPI :: (HasForeign api, GenerateList (Foreign api)) => Proxy api -> [AjaxReq]
172+listFromAPI :: (HasForeign LangJS api, GenerateList (Foreign api)) => Proxy api -> [AjaxReq]
169173listFromAPI p = generateList (javascript p)
170174
servant-js/test/Servant/JSSpec/CustomHeaders.hs+10−9
@@ -6,6 +6,7 @@
66{-# LANGUAGE TypeFamilies #-}
77{-# LANGUAGE TypeOperators #-}
88{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE MultiParamTypeClasses #-}
910
1011module Servant.JSSpec.CustomHeaders where
1112
@@ -21,11 +22,11 @@ import Servant.JS.Internal
2122-- using -- Basic, Digest, whatever.
2223data Authorization (sym :: Symbol) a
2324
24−instance (KnownSymbol sym, HasForeign sublayout)
25− => HasForeign (Authorization sym a :> sublayout) where
25+instance (KnownSymbol sym, HasForeign lang sublayout)
26+ => HasForeign lang (Authorization sym a :> sublayout) where
2627 type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
2728
28− foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
29+ foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
2930 req & reqHeaders <>~ [ ReplaceHeaderArg ("Authorization", "") $
3031 tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
3132 where
@@ -34,11 +35,11 @@ instance (KnownSymbol sym, HasForeign sublayout)
3435-- | This is a combinator that fetches an X-MyLovelyHorse header.
3536data MyLovelyHorse a
3637
37−instance (HasForeign sublayout)
38− => HasForeign (MyLovelyHorse a :> sublayout) where
38+instance (HasForeign lang sublayout)
39+ => HasForeign lang (MyLovelyHorse a :> sublayout) where
3940 type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
4041
41− foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
42+ foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
4243 req & reqHeaders <>~ [ ReplaceHeaderArg ("X-MyLovelyHorse", "") tpl ]
4344 where
4445 tpl = "I am good friends with {X-MyLovelyHorse}"
@@ -46,11 +47,11 @@ instance (HasForeign sublayout)
4647-- | This is a combinator that fetches an X-WhatsForDinner header.
4748data WhatsForDinner a
4849
49−instance (HasForeign sublayout)
50− => HasForeign (WhatsForDinner a :> sublayout) where
50+instance (HasForeign lang sublayout)
51+ => HasForeign lang (WhatsForDinner a :> sublayout) where
5152 type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
5253
53− foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
54+ foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) $
5455 req & reqHeaders <>~ [ ReplaceHeaderArg ("X-WhatsForDinner", "") tpl ]
5556 where
5657 tpl = "I would like {X-WhatsForDinner} with a cherry on top."