Merge pull request #132 from haskell-servant/fix-129-queryparam-link
Tweak the HasLink instance for QueryParam, fixes #129
This commit is contained in:
commit
7399f30cc4
2 changed files with 10 additions and 10 deletions
|
@ -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] ())
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
|
|
@ -61,13 +61,13 @@ 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"
|
||||
:> 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"
|
||||
|
||||
|
|
Loading…
Reference in a new issue