@@ -5,7 +5,9 @@
5{-# LANGUAGE DeriveFunctor #-}
5{-# LANGUAGE DeriveFunctor #-}
6{-# LANGUAGE DeriveTraversable #-}
6{-# LANGUAGE DeriveTraversable #-}
7{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE FlexibleInstances #-}
8+{-# LANGUAGE KindSignatures #-}
8{-# LANGUAGE OverloadedStrings #-}
9{-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE PolyKinds #-}
9{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TypeSynonymInstances #-}
12{-# LANGUAGE TypeSynonymInstances #-}
@@ -16,6 +18,7 @@ module Web.Internal.HttpApiData where
16import Prelude ()
18import Prelude ()
17import Prelude.Compat
19import Prelude.Compat
21+import Control.Applicative (Const(Const))
19import Control.Arrow (left, (&&&))
22import Control.Arrow (left, (&&&))
20import Control.Monad ((<=<))
23import Control.Monad ((<=<))
21import qualified Data.Attoparsec.Text as Atto
24import qualified Data.Attoparsec.Text as Atto
@@ -27,6 +30,7 @@ import qualified Data.ByteString.Lazy as LBS
27import Data.Coerce (coerce)
30import Data.Coerce (coerce)
28import Data.Data (Data)
31import Data.Data (Data)
29import qualified Data.Fixed as F
32import qualified Data.Fixed as F
33+import Data.Functor.Identity (Identity(Identity))
30import Data.Int (Int16, Int32, Int64, Int8)
34import Data.Int (Int16, Int32, Int64, Int8)
31import qualified Data.Map as Map
35import qualified Data.Map as Map
32import Data.Monoid (All (..), Any (..), Dual (..),
36import Data.Monoid (All (..), Any (..), Dual (..),
@@ -64,14 +68,16 @@ import Text.Read (readMaybe)
64import Web.Cookie (SetCookie, parseSetCookie,
68import Web.Cookie (SetCookie, parseSetCookie,
65 renderSetCookie)
69 renderSetCookie)
71+#if MIN_VERSION_base(4,9,0)
72+import Data.Kind (Type)
68#if USE_TEXT_SHOW
77#if USE_TEXT_SHOW
69import TextShow (TextShow, showt)
78import TextShow (TextShow, showt)
76-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
82-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
77-- >>> instance FromHttpApiData BasicAuthToken where parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h; parseQueryParam p = BasicAuthToken <$> parseQueryParam p
83-- >>> instance FromHttpApiData BasicAuthToken where parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h; parseQueryParam p = BasicAuthToken <$> parseQueryParam p
@@ -455,7 +461,8 @@ instance ToHttpApiData Word16 where toUrlPiece = showt; toEncodedUrlPiece = un
455instance ToHttpApiData Word32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
461instance ToHttpApiData Word32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
456instance ToHttpApiData Word64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
462instance ToHttpApiData Word64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
464+-- | Note: this instance is not polykinded
458−instance F.HasResolution a => ToHttpApiData (F.Fixed a) where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
465+instance F.HasResolution a => ToHttpApiData (F.Fixed (a :: Type)) where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
461-- >>> toUrlPiece (fromGregorian 2015 10 03)
468-- >>> toUrlPiece (fromGregorian 2015 10 03)
@@ -592,7 +599,22 @@ instance ToHttpApiData SetCookie where
592 toHeader = LBS.toStrict . BS.toLazyByteString . renderSetCookie
599 toHeader = LBS.toStrict . BS.toLazyByteString . renderSetCookie
593 -- toEncodedUrlPiece = renderSetCookie -- doesn't do things.
600 -- toEncodedUrlPiece = renderSetCookie -- doesn't do things.
602+-- | Note: this instance is not polykinded
595−instance ToHttpApiData a => ToHttpApiData (Tagged b a) where
603+instance ToHttpApiData a => ToHttpApiData (Tagged (b :: Type) a) where
604+ toUrlPiece = coerce (toUrlPiece :: a -> Text)
605+ toHeader = coerce (toHeader :: a -> ByteString)
606+ toQueryParam = coerce (toQueryParam :: a -> Text)
607+ toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS.Builder)
610+instance ToHttpApiData a => ToHttpApiData (Const a b) where
611+ toUrlPiece = coerce (toUrlPiece :: a -> Text)
612+ toHeader = coerce (toHeader :: a -> ByteString)
613+ toQueryParam = coerce (toQueryParam :: a -> Text)
614+ toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS.Builder)
617+instance ToHttpApiData a => ToHttpApiData (Identity a) where
596 toUrlPiece = coerce (toUrlPiece :: a -> Text)
618 toUrlPiece = coerce (toUrlPiece :: a -> Text)
597 toHeader = coerce (toHeader :: a -> ByteString)
619 toHeader = coerce (toHeader :: a -> ByteString)
598 toQueryParam = coerce (toQueryParam :: a -> Text)
620 toQueryParam = coerce (toQueryParam :: a -> Text)
@@ -650,7 +672,8 @@ instance FromHttpApiData String where parseUrlPiece = Right . T.unpack
650instance FromHttpApiData Text where parseUrlPiece = Right
672instance FromHttpApiData Text where parseUrlPiece = Right
651instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
673instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
675+-- | Note: this instance is not polykinded
653−instance F.HasResolution a => FromHttpApiData (F.Fixed a) where
676+instance F.HasResolution a => FromHttpApiData (F.Fixed (a :: Type)) where
654 parseUrlPiece = runReader rational
677 parseUrlPiece = runReader rational
@@ -755,7 +778,20 @@ instance FromHttpApiData SetCookie where
755 parseUrlPiece = parseHeader . encodeUtf8
778 parseUrlPiece = parseHeader . encodeUtf8
756 parseHeader = Right . parseSetCookie
779 parseHeader = Right . parseSetCookie
781+-- | Note: this instance is not polykinded
758−instance FromHttpApiData a => FromHttpApiData (Tagged b a) where
782+instance FromHttpApiData a => FromHttpApiData (Tagged (b :: Type) a) where
783+ parseUrlPiece = coerce (parseUrlPiece :: Text -> Either Text a)
784+ parseHeader = coerce (parseHeader :: ByteString -> Either Text a)
785+ parseQueryParam = coerce (parseQueryParam :: Text -> Either Text a)
788+instance FromHttpApiData a => FromHttpApiData (Const a b) where
789+ parseUrlPiece = coerce (parseUrlPiece :: Text -> Either Text a)
790+ parseHeader = coerce (parseHeader :: ByteString -> Either Text a)
791+ parseQueryParam = coerce (parseQueryParam :: Text -> Either Text a)
794+instance FromHttpApiData a => FromHttpApiData (Identity a) where
759 parseUrlPiece = coerce (parseUrlPiece :: Text -> Either Text a)
795 parseUrlPiece = coerce (parseUrlPiece :: Text -> Either Text a)
760 parseHeader = coerce (parseHeader :: ByteString -> Either Text a)
796 parseHeader = coerce (parseHeader :: ByteString -> Either Text a)
761 parseQueryParam = coerce (parseQueryParam :: Text -> Either Text a)
797 parseQueryParam = coerce (parseQueryParam :: Text -> Either Text a)