From 8b3258a0c101951015acad60930a89049d2b6ad8 Mon Sep 17 00:00:00 2001 From: "Julian K. Arni" Date: Sun, 27 Dec 2015 02:20:46 +0100 Subject: [PATCH] Add ToHttpApiData instance for Link. --- servant/src/Servant/Utils/Links.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/servant/src/Servant/Utils/Links.hs b/servant/src/Servant/Utils/Links.hs index b6bf7137..f218377f 100644 --- a/servant/src/Servant/Utils/Links.hs +++ b/servant/src/Servant/Utils/Links.hs @@ -103,7 +103,8 @@ module Servant.Utils.Links ( import Data.List import Data.Proxy ( Proxy(..) ) -import Data.Text (Text, unpack) +import qualified Data.Text as Text +import qualified Data.ByteString.Char8 as BSC #if !MIN_VERSION_base(4,8,0) import Data.Monoid ( Monoid(..), (<>) ) #else @@ -135,6 +136,10 @@ data Link = Link , _queryParams :: [Param Query] } deriving Show +instance ToHttpApiData Link where + toUrlPiece = Text.pack . show + toHeader = BSC.pack . show + -- | If either a or b produce an empty constraint, produce an empty constraint. type family Or (a :: Constraint) (b :: Constraint) :: Constraint where -- This works because of: @@ -193,8 +198,8 @@ data Query -- | Query param data Param a - = SingleParam String Text - | ArrayElemParam String Text + = SingleParam String Text.Text + | ArrayElemParam String Text.Text | FlagParam String deriving Show @@ -218,8 +223,8 @@ linkURI (Link segments q_params) = "?" <> intercalate "&" (fmap makeQuery xs) makeQuery :: Param Query -> String - makeQuery (ArrayElemParam k v) = escape k <> "[]=" <> escape (unpack v) - makeQuery (SingleParam k v) = escape k <> "=" <> escape (unpack v) + makeQuery (ArrayElemParam k v) = escape k <> "[]=" <> escape (Text.unpack v) + makeQuery (SingleParam k v) = escape k <> "=" <> escape (Text.unpack v) makeQuery (FlagParam k) = escape k escape :: String -> String @@ -291,7 +296,7 @@ instance (ToHttpApiData v, HasLink sub) type MkLink (Capture sym v :> sub) = v -> MkLink sub toLink _ l v = toLink (Proxy :: Proxy sub) $ - addSegment (escape . unpack $ toUrlPiece v) l + addSegment (escape . Text.unpack $ toUrlPiece v) l instance HasLink sub => HasLink (Header sym a :> sub) where type MkLink (Header sym a :> sub) = MkLink sub