@@ -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) $