git.iko.soy/typeable/http-api-data
on master help sign in
Merge pull request #103 from phadej/pr-101-const-instances
114bd55Oleg Grenrus
http-api-data.cabal+1
@@ -48,6 +48,7 @@ library
48 , bytestring >= 0.10.4.0 && < 0.11
48 , bytestring >= 0.10.4.0 && < 0.11
49 , containers >= 0.5.5.1 && < 0.7
49 , containers >= 0.5.5.1 && < 0.7
50 , text >= 1.2.3.0 && < 1.3
50 , text >= 1.2.3.0 && < 1.3
51+ , transformers >= 0.3 && < 0.6
51
52
52 -- so Semigroup Builder exists
53 -- so Semigroup Builder exists
53 if impl(ghc >= 8.0)
54 if impl(ghc >= 8.0)
src/Web/Internal/FormUrlEncoded.hs+17−1
@@ -1,5 +1,5 @@
1−{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
1{-# LANGUAGE ConstraintKinds #-}
2+{-# LANGUAGE CPP #-}
3{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE DefaultSignatures #-}
4{-# LANGUAGE DefaultSignatures #-}
5{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE DeriveGeneric #-}
@@ -20,6 +20,7 @@ module Web.Internal.FormUrlEncoded where
20import Prelude ()
20import Prelude ()
21import Prelude.Compat
21import Prelude.Compat
22
22
23+import Control.Applicative (Const(Const))
23import Control.Arrow ((***))
24import Control.Arrow ((***))
24import Control.Monad ((<=<))
25import Control.Monad ((<=<))
25import Data.ByteString.Builder (shortByteString, toLazyByteString)
26import Data.ByteString.Builder (shortByteString, toLazyByteString)
@@ -27,6 +28,7 @@ import qualified Data.ByteString.Lazy as BSL
27import qualified Data.ByteString.Lazy.Char8 as BSL8
28import qualified Data.ByteString.Lazy.Char8 as BSL8
28import Data.Coerce (coerce)
29import Data.Coerce (coerce)
29import qualified Data.Foldable as F
30import qualified Data.Foldable as F
31+import Data.Functor.Identity (Identity(Identity))
30import Data.Hashable (Hashable)
32import Data.Hashable (Hashable)
31import Data.HashMap.Strict (HashMap)
33import Data.HashMap.Strict (HashMap)
32import qualified Data.HashMap.Strict as HashMap
34import qualified Data.HashMap.Strict as HashMap
@@ -130,6 +132,13 @@ instance ToFormKey a => ToFormKey (Semi.Last a) where toFormKey = coerce (toFor
130
132
131instance ToFormKey a => ToFormKey (Tagged b a) where toFormKey = coerce (toFormKey :: a -> Text)
133instance ToFormKey a => ToFormKey (Tagged b a) where toFormKey = coerce (toFormKey :: a -> Text)
132
134
135+-- | @since 0.4.2
136+instance ToFormKey a => ToFormKey (Identity a) where toFormKey = coerce (toFormKey :: a -> Text)
137+
138+-- | @since 0.4.2
139+instance ToFormKey a => ToFormKey (Const a b) where
140+ toFormKey = coerce (toFormKey :: a -> Text)
141+
133instance ToFormKey Void where toFormKey = toQueryParam
142instance ToFormKey Void where toFormKey = toQueryParam
134instance ToFormKey Natural where toFormKey = toQueryParam
143instance ToFormKey Natural where toFormKey = toQueryParam
135
144
@@ -182,6 +191,13 @@ instance FromFormKey a => FromFormKey (Semi.Last a) where parseFormKey = coerce
182
191
183instance FromFormKey a => FromFormKey (Tagged b a) where parseFormKey = coerce (parseFormKey :: Text -> Either Text a)
192instance FromFormKey a => FromFormKey (Tagged b a) where parseFormKey = coerce (parseFormKey :: Text -> Either Text a)
184
193
194+-- | @since 0.4.2
195+instance FromFormKey a => FromFormKey (Identity a) where parseFormKey = coerce (parseFormKey :: Text -> Either Text a)
196+
197+-- | @since 0.4.2
198+instance FromFormKey a => FromFormKey (Const a b) where
199+ parseFormKey = coerce (parseFormKey :: Text -> Either Text a)
200+
185instance FromFormKey Void where parseFormKey = parseQueryParam
201instance FromFormKey Void where parseFormKey = parseQueryParam
186instance FromFormKey Natural where parseFormKey = parseQueryParam
202instance FromFormKey Natural where parseFormKey = parseQueryParam
187
203
src/Web/Internal/HttpApiData.hs+43−7
@@ -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 #-}
11-- |
13-- |
@@ -16,6 +18,7 @@ module Web.Internal.HttpApiData where
16import Prelude ()
18import Prelude ()
17import Prelude.Compat
19import Prelude.Compat
18
20
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)
66
70
71+#if MIN_VERSION_base(4,9,0)
72+import Data.Kind (Type)
73+#else
74+#define Type *
75+#endif
67
76
68#if USE_TEXT_SHOW
77#if USE_TEXT_SHOW
69import TextShow (TextShow, showt)
78import TextShow (TextShow, showt)
70#endif
79#endif
71
80
72−
73−
74−
75-- $setup
81-- $setup
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
457
463
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
459
466
460-- |
467-- |
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.
594
601
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)
608+
609+-- | @since 0.4.2
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)
615+
616+-- | @since 0.4.2
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
652
674
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
655
678
656-- |
679-- |
@@ -755,7 +778,20 @@ instance FromHttpApiData SetCookie where
755 parseUrlPiece = parseHeader . encodeUtf8
778 parseUrlPiece = parseHeader . encodeUtf8
756 parseHeader = Right . parseSetCookie
779 parseHeader = Right . parseSetCookie
757
780
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)
786+
787+-- | @since 0.4.2
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)
792+
793+-- | @since 0.4.2
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)