on master help sign in
Code changes to support Jkarni/content types.

These are just straight forward changes to compile against servant.

commit5578488538c8213d2015cc83eaf4d37217c6e3d2 authorTimo von Holtz <tvh@anchor.com.au> dateThu 19 Feb 2015, 11:36 (12 years ago) parentf62bb79
2 files changed+25−19
src/Servant/JQuery/Internal.hs+18−10 test/Servant/JQuerySpec.hs+7−9
src/Servant/JQuery/Internal.hs+18−10
@@ -1,9 +1,12 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE TypeFamilies #-}
33{-# LANGUAGE TypeOperators #-}
4+{-# LANGUAGE ConstraintKinds #-}
45{-# LANGUAGE TemplateHaskell #-}
6+{-# LANGUAGE FlexibleContexts #-}
57{-# LANGUAGE FlexibleInstances #-}
68{-# LANGUAGE ScopedTypeVariables #-}
9+{-# LANGUAGE UndecidableInstances #-}
710module Servant.JQuery.Internal where
811
912import Control.Applicative
@@ -15,6 +18,7 @@ import Data.List
1518import Data.Monoid
1619import Data.Proxy
1720import qualified Data.Text as T
21+import GHC.Exts (Constraint)
1822import GHC.TypeLits
1923import Servant.API
2024
@@ -157,7 +161,12 @@ paramToStr qarg notTheEnd =
157161defReq :: AjaxReq
158162defReq = AjaxReq defUrl "GET" [] False ""
159163
160−class HasJQ layout where
164+type family Elem (a :: *) (ls::[*]) :: Constraint where
165+ Elem a '[] = 'False ~ 'True
166+ Elem a (a ': list) = ()
167+ Elem a (b ': list) = Elem a list
168+
169+class HasJQ (layout :: *) where
161170 type JQ layout :: *
162171 jqueryFor :: Proxy layout -> AjaxReq -> JQ layout
163172
@@ -186,8 +195,8 @@ instance HasJQ Delete where
186195 req & funcName %~ ("delete" <>)
187196 & reqMethod .~ "DELETE"
188197
189−instance HasJQ (Get a) where
190− type JQ (Get a) = AjaxReq
198+instance Elem JSON list => HasJQ (Get list a) where
199+ type JQ (Get list a) = AjaxReq
191200
192201 jqueryFor Proxy req =
193202 req & funcName %~ ("get" <>)
@@ -203,15 +212,15 @@ instance (KnownSymbol sym, HasJQ sublayout)
203212 where hname = symbolVal (Proxy :: Proxy sym)
204213 subP = Proxy :: Proxy sublayout
205214
206−instance HasJQ (Post a) where
207− type JQ (Post a) = AjaxReq
215+instance Elem JSON list => HasJQ (Post list a) where
216+ type JQ (Post list a) = AjaxReq
208217
209218 jqueryFor Proxy req =
210219 req & funcName %~ ("post" <>)
211220 & reqMethod .~ "POST"
212221
213−instance HasJQ (Put a) where
214− type JQ (Put a) = AjaxReq
222+instance Elem JSON list => HasJQ (Put list a) where
223+ type JQ (Put list a) = AjaxReq
215224
216225 jqueryFor Proxy req =
217226 req & funcName %~ ("put" <>)
@@ -226,7 +235,6 @@ instance (KnownSymbol sym, HasJQ sublayout)
226235 req & reqUrl.queryStr <>~ [QueryArg str Normal]
227236
228237 where str = symbolVal (Proxy :: Proxy sym)
229− strArg = str ++ "Value"
230238
231239instance (KnownSymbol sym, HasJQ sublayout)
232240 => HasJQ (QueryParams sym a :> sublayout) where
@@ -255,8 +263,8 @@ instance HasJQ Raw where
255263 req & funcName %~ ((toLower <$> method) <>)
256264 & reqMethod .~ method
257265
258−instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
259− type JQ (ReqBody a :> sublayout) = JQ sublayout
266+instance (Elem JSON list, HasJQ sublayout) => HasJQ (ReqBody list a :> sublayout) where
267+ type JQ (ReqBody list a :> sublayout) = JQ sublayout
260268
261269 jqueryFor Proxy req =
262270 jqueryFor (Proxy :: Proxy sublayout) $
test/Servant/JQuerySpec.hs+7−9
@@ -16,25 +16,23 @@ import Servant.API
1616import Servant.JQuery
1717import Servant.JQuerySpec.CustomHeaders
1818
19−type TestAPI = [sitemap|
20−POST /simple String -> Bool
21−GET /has.extension Bool
22−|]
19+type TestAPI = "simple" :> ReqBody '[JSON] String :> Post '[JSON] Bool
20+ :<|> "has.extension" :> Get '[JSON] Bool
2321
24−type TopLevelRawAPI = "something" :> Get Int
22+type TopLevelRawAPI = "something" :> Get '[JSON] Int
2523 :<|> Raw
2624
2725type HeaderHandlingAPI = "something" :> Header "Foo" String
28− :> Get Int
26+ :> Get '[JSON] Int
2927
3028type CustomAuthAPI = "something" :> Authorization "Basic" String
31− :> Get Int
29+ :> Get '[JSON] Int
3230
3331type CustomHeaderAPI = "something" :> MyLovelyHorse String
34− :> Get Int
32+ :> Get '[JSON] Int
3533
3634type CustomHeaderAPI2 = "something" :> WhatsForDinner String
37− :> Get Int
35+ :> Get '[JSON] Int
3836
3937headerHandlingProxy :: Proxy HeaderHandlingAPI
4038headerHandlingProxy = Proxy