git.iko.soy/typeable/http-api-data
on master help sign in
Change Form's internal representation to HashMap
commit0456604829fe83728be809a8c14b7c9d8ac8d853 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateTue 30 Aug 2016, 15:45 (10 years ago) parent70060ee
4 files changed+18−18
http-api-data.cabal+1−1 src/Web/Internal/FormUrlEncoded.hs+6−6 test/Web/Internal/FormUrlEncodedSpec.hs+9−9 test/Web/Internal/TestInstances.hs+2−2
http-api-data.cabal+1−1
@@ -57,7 +57,7 @@ test-suite spec
5757 , base >= 4 && < 5
5858 , bytestring
5959 , QuickCheck
60− , containers
60+ , unordered-containers
6161 , http-api-data
6262 , text
6363 , time
src/Web/Internal/FormUrlEncoded.hs+6−6
@@ -146,7 +146,7 @@ instance FromFormKey a => FromFormKey (Product a) where parseFormKey = fmap Prod
146146-- | The contents of a form, not yet URL-encoded.
147147--
148148-- 'Form' can be URL-encoded with 'encodeForm' and URL-decoded with 'decodeForm'.
149−newtype Form = Form { unForm :: Map Text [Text] }
149+newtype Form = Form { unForm :: HashMap Text [Text] }
150150 deriving (Eq, Read, Generic, Monoid)
151151
152152instance Show Form where
@@ -155,8 +155,8 @@ instance Show Form where
155155
156156instance IsList Form where
157157 type Item Form = (Text, Text)
158− fromList = Form . Map.fromListWith (flip (<>)) . fmap (\(k, v) -> (k, [v]))
159− toList = concatMap (\(k, vs) -> map ((,) k) vs) . Map.toList . unForm
158+ fromList = Form . HashMap.fromListWith (flip (<>)) . fmap (\(k, v) -> (k, [v]))
159+ toList = concatMap (\(k, vs) -> map ((,) k) vs) . HashMap.toList . unForm
160160
161161-- | Convert a value into 'Form'.
162162--
@@ -216,7 +216,7 @@ instance ToHttpApiData v => ToForm (IntMap [v]) where
216216
217217-- | Convert a list of entries groupped by key into a 'Form'.
218218fromEntriesByKey :: (ToFormKey k, ToHttpApiData v) => [(k, [v])] -> Form
219−fromEntriesByKey = Form . Map.fromListWith (<>) . map (toFormKey *** map toQueryParam)
219+fromEntriesByKey = Form . HashMap.fromListWith (<>) . map (toFormKey *** map toQueryParam)
220220
221221-- | A 'Generic'-based implementation of 'toForm'.
222222-- This is used as a default implementation in 'ToForm'.
@@ -301,7 +301,7 @@ instance FromHttpApiData v => FromForm (IntMap [v]) where
301301
302302-- | Parse a 'Form' into a list of entries groupped by key.
303303toEntriesByKey :: (FromFormKey k, FromHttpApiData v) => Form -> Either Text [(k, [v])]
304−toEntriesByKey = traverse parseGroup . Map.toList . unForm
304+toEntriesByKey = traverse parseGroup . HashMap.toList . unForm
305305 where
306306 parseGroup (k, vs) = (,) <$> parseFormKey k <*> traverse parseQueryParam vs
307307
@@ -450,7 +450,7 @@ encodeAsForm = encodeForm . toForm
450450-- >>> lookupKey "name" [("name", "Oleg"), ("name", "David")]
451451-- ["Oleg","David"]
452452lookupKey :: Text -> Form -> [Text]
453−lookupKey key = concat . Map.lookup key . unForm
453+lookupKey key = concat . HashMap.lookup key . unForm
454454
455455-- | Lookup a unique value for a key.
456456-- Fail if there is zero or more than one value.
test/Web/Internal/FormUrlEncodedSpec.hs+9−9
@@ -9,7 +9,7 @@ import Control.Applicative
99
1010import Control.Monad ((<=<))
1111import qualified Data.ByteString.Lazy.Char8 as BSL
12−import qualified Data.Map as M
12+import qualified Data.HashMap.Strict as HashMap
1313import Data.Text (Text, unpack)
1414import Test.Hspec
1515import Test.QuickCheck
@@ -32,13 +32,13 @@ genericSpec = describe "Default (generic) instances" $ do
3232
3333 it "contains the record names" $ property $ \(x :: SimpleRec) -> do
3434 let f = unForm $ toForm x
35− M.member "rec1" f `shouldBe` True
36− M.member "rec2" f `shouldBe` True
35+ HashMap.member "rec1" f `shouldBe` True
36+ HashMap.member "rec2" f `shouldBe` True
3737
3838 it "contains the correct record values" $ property $ \(x :: SimpleRec) -> do
3939 let f = unForm $ toForm x
40− M.lookup "rec1" f `shouldBe` Just [rec1 x]
41− (parseQueryParams <$> M.lookup "rec2" f) `shouldBe` Just (Right [rec2 x])
40+ HashMap.lookup "rec1" f `shouldBe` Just [rec1 x]
41+ (parseQueryParams <$> HashMap.lookup "rec2" f) `shouldBe` Just (Right [rec2 x])
4242
4343 context "for sum types" $ do
4444
@@ -46,11 +46,11 @@ genericSpec = describe "Default (generic) instances" $ do
4646 let f = unForm $ toForm x
4747 case x of
4848 SSRLeft _ _ -> do
49− parseQueryParams <$> M.lookup "left1" f `shouldBe` Just (Right [left1 x])
50− parseQueryParams <$> M.lookup "left2" f `shouldBe` Just (Right [left2 x])
49+ parseQueryParams <$> HashMap.lookup "left1" f `shouldBe` Just (Right [left1 x])
50+ parseQueryParams <$> HashMap.lookup "left2" f `shouldBe` Just (Right [left2 x])
5151 SSRRight _ _ -> do
52− parseQueryParams <$> M.lookup "right1" f `shouldBe` Just (Right [right1 x])
53− parseQueryParams <$> M.lookup "right2" f `shouldBe` Just (Right [right2 x])
52+ parseQueryParams <$> HashMap.lookup "right1" f `shouldBe` Just (Right [right1 x])
53+ parseQueryParams <$> HashMap.lookup "right2" f `shouldBe` Just (Right [right2 x])
5454
5555
5656 context "FromForm" $ do
test/Web/Internal/TestInstances.hs+2−2
@@ -12,7 +12,7 @@ module Web.Internal.TestInstances
1212import Control.Applicative
1313import qualified Data.ByteString.Lazy.Char8 as BSL
1414import Data.Char
15−import qualified Data.Map as M
15+import qualified Data.HashMap.Strict as HashMap
1616import qualified Data.Text as T
1717import qualified Data.Text.Lazy as L
1818import Data.Time
@@ -114,4 +114,4 @@ newtype NoEmptyKeyForm =
114114instance Arbitrary NoEmptyKeyForm where
115115 arbitrary = NoEmptyKeyForm . removeEmptyKeys <$> arbitrary
116116 where
117− removeEmptyKeys (Form m) = Form (M.delete "" m)
117+ removeEmptyKeys (Form m) = Form (HashMap.delete "" m)