From e8bae7d03561208f9d5e6def541ef932ca8264e7 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Thu, 18 Jun 2015 18:03:48 +0200 Subject: [PATCH 1/4] Tweak the HasLink instance for QueryParam, fixes #129 --- servant/src/Servant/Utils/Links.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/servant/src/Servant/Utils/Links.hs b/servant/src/Servant/Utils/Links.hs index 15f38096..ec6ebbd6 100644 --- a/servant/src/Servant/Utils/Links.hs +++ b/servant/src/Servant/Utils/Links.hs @@ -273,10 +273,10 @@ instance (KnownSymbol sym, HasLink sub) => HasLink (sym :> sub) where -- QueryParam instances instance (KnownSymbol sym, ToText v, HasLink sub) => HasLink (QueryParam sym v :> sub) where - type MkLink (QueryParam sym v :> sub) = v -> MkLink sub - toLink _ l v = - toLink (Proxy :: Proxy sub) - (addQueryParam (SingleParam k (toText v)) l) + type MkLink (QueryParam sym v :> sub) = Maybe v -> MkLink sub + toLink _ l mv = + toLink (Proxy :: Proxy sub) $ + maybe id (addQueryParam . SingleParam k . toText) mv l where k :: String k = symbolVal (Proxy :: Proxy sym) From 9fbd312b4c707d73fefb77eb6724767f4a4d0623 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Fri, 19 Jun 2015 10:03:24 +0200 Subject: [PATCH 2/4] adapt tests to the modified HasLink instance for QueryParam --- servant/test/Servant/Utils/LinksSpec.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servant/test/Servant/Utils/LinksSpec.hs b/servant/test/Servant/Utils/LinksSpec.hs index 0fa17b3a..b261559e 100644 --- a/servant/test/Servant/Utils/LinksSpec.hs +++ b/servant/test/Servant/Utils/LinksSpec.hs @@ -61,7 +61,7 @@ spec = describe "Servant.Utils.Links" $ do let l2 = Proxy :: Proxy ("hello" :> Capture "name" String :> QueryParam "capital" Bool :> Delete '[JSON] ()) - apiLink l2 "bye" True `shouldBeURI` "hello/bye?capital=true" + apiLink l2 "bye" (Just True) `shouldBeURI` "hello/bye?capital=true" let l3 = Proxy :: Proxy ("parent" :> MatrixParams "name" String :> "child" From 82328247e693b97f822d5fb56095f0ac20b064e3 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Fri, 19 Jun 2015 10:29:06 +0200 Subject: [PATCH 3/4] Fix doctests for Link/QueryParam --- servant/src/Servant/Utils/Links.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servant/src/Servant/Utils/Links.hs b/servant/src/Servant/Utils/Links.hs index ec6ebbd6..5fa8bd0b 100644 --- a/servant/src/Servant/Utils/Links.hs +++ b/servant/src/Servant/Utils/Links.hs @@ -49,7 +49,7 @@ -- or without those: -- -- >>> let with = Proxy :: Proxy ("bye" :> QueryParam "name" String :> Delete '[JSON] ()) --- >>> print $ safeLink api with "Hubert" +-- >>> print $ safeLink api with (Just "Hubert") -- bye?name=Hubert -- -- >>> let without = Proxy :: Proxy ("bye" :> Delete '[JSON] ()) From 0e22d19800514f14ca838257b2debb809e7ef939 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Fri, 19 Jun 2015 11:30:44 +0200 Subject: [PATCH 4/4] Make Link take a Maybe argument for MatrixParam, just like QueryParam --- servant/src/Servant/Utils/Links.hs | 6 +++--- servant/test/Servant/Utils/LinksSpec.hs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/servant/src/Servant/Utils/Links.hs b/servant/src/Servant/Utils/Links.hs index 5fa8bd0b..19fdc019 100644 --- a/servant/src/Servant/Utils/Links.hs +++ b/servant/src/Servant/Utils/Links.hs @@ -303,10 +303,10 @@ instance (KnownSymbol sym, HasLink sub) -- MatrixParam instances instance (KnownSymbol sym, ToText v, HasLink sub) => HasLink (MatrixParam sym v :> sub) where - type MkLink (MatrixParam sym v :> sub) = v -> MkLink sub - toLink _ l v = + type MkLink (MatrixParam sym v :> sub) = Maybe v -> MkLink sub + toLink _ l mv = toLink (Proxy :: Proxy sub) $ - addMatrixParam (SingleParam k (toText v)) l + maybe id (addMatrixParam . SingleParam k . toText) mv l where k = symbolVal (Proxy :: Proxy sym) diff --git a/servant/test/Servant/Utils/LinksSpec.hs b/servant/test/Servant/Utils/LinksSpec.hs index b261559e..16f13925 100644 --- a/servant/test/Servant/Utils/LinksSpec.hs +++ b/servant/test/Servant/Utils/LinksSpec.hs @@ -67,7 +67,7 @@ spec = describe "Servant.Utils.Links" $ do :> "child" :> MatrixParam "gender" String :> Get '[JSON] String) - apiLink l3 ["Hubert?x=;&", "Cumberdale"] "Edward?" + apiLink l3 ["Hubert?x=;&", "Cumberdale"] (Just "Edward?") `shouldBeURI` "parent;name[]=Hubert%3Fx%3D%3B%26;\ \name[]=Cumberdale/child;gender=Edward%3F"