on master help sign in
Added support for matrix parameters
commitfe541f3bf485978f3c6461b2aaa0b1abae281143 authorDaniel Larsson <daniel.j.larsson@gmail.com> dateSun 28 Dec 2014, 22:56 (12 years ago) parent7ea1203
3 files changed+39
servant.cabal+1 src/Servant/API.hs+3 src/Servant/API/MatrixParam.hs+35
servant.cabal+1
@@ -34,6 +34,7 @@ library
3434 Servant.API.Post
3535 Servant.API.Put
3636 Servant.API.QueryParam
37+ Servant.API.MatrixParam
3738 Servant.API.Raw
3839 Servant.API.ReqBody
3940 Servant.API.Sub
src/Servant/API.hs+3
@@ -15,6 +15,8 @@ module Servant.API (
1515 module Servant.API.QueryParam,
1616 -- | Accessing the request body as a JSON-encoded type: @'ReqBody'@
1717 module Servant.API.ReqBody,
18+ -- | Retrieving matrix parameters from the 'URI' segment: @'MatrixParam'@
19+ module Servant.API.MatrixParam,
1820
1921 -- * Actual endpoints, distinguished by HTTP method
2022 -- | GET requests
@@ -45,6 +47,7 @@ import Servant.API.Header
4547import Servant.API.Post
4648import Servant.API.Put
4749import Servant.API.QueryParam
50+import Servant.API.MatrixParam
4851import Servant.API.Raw
4952import Servant.API.ReqBody
5053import Servant.API.Sub
src/Servant/API/MatrixParam.hsnew file+35
@@ -0,0 +1,35 @@
1+{-# LANGUAGE PolyKinds #-}
2+module Servant.API.MatrixParam where
3+
4+-- | Lookup the value associated to the @sym@ matrix string parameter
5+-- and try to extract it as a value of type @a@.
6+--
7+-- Example:
8+--
9+-- > -- /books;author=<author name>
10+-- > type MyApi = "books" :> MatrixParam "author" Text :> Get [Book]
11+data MatrixParam sym a
12+
13+-- | Lookup the values associated to the @sym@ matrix string parameter
14+-- and try to extract it as a value of type @[a]@. This is typically
15+-- meant to support matrix string parameters of the form
16+-- @param[]=val1;param[]=val2@ and so on. Note that servant doesn't actually
17+-- require the @[]@s and will fetch the values just fine with
18+-- @param=val1;param=val2@, too.
19+--
20+-- Example:
21+--
22+-- > -- /books;authors[]=<author1>;authors[]=<author2>;...
23+-- > type MyApi = "books" :> MatrixParams "authors" Text :> Get [Book]
24+data MatrixParams sym a
25+
26+-- | Lookup a potentially value-less matrix string parameter
27+-- with boolean semantics. If the param @sym@ is there without any value,
28+-- or if it's there with value "true" or "1", it's interpreted as 'True'.
29+-- Otherwise, it's interpreted as 'False'.
30+--
31+-- Example:
32+--
33+-- > -- /books;published
34+-- > type MyApi = "books" :> MatrixFlag "published" :> Get [Book]
35+data MatrixFlag sym