git.iko.soy/typeable/servant
on master help sign in
Merge pull request #132 from haskell-servant/fix-129-queryparam-link
7399f30Alp Mestanogullari
servant/src/Servant/Utils/Links.hs+8−8
@@ -49,7 +49,7 @@
49-- or without those:
49-- or without those:
50--
50--
51-- >>> let with = Proxy :: Proxy ("bye" :> QueryParam "name" String :> Delete '[JSON] ())
51-- >>> let with = Proxy :: Proxy ("bye" :> QueryParam "name" String :> Delete '[JSON] ())
52−-- >>> print $ safeLink api with "Hubert"
52+-- >>> print $ safeLink api with (Just "Hubert")
53-- bye?name=Hubert
53-- bye?name=Hubert
54--
54--
55-- >>> let without = Proxy :: Proxy ("bye" :> Delete '[JSON] ())
55-- >>> let without = Proxy :: Proxy ("bye" :> Delete '[JSON] ())
@@ -273,10 +273,10 @@ instance (KnownSymbol sym, HasLink sub) => HasLink (sym :> sub) where
273-- QueryParam instances
273-- QueryParam instances
274instance (KnownSymbol sym, ToText v, HasLink sub)
274instance (KnownSymbol sym, ToText v, HasLink sub)
275 => HasLink (QueryParam sym v :> sub) where
275 => HasLink (QueryParam sym v :> sub) where
276− type MkLink (QueryParam sym v :> sub) = v -> MkLink sub
276+ type MkLink (QueryParam sym v :> sub) = Maybe v -> MkLink sub
277− toLink _ l v =
277+ toLink _ l mv =
278− toLink (Proxy :: Proxy sub)
278+ toLink (Proxy :: Proxy sub) $
279− (addQueryParam (SingleParam k (toText v)) l)
279+ maybe id (addQueryParam . SingleParam k . toText) mv l
280 where
280 where
281 k :: String
281 k :: String
282 k = symbolVal (Proxy :: Proxy sym)
282 k = symbolVal (Proxy :: Proxy sym)
@@ -303,10 +303,10 @@ instance (KnownSymbol sym, HasLink sub)
303-- MatrixParam instances
303-- MatrixParam instances
304instance (KnownSymbol sym, ToText v, HasLink sub)
304instance (KnownSymbol sym, ToText v, HasLink sub)
305 => HasLink (MatrixParam sym v :> sub) where
305 => HasLink (MatrixParam sym v :> sub) where
306− type MkLink (MatrixParam sym v :> sub) = v -> MkLink sub
306+ type MkLink (MatrixParam sym v :> sub) = Maybe v -> MkLink sub
307− toLink _ l v =
307+ toLink _ l mv =
308 toLink (Proxy :: Proxy sub) $
308 toLink (Proxy :: Proxy sub) $
309− addMatrixParam (SingleParam k (toText v)) l
309+ maybe id (addMatrixParam . SingleParam k . toText) mv l
310 where
310 where
311 k = symbolVal (Proxy :: Proxy sym)
311 k = symbolVal (Proxy :: Proxy sym)
312
312
servant/test/Servant/Utils/LinksSpec.hs+2−2
@@ -61,13 +61,13 @@ spec = describe "Servant.Utils.Links" $ do
61 let l2 = Proxy :: Proxy ("hello" :> Capture "name" String
61 let l2 = Proxy :: Proxy ("hello" :> Capture "name" String
62 :> QueryParam "capital" Bool
62 :> QueryParam "capital" Bool
63 :> Delete '[JSON] ())
63 :> Delete '[JSON] ())
64− apiLink l2 "bye" True `shouldBeURI` "hello/bye?capital=true"
64+ apiLink l2 "bye" (Just True) `shouldBeURI` "hello/bye?capital=true"
65
65
66 let l3 = Proxy :: Proxy ("parent" :> MatrixParams "name" String
66 let l3 = Proxy :: Proxy ("parent" :> MatrixParams "name" String
67 :> "child"
67 :> "child"
68 :> MatrixParam "gender" String
68 :> MatrixParam "gender" String
69 :> Get '[JSON] String)
69 :> Get '[JSON] String)
70− apiLink l3 ["Hubert?x=;&", "Cumberdale"] "Edward?"
70+ apiLink l3 ["Hubert?x=;&", "Cumberdale"] (Just "Edward?")
71 `shouldBeURI` "parent;name[]=Hubert%3Fx%3D%3B%26;\
71 `shouldBeURI` "parent;name[]=Hubert%3Fx%3D%3B%26;\
72 \name[]=Cumberdale/child;gender=Edward%3F"
72 \name[]=Cumberdale/child;gender=Edward%3F"
73
73