git.iko.soy/typeable/servant
on master help sign in
rename GetParam to QueryParam everywhere
commitfc67c3b7aab02c81cfb9eb9c918dba1073c2a8f9 authorAlp Mestanogullari <alpmestan@gmail.com> dateTue 28 Oct 2014, 15:06 (12 years ago) parent641ee69
6 files changed+53−47
example/greet.hs+5−5 servant.cabal+1−1 src/Servant/API.hs+3−3 src/Servant/API/GetParam.hs → src/Servant/API/QueryParam.hs+16−10 src/Servant/Docs.hs+16−16 test/Servant/ServerSpec.hs+12−12
example/greet.hs+5−5
@@ -37,11 +37,11 @@ instance ToCapture (Capture "name" Text) where
3737instance ToCapture (Capture "greetid" Text) where
3838 toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
3939
40−instance ToParam (GetParam "capital" Bool) where
40+instance ToParam (QueryParam "capital" Bool) where
4141 toParam _ =
42− DocGetParam "capital"
43− ["true", "false"]
44− "Get the greeting message in uppercase (true) or not (false). Default is false."
42+ DocQueryParam "capital"
43+ ["true", "false"]
44+ "Get the greeting message in uppercase (true) or not (false). Default is false."
4545
4646instance ToSample Greet where
4747 toSample Proxy = Just (encode g)
@@ -50,7 +50,7 @@ instance ToSample Greet where
5050
5151-- API specification
5252type TestApi =
53− "hello" :> Capture "name" Text :> GetParam "capital" Bool :> Get Greet
53+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
5454 :<|> "greet" :> RQBody Greet :> Post Greet
5555 :<|> "delete" :> Capture "greetid" Text :> Delete
5656
servant.cabal+1−1
@@ -22,9 +22,9 @@ library
2222 Servant.API.Capture
2323 Servant.API.Delete
2424 Servant.API.Get
25− Servant.API.GetParam
2625 Servant.API.Post
2726 Servant.API.Put
27+ Servant.API.QueryParam
2828 Servant.API.Raw
2929 Servant.API.RQBody
3030 Servant.API.Sub
src/Servant/API.hs+3−3
@@ -9,8 +9,8 @@ module Servant.API (
99 -- * Accessing information from the request
1010 -- | Capturing parts of the url path as parsed values: @'Capture'@
1111 module Servant.API.Capture,
12− -- | Retrieving parameters from the query part of the 'URI': @'GetParam'@
13− module Servant.API.GetParam,
12+ -- | Retrieving parameters from the query part of the 'URI': @'QueryParam'@
13+ module Servant.API.QueryParam,
1414 -- | Accessing the request's body: @'RQBody'@
1515 module Servant.API.RQBody,
1616
@@ -28,9 +28,9 @@ module Servant.API (
2828import Servant.API.Capture
2929import Servant.API.Delete
3030import Servant.API.Get
31−import Servant.API.GetParam
3231import Servant.API.Post
3332import Servant.API.Put
33+import Servant.API.QueryParam
3434import Servant.API.RQBody
3535import Servant.API.Sub
3636import Servant.API.Union
src/Servant/API/GetParam.hs → src/Servant/API/QueryParam.hsrenamed+16−10
@@ -4,7 +4,7 @@
44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE ScopedTypeVariables #-}
7−module Servant.API.GetParam where
7+module Servant.API.QueryParam where
88
99import Data.Proxy
1010import Data.String.Conversions
@@ -18,13 +18,19 @@ import Servant.Docs
1818import Servant.Server
1919import Servant.Utils.Text
2020
21−-- * GET params support (i.e query string arguments)
22−data GetParam sym a
21+-- * Query String parameter lookup
22+
23+-- | Your must implement:
24+--
25+-- - a @'FromText' a@ instance for serving
26+-- - a @'ToText' a@ instance for (client-side) querying
27+-- - a @'ToParam' ('QueryParam' sym a)@ instance for automatic documentation generation
28+data QueryParam sym a
2329
2430instance (KnownSymbol sym, FromText a, HasServer sublayout)
25− => HasServer (GetParam sym a :> sublayout) where
31+ => HasServer (QueryParam sym a :> sublayout) where
2632
27− type Server (GetParam sym a :> sublayout) =
33+ type Server (QueryParam sym a :> sublayout) =
2834 Maybe a -> Server sublayout
2935
3036 route Proxy subserver request respond = do
@@ -41,9 +47,9 @@ instance (KnownSymbol sym, FromText a, HasServer sublayout)
4147 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
4248
4349instance (KnownSymbol sym, ToText a, HasClient sublayout)
44− => HasClient (GetParam sym a :> sublayout) where
50+ => HasClient (QueryParam sym a :> sublayout) where
4551
46− type Client (GetParam sym a :> sublayout) =
52+ type Client (QueryParam sym a :> sublayout) =
4753 Maybe a -> Client sublayout
4854
4955 -- if mparam = Nothing, we don't add it to the query string
@@ -55,12 +61,12 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
5561 pname' = symbolVal (Proxy :: Proxy sym)
5662 mparamText = fmap toText mparam
5763
58−instance (KnownSymbol sym, ToParam (GetParam sym a), HasDocs sublayout)
59− => HasDocs (GetParam sym a :> sublayout) where
64+instance (KnownSymbol sym, ToParam (QueryParam sym a), HasDocs sublayout)
65+ => HasDocs (QueryParam sym a :> sublayout) where
6066
6167 docsFor Proxy (endpoint, action) =
6268 docsFor sublayoutP (endpoint, action')
6369
6470 where sublayoutP = Proxy :: Proxy sublayout
65− paramP = Proxy :: Proxy (GetParam sym a)
71+ paramP = Proxy :: Proxy (QueryParam sym a)
6672 action' = over params (|> toParam paramP) action
src/Servant/Docs.hs+16−16
@@ -56,11 +56,11 @@
5656-- >
5757-- > where g = Greet "Hello, haskeller!"
5858-- >
59−-- > instance ToParam (GetParam "capital" Bool) where
59+-- > instance ToParam (QueryParam "capital" Bool) where
6060-- > toParam _ =
61−-- > DocGetParam "capital"
62−-- > ["true", "false"]
63−-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
61+-- > DocQueryParam "capital"
62+-- > ["true", "false"]
63+-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
6464-- >
6565-- > instance ToCapture (Capture "name" Text) where
6666-- > toCapture _ = DocCapture "name" "name of the person to greet"
@@ -70,7 +70,7 @@
7070-- >
7171-- > -- API specification
7272-- > type TestApi =
73−-- > "hello" :> Capture "name" Text :> GetParam "capital" Bool :> Get Greet
73+-- > "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
7474-- > :<|> "greet" :> RQBody Greet :> Post Greet
7575-- > :<|> "delete" :> Capture "greetid" Text :> Delete
7676-- >
@@ -95,7 +95,7 @@ module Servant.Docs
9595 , Endpoint, path, method, defEndpoint
9696 , API, emptyAPI
9797 , DocCapture(..), capSymbol, capDesc
98− , DocGetParam(..), paramName, paramValues, paramDesc
98+ , DocQueryParam(..), paramName, paramValues, paramDesc
9999 , Response, respStatus, respBody, defResponse
100100 , Action, captures, params, rqbody, response, defAction
101101 , single
@@ -190,12 +190,12 @@ data DocCapture = DocCapture
190190 , _capDesc :: String -- user supplied
191191 } deriving (Eq, Show)
192192
193−-- | A type to represent /GET/ parameters. Holds its name,
193+-- | A type to represent a /GET/ parameter from the Query String. Holds its name,
194194-- the possible values (leave empty if there isn't a finite number of them),
195195-- and a description of how it influences the output or behavior.
196196--
197197-- Write a 'ToParam' instance for your GET parameter types
198−data DocGetParam = DocGetParam
198+data DocQueryParam = DocQueryParam
199199 { _paramName :: String -- type supplied
200200 , _paramValues :: [String] -- user supplied
201201 , _paramDesc :: String -- user supplied
@@ -243,7 +243,7 @@ defResponse = Response 200 Nothing
243243-- to transform an action and add some information to it.
244244data Action = Action
245245 { _captures :: [DocCapture] -- type collected + user supplied info
246− , _params :: [DocGetParam] -- type collected + user supplied info
246+ , _params :: [DocQueryParam] -- type collected + user supplied info
247247 , _rqbody :: Maybe ByteString -- user supplied
248248 , _response :: Response -- user supplied
249249 } deriving (Eq, Show)
@@ -273,7 +273,7 @@ single = HM.singleton
273273-- gimme some lenses
274274makeLenses ''Endpoint
275275makeLenses ''DocCapture
276−makeLenses ''DocGetParam
276+makeLenses ''DocQueryParam
277277makeLenses ''Response
278278makeLenses ''Action
279279
@@ -319,13 +319,13 @@ class ToJSON a => ToSample a where
319319--
320320-- Example of an instance:
321321--
322−-- > instance ToParam (GetParam "capital" Bool) where
322+-- > instance ToParam (QueryParam "capital" Bool) where
323323-- > toParam _ =
324−-- > DocGetParam "capital"
325−-- > ["true", "false"]
326−-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
324+-- > DocQueryParam "capital"
325+-- > ["true", "false"]
326+-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
327327class ToParam t where
328− toParam :: Proxy t -> DocGetParam
328+ toParam :: Proxy t -> DocQueryParam
329329
330330-- | The class that helps us automatically get documentation
331331-- for URL captures.
@@ -364,7 +364,7 @@ printMarkdown = imapM_ printEndpoint
364364 captureStr cap =
365365 putStrLn $ "- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc)
366366
367− paramsStr :: [DocGetParam] -> IO ()
367+ paramsStr :: [DocQueryParam] -> IO ()
368368 paramsStr [] = return ()
369369 paramsStr l = do
370370 putStrLn "**GET Parameters**: "
test/Servant/ServerSpec.hs+12−12
@@ -22,8 +22,8 @@ import Test.Hspec.Wai
2222
2323import Servant.API.Capture
2424import Servant.API.Get
25−import Servant.API.GetParam
2625import Servant.API.Post
26+import Servant.API.QueryParam
2727import Servant.API.Raw
2828import Servant.API.RQBody
2929import Servant.API.Sub
@@ -67,7 +67,7 @@ spec :: Spec
6767spec = do
6868 captureSpec
6969 getSpec
70− getParamSpec
70+ queryParamSpec
7171 postSpec
7272 rawSpec
7373 unionSpec
@@ -117,19 +117,19 @@ getSpec = do
117117 post "/" "" `shouldRespondWith` 405
118118
119119
120−type GetParamApi = GetParam "name" String :> Get Person
121−getParamApi :: Proxy GetParamApi
122−getParamApi = Proxy
120+type QueryParamApi = QueryParam "name" String :> Get Person
121+queryParamApi :: Proxy QueryParamApi
122+queryParamApi = Proxy
123123
124−getParamServer :: Server GetParamApi
125−getParamServer (Just name) = return alice{name = name}
126−getParamServer Nothing = return alice
124+queryParamServer :: Server QueryParamApi
125+queryParamServer (Just name) = return alice{name = name}
126+queryParamServer Nothing = return alice
127127
128−getParamSpec :: Spec
129−getParamSpec = do
130− describe "Servant.API.GetParam" $ do
128+queryParamSpec :: Spec
129+queryParamSpec = do
130+ describe "Servant.API.QueryParam" $ do
131131 it "allows to retrieve GET parameters" $ do
132− (flip runSession) (serve getParamApi getParamServer) $ do
132+ (flip runSession) (serve queryParamApi queryParamServer) $ do
133133 let params = "?name=bob"
134134 response <- Network.Wai.Test.request defaultRequest{
135135 rawQueryString = params,