git.iko.soy/typeable/http-api-data
on master help sign in
Refactor qualified names
commitd04cfbaf0d9eede456d3e86c7c6859253d3ed9af authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 29 Aug 2016, 18:28 (10 years ago) parent765249b
1 file changed+31−27
src/Web/Internal/FormUrlEncoded.hs+31−27
src/Web/Internal/FormUrlEncoded.hs+31−27
@@ -19,11 +19,15 @@ import Control.Applicative
1919import Control.Monad ((<=<))
2020import qualified Data.ByteString.Lazy as BSL
2121import qualified Data.ByteString.Lazy.Char8 as BSL8
22−import qualified Data.Map as M
22+import Data.Map (Map)
23+import qualified Data.Map as Map
2324import Data.Monoid
24−import qualified Data.Text as T
25−import Data.Text.Encoding (decodeUtf8With, encodeUtf8)
25+
26+import Data.Text (Text)
27+import qualified Data.Text as Text
28+import Data.Text.Encoding as Text
2629import Data.Text.Encoding.Error (lenientDecode)
30+
2731import GHC.Exts (IsList (..))
2832import GHC.Generics
2933import Network.URI (escapeURIString, isUnreserved,
@@ -47,7 +51,7 @@ import Web.Internal.HttpApiData
4751-- | The contents of a form, not yet URL-encoded.
4852--
4953-- '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 }
5155 deriving (Eq, Read, Generic, Monoid)
5256
5357instance Show Form where
@@ -55,9 +59,9 @@ instance Show Form where
5559 showString "fromList " . shows (toList form)
5660
5761instance IsList Form where
58− type Item Form = (T.Text, T.Text)
59− fromList = Form . M.fromList
60− toList = M.toList . unForm
62+ type Item Form = (Text, Text)
63+ fromList = Form . Map.fromList
64+ toList = Map.toList . unForm
6165
6266-- | Convert a value into 'Form'.
6367--
@@ -101,8 +105,8 @@ class ToForm a where
101105 default toForm :: (Generic a, GToForm (Rep a)) => a -> Form
102106 toForm = genericToForm
103107
104−instance ToForm [(T.Text, T.Text)] where toForm = fromList
105−instance ToForm (M.Map T.Text T.Text) where toForm = Form
108+instance ToForm [(Text, Text)] where toForm = fromList
109+instance ToForm (Map Text Text) where toForm = Form
106110instance ToForm Form where toForm = id
107111
108112-- | A 'Generic'-based implementation of 'toForm'.
@@ -129,7 +133,7 @@ instance (GToForm f) => GToForm (M1 C x f) where
129133instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
130134 gToForm (M1 (K1 c)) = fromList [(key, toQueryParam c)]
131135 where
132− key = T.pack $ selName (Proxy3 :: Proxy3 s g p)
136+ key = Text.pack $ selName (Proxy3 :: Proxy3 s g p)
133137
134138-- | Parse 'Form' into a value.
135139--
@@ -167,23 +171,23 @@ instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
167171-- The default implementation will use 'parseQueryParam' for each field's value.
168172class FromForm a where
169173 -- | Parse 'Form' into a value.
170− fromForm :: Form -> Either T.Text a
174+ fromForm :: Form -> Either Text a
171175 default fromForm :: (Generic a, GFromForm (Rep a))
172− => Form -> Either T.Text a
176+ => Form -> Either Text a
173177 fromForm = genericFromForm
174178
175179instance FromForm Form where fromForm = return
176−instance FromForm [(T.Text, T.Text)] where fromForm = return . toList
177−instance FromForm (M.Map T.Text T.Text) where fromForm = return . unForm
180+instance FromForm [(Text, Text)] where fromForm = return . toList
181+instance FromForm (Map Text Text) where fromForm = return . unForm
178182
179183-- | A 'Generic'-based implementation of 'fromForm'.
180184-- This is used as a default implementation in 'FromForm'.
181185genericFromForm :: (Generic a, GFromForm (Rep a))
182− => Form -> Either T.Text a
186+ => Form -> Either Text a
183187genericFromForm f = to <$> gFromForm f
184188
185189class GFromForm (f :: * -> *) where
186− gFromForm :: Form -> Either T.Text (f x)
190+ gFromForm :: Form -> Either Text (f x)
187191
188192instance (GFromForm f, GFromForm g) => GFromForm (f :*: g) where
189193 gFromForm f = (:*:) <$> gFromForm f <*> gFromForm f
@@ -198,11 +202,11 @@ instance (GFromForm f, GFromForm g) => GFromForm (f :+: g) where
198202
199203instance (Selector s, FromHttpApiData f) => GFromForm (M1 S s (K1 i f)) where
200204 gFromForm f =
201− case M.lookup key (unForm f) of
202− Nothing -> Left $ "Could not find key " <> T.pack (show key)
205+ case Map.lookup key (unForm f) of
206+ Nothing -> Left $ "Could not find key " <> Text.pack (show key)
203207 Just v -> M1 . K1 <$> parseQueryParam v
204208 where
205− key = T.pack $ selName (Proxy3 :: Proxy3 s g p)
209+ key = Text.pack $ selName (Proxy3 :: Proxy3 s g p)
206210
207211instance (GFromForm f) => GFromForm (M1 D x f) where
208212 gFromForm f = M1 <$> gFromForm f
@@ -240,7 +244,7 @@ instance (GFromForm f) => GFromForm (M1 C x f) where
240244encodeForm :: Form -> BSL.ByteString
241245encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
242246 where
243− escape = encodeUtf8 . T.pack . escapeURIString isUnreserved . T.unpack
247+ escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
244248
245249 encodePair (k, "") = escape k
246250 encodePair (k, v) = escape k <> "=" <> escape v
@@ -277,16 +281,16 @@ encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList
277281--
278282-- >>> decodeForm "this=has=too=many=equals"
279283-- 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
281285decodeForm bs = toForm <$> traverse parsePair pairs
282286 where
283− pairs = map (decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
287+ pairs = map (Text.decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
284288
285− unescape = T.pack . unEscapeString . T.unpack . T.replace "+" "%20"
289+ unescape = Text.pack . unEscapeString . Text.unpack . Text.replace "+" "%20"
286290
287− parsePair :: T.Text -> Either T.Text (T.Text, T.Text)
291+ parsePair :: Text -> Either Text (Text, Text)
288292 parsePair p =
289− case T.splitOn "=" p of
293+ case Text.splitOn "=" p of
290294 [k, v] -> return (unescape k, unescape v)
291295 [k] -> return (unescape k, "" )
292296 _ -> Left $ "not a valid pair: " <> p
@@ -299,9 +303,9 @@ data Proxy3 a b c = Proxy3
299303--
300304-- This is effectively @'fromForm' '<=<' 'decodeForm'@.
301305--
302−-- >>> decodeAsForm "name=Dennis&age=22" :: Either T.Text Person
306+-- >>> decodeAsForm "name=Dennis&age=22" :: Either Text Person
303307-- 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
305309decodeAsForm = fromForm <=< decodeForm
306310
307311-- | This is a convenience function for encoding a datatype that has instance