git.iko.soy/typeable/servant
on master help sign in
servant-foreign support for CaptureAll
commitcda31614bfa6a65ad22d3f5f6bf0a9386d311bc5 authorJonathan Lange <jml@mumak.net> dateThu 26 May 2016, 20:51 (10 years ago) parentc79a254
2 files changed+31−2
servant-foreign/src/Servant/Foreign/Internal.hs+15 servant-foreign/test/Servant/ForeignSpec.hs+16−2
servant-foreign/src/Servant/Foreign/Internal.hs+15
@@ -211,6 +211,21 @@ instance (KnownSymbol sym, HasForeignType lang ftype t, HasForeign lang ftype ap
211211 { _argName = PathSegment str
212212 , _argType = ftype }
213213
214+instance (KnownSymbol sym, HasForeignType lang ftype [t], HasForeign lang ftype sublayout)
215+ => HasForeign lang ftype (CaptureAll sym t :> sublayout) where
216+ type Foreign ftype (CaptureAll sym t :> sublayout) = Foreign ftype sublayout
217+
218+ foreignFor lang Proxy Proxy req =
219+ foreignFor lang Proxy (Proxy :: Proxy sublayout) $
220+ req & reqUrl . path <>~ [Segment (Cap arg)]
221+ & reqFuncName . _FunctionName %~ (++ ["by", str])
222+ where
223+ str = pack . symbolVal $ (Proxy :: Proxy sym)
224+ ftype = typeFor lang (Proxy :: Proxy ftype) (Proxy :: Proxy [t])
225+ arg = Arg
226+ { _argName = PathSegment str
227+ , _argType = ftype }
228+
214229instance (Elem JSON list, HasForeignType lang ftype a, ReflectMethod method)
215230 => HasForeign lang ftype (Verb method status list a) where
216231 type Foreign ftype (Verb method status list a) = Req ftype
servant-foreign/test/Servant/ForeignSpec.hs+16−2
@@ -46,6 +46,7 @@ type TestApi
4646 :<|> "test" :> QueryParam "param" Int :> ReqBody '[JSON] [String] :> Post '[JSON] NoContent
4747 :<|> "test" :> QueryParams "params" Int :> ReqBody '[JSON] String :> Put '[JSON] NoContent
4848 :<|> "test" :> Capture "id" Int :> Delete '[JSON] NoContent
49+ :<|> "test" :> CaptureAll "ids" Int :> Get '[JSON] [Int]
4950
5051testApi :: [Req String]
5152testApi = listFromAPI (Proxy :: Proxy LangX) (Proxy :: Proxy String) (Proxy :: Proxy TestApi)
@@ -53,9 +54,9 @@ testApi = listFromAPI (Proxy :: Proxy LangX) (Proxy :: Proxy String) (Proxy :: P
5354listFromAPISpec :: Spec
5455listFromAPISpec = describe "listFromAPI" $ do
5556 it "generates 4 endpoints for TestApi" $ do
56− length testApi `shouldBe` 4
57+ length testApi `shouldBe` 5
5758
58− let [getReq, postReq, putReq, deleteReq] = testApi
59+ let [getReq, postReq, putReq, deleteReq, captureAllReq] = testApi
5960
6061 it "collects all info for get request" $ do
6162 shouldBe getReq $ defReq
@@ -106,3 +107,16 @@ listFromAPISpec = describe "listFromAPI" $ do
106107 , _reqReturnType = Just "voidX"
107108 , _reqFuncName = FunctionName ["delete", "test", "by", "id"]
108109 }
110+
111+ it "collects all info for capture all request" $ do
112+ shouldBe captureAllReq $ defReq
113+ { _reqUrl = Url
114+ [ Segment $ Static "test"
115+ , Segment $ Cap (Arg "ids" "listX of intX") ]
116+ []
117+ , _reqMethod = "GET"
118+ , _reqHeaders = []
119+ , _reqBody = Nothing
120+ , _reqReturnType = Just "listX of intX"
121+ , _reqFuncName = FunctionName ["get", "test", "by", "ids"]
122+ }