@@ -19,11 +19,15 @@ import Control.Applicative
19import Control.Monad ((<=<))
19import Control.Monad ((<=<))
20import qualified Data.ByteString.Lazy as BSL
20import qualified Data.ByteString.Lazy as BSL
21import qualified Data.ByteString.Lazy.Char8 as BSL8
21import qualified Data.ByteString.Lazy.Char8 as BSL8
22−import qualified Data.Map as M
23+import qualified Data.Map as Map
23import Data.Monoid
24import Data.Monoid
26+import Data.Text (Text)
24−import qualified Data.Text as T
27+import qualified Data.Text as Text
25−import Data.Text.Encoding (decodeUtf8With, encodeUtf8)
28+import Data.Text.Encoding as Text
26import Data.Text.Encoding.Error (lenientDecode)
29import Data.Text.Encoding.Error (lenientDecode)
27import GHC.Exts (IsList (..))
31import GHC.Exts (IsList (..))
28import GHC.Generics
32import GHC.Generics
29import Network.URI (escapeURIString, isUnreserved,
33import Network.URI (escapeURIString, isUnreserved,
@@ -47,7 +51,7 @@ import Web.Internal.HttpApiData
47-- | The contents of a form, not yet URL-encoded.
51-- | The contents of a form, not yet URL-encoded.
49-- 'Form' can be URL-encoded with 'encodeForm' and URL-decoded with 'decodeForm'.
53-- 'Form' can be URL-encoded with 'encodeForm' and URL-decoded with 'decodeForm'.
50−newtype Form = Form { unForm :: M.Map T.Text T.Text }
54+newtype Form = Form { unForm :: Map Text Text }
51 deriving (Eq, Read, Generic, Monoid)
55 deriving (Eq, Read, Generic, Monoid)
53instance Show Form where
57instance Show Form where
@@ -55,9 +59,9 @@ instance Show Form where
55 showString "fromList " . shows (toList form)
59 showString "fromList " . shows (toList form)
57instance IsList Form where
61instance IsList Form where
58− type Item Form = (T.Text, T.Text)
62+ type Item Form = (Text, Text)
59− fromList = Form . M.fromList
63+ fromList = Form . Map.fromList
60− toList = M.toList . unForm
64+ toList = Map.toList . unForm
62-- | Convert a value into 'Form'.
66-- | Convert a value into 'Form'.
@@ -101,8 +105,8 @@ class ToForm a where
101 default toForm :: (Generic a, GToForm (Rep a)) => a -> Form
105 default toForm :: (Generic a, GToForm (Rep a)) => a -> Form
102 toForm = genericToForm
106 toForm = genericToForm
104−instance ToForm [(T.Text, T.Text)] where toForm = fromList
108+instance ToForm [(Text, Text)] where toForm = fromList
105−instance ToForm (M.Map T.Text T.Text) where toForm = Form
109+instance ToForm (Map Text Text) where toForm = Form
106instance ToForm Form where toForm = id
110instance ToForm Form where toForm = id
108-- | A 'Generic'-based implementation of 'toForm'.
112-- | A 'Generic'-based implementation of 'toForm'.
@@ -129,7 +133,7 @@ instance (GToForm f) => GToForm (M1 C x f) where
129instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
133instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
130 gToForm (M1 (K1 c)) = fromList [(key, toQueryParam c)]
134 gToForm (M1 (K1 c)) = fromList [(key, toQueryParam c)]
132− key = T.pack $ selName (Proxy3 :: Proxy3 s g p)
136+ key = Text.pack $ selName (Proxy3 :: Proxy3 s g p)
134-- | Parse 'Form' into a value.
138-- | Parse 'Form' into a value.
@@ -167,23 +171,23 @@ instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
167-- The default implementation will use 'parseQueryParam' for each field's value.
171-- The default implementation will use 'parseQueryParam' for each field's value.
168class FromForm a where
172class FromForm a where
169 -- | Parse 'Form' into a value.
173 -- | Parse 'Form' into a value.
170− fromForm :: Form -> Either T.Text a
174+ fromForm :: Form -> Either Text a
171 default fromForm :: (Generic a, GFromForm (Rep a))
175 default fromForm :: (Generic a, GFromForm (Rep a))
172− => Form -> Either T.Text a
176+ => Form -> Either Text a
173 fromForm = genericFromForm
177 fromForm = genericFromForm
175instance FromForm Form where fromForm = return
179instance FromForm Form where fromForm = return
176−instance FromForm [(T.Text, T.Text)] where fromForm = return . toList
180+instance FromForm [(Text, Text)] where fromForm = return . toList
177−instance FromForm (M.Map T.Text T.Text) where fromForm = return . unForm
181+instance FromForm (Map Text Text) where fromForm = return . unForm
179-- | A 'Generic'-based implementation of 'fromForm'.
183-- | A 'Generic'-based implementation of 'fromForm'.
180-- This is used as a default implementation in 'FromForm'.
184-- This is used as a default implementation in 'FromForm'.
181genericFromForm :: (Generic a, GFromForm (Rep a))
185genericFromForm :: (Generic a, GFromForm (Rep a))
182− => Form -> Either T.Text a
186+ => Form -> Either Text a
183genericFromForm f = to <$> gFromForm f
187genericFromForm f = to <$> gFromForm f
185class GFromForm (f :: * -> *) where
189class GFromForm (f :: * -> *) where
186− gFromForm :: Form -> Either T.Text (f x)
190+ gFromForm :: Form -> Either Text (f x)
188instance (GFromForm f, GFromForm g) => GFromForm (f :*: g) where
192instance (GFromForm f, GFromForm g) => GFromForm (f :*: g) where
189 gFromForm f = (:*:) <$> gFromForm f <*> gFromForm f
193 gFromForm f = (:*:) <$> gFromForm f <*> gFromForm f
@@ -198,11 +202,11 @@ instance (GFromForm f, GFromForm g) => GFromForm (f :+: g) where
199instance (Selector s, FromHttpApiData f) => GFromForm (M1 S s (K1 i f)) where
203instance (Selector s, FromHttpApiData f) => GFromForm (M1 S s (K1 i f)) where
200 gFromForm f =
204 gFromForm f =
201− case M.lookup key (unForm f) of
205+ case Map.lookup key (unForm f) of
202− Nothing -> Left $ "Could not find key " <> T.pack (show key)
206+ Nothing -> Left $ "Could not find key " <> Text.pack (show key)
203 Just v -> M1 . K1 <$> parseQueryParam v
207 Just v -> M1 . K1 <$> parseQueryParam v
205− key = T.pack $ selName (Proxy3 :: Proxy3 s g p)
209+ key = Text.pack $ selName (Proxy3 :: Proxy3 s g p)
207instance (GFromForm f) => GFromForm (M1 D x f) where
211instance (GFromForm f) => GFromForm (M1 D x f) where
208 gFromForm f = M1 <$> gFromForm f
212 gFromForm f = M1 <$> gFromForm f
@@ -240,7 +244,7 @@ instance (GFromForm f) => GFromForm (M1 C x f) where
240encodeForm :: Form -> BSL.ByteString
244encodeForm :: Form -> BSL.ByteString
241encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
245encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
243− escape = encodeUtf8 . T.pack . escapeURIString isUnreserved . T.unpack
247+ escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
245 encodePair (k, "") = escape k
249 encodePair (k, "") = escape k
246 encodePair (k, v) = escape k <> "=" <> escape v
250 encodePair (k, v) = escape k <> "=" <> escape v
@@ -277,16 +281,16 @@ encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList
278-- >>> decodeForm "this=has=too=many=equals"
282-- >>> decodeForm "this=has=too=many=equals"
279-- Left "not a valid pair: this=has=too=many=equals"
283-- Left "not a valid pair: this=has=too=many=equals"
280−decodeForm :: BSL.ByteString -> Either T.Text Form
284+decodeForm :: BSL.ByteString -> Either Text Form
281decodeForm bs = toForm <$> traverse parsePair pairs
285decodeForm bs = toForm <$> traverse parsePair pairs
283− pairs = map (decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
287+ pairs = map (Text.decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
285− unescape = T.pack . unEscapeString . T.unpack . T.replace "+" "%20"
289+ unescape = Text.pack . unEscapeString . Text.unpack . Text.replace "+" "%20"
287− parsePair :: T.Text -> Either T.Text (T.Text, T.Text)
291+ parsePair :: Text -> Either Text (Text, Text)
288 parsePair p =
292 parsePair p =
289− case T.splitOn "=" p of
293+ case Text.splitOn "=" p of
290 [k, v] -> return (unescape k, unescape v)
294 [k, v] -> return (unescape k, unescape v)
291 [k] -> return (unescape k, "" )
295 [k] -> return (unescape k, "" )
292 _ -> Left $ "not a valid pair: " <> p
296 _ -> Left $ "not a valid pair: " <> p
@@ -299,9 +303,9 @@ data Proxy3 a b c = Proxy3
300-- This is effectively @'fromForm' '<=<' 'decodeForm'@.
304-- This is effectively @'fromForm' '<=<' 'decodeForm'@.
302−-- >>> decodeAsForm "name=Dennis&age=22" :: Either T.Text Person
306+-- >>> decodeAsForm "name=Dennis&age=22" :: Either Text Person
303-- Right (Person {name = "Dennis", age = 22})
307-- Right (Person {name = "Dennis", age = 22})
304−decodeAsForm :: FromForm a => BSL.ByteString -> Either T.Text a
308+decodeAsForm :: FromForm a => BSL.ByteString -> Either Text a
305decodeAsForm = fromForm <=< decodeForm
309decodeAsForm = fromForm <=< decodeForm
307-- | This is a convenience function for encoding a datatype that has instance
311-- | This is a convenience function for encoding a datatype that has instance