git.iko.soy/typeable/http-api-data
on master help sign in
Change Form's internal representation to HashMap
0456604Nickolay Kudasov
http-api-data.cabal+1−1
@@ -57,7 +57,7 @@ test-suite spec
57 , base >= 4 && < 5
57 , base >= 4 && < 5
58 , bytestring
58 , bytestring
59 , QuickCheck
59 , QuickCheck
60− , containers
60+ , unordered-containers
61 , http-api-data
61 , http-api-data
62 , text
62 , text
63 , time
63 , time
src/Web/Internal/FormUrlEncoded.hs+6−6
@@ -146,7 +146,7 @@ instance FromFormKey a => FromFormKey (Product a) where parseFormKey = fmap Prod
146-- | The contents of a form, not yet URL-encoded.
146-- | The contents of a form, not yet URL-encoded.
147--
147--
148-- 'Form' can be URL-encoded with 'encodeForm' and URL-decoded with 'decodeForm'.
148-- '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] }
150 deriving (Eq, Read, Generic, Monoid)
150 deriving (Eq, Read, Generic, Monoid)
151
151
152instance Show Form where
152instance Show Form where
@@ -155,8 +155,8 @@ instance Show Form where
155
155
156instance IsList Form where
156instance IsList Form where
157 type Item Form = (Text, Text)
157 type Item Form = (Text, Text)
158− fromList = Form . Map.fromListWith (flip (<>)) . fmap (\(k, v) -> (k, [v]))
158+ fromList = Form . HashMap.fromListWith (flip (<>)) . fmap (\(k, v) -> (k, [v]))
159− toList = concatMap (\(k, vs) -> map ((,) k) vs) . Map.toList . unForm
159+ toList = concatMap (\(k, vs) -> map ((,) k) vs) . HashMap.toList . unForm
160
160
161-- | Convert a value into 'Form'.
161-- | Convert a value into 'Form'.
162--
162--
@@ -216,7 +216,7 @@ instance ToHttpApiData v => ToForm (IntMap [v]) where
216
216
217-- | Convert a list of entries groupped by key into a 'Form'.
217-- | Convert a list of entries groupped by key into a 'Form'.
218fromEntriesByKey :: (ToFormKey k, ToHttpApiData v) => [(k, [v])] -> Form
218fromEntriesByKey :: (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)
220
220
221-- | A 'Generic'-based implementation of 'toForm'.
221-- | A 'Generic'-based implementation of 'toForm'.
222-- This is used as a default implementation in 'ToForm'.
222-- This is used as a default implementation in 'ToForm'.
@@ -301,7 +301,7 @@ instance FromHttpApiData v => FromForm (IntMap [v]) where
301
301
302-- | Parse a 'Form' into a list of entries groupped by key.
302-- | Parse a 'Form' into a list of entries groupped by key.
303toEntriesByKey :: (FromFormKey k, FromHttpApiData v) => Form -> Either Text [(k, [v])]
303toEntriesByKey :: (FromFormKey k, FromHttpApiData v) => Form -> Either Text [(k, [v])]
304−toEntriesByKey = traverse parseGroup . Map.toList . unForm
304+toEntriesByKey = traverse parseGroup . HashMap.toList . unForm
305 where
305 where
306 parseGroup (k, vs) = (,) <$> parseFormKey k <*> traverse parseQueryParam vs
306 parseGroup (k, vs) = (,) <$> parseFormKey k <*> traverse parseQueryParam vs
307
307
@@ -450,7 +450,7 @@ encodeAsForm = encodeForm . toForm
450-- >>> lookupKey "name" [("name", "Oleg"), ("name", "David")]
450-- >>> lookupKey "name" [("name", "Oleg"), ("name", "David")]
451-- ["Oleg","David"]
451-- ["Oleg","David"]
452lookupKey :: Text -> Form -> [Text]
452lookupKey :: Text -> Form -> [Text]
453−lookupKey key = concat . Map.lookup key . unForm
453+lookupKey key = concat . HashMap.lookup key . unForm
454
454
455-- | Lookup a unique value for a key.
455-- | Lookup a unique value for a key.
456-- Fail if there is zero or more than one value.
456-- Fail if there is zero or more than one value.
test/Web/Internal/FormUrlEncodedSpec.hs+9−9
@@ -9,7 +9,7 @@ import Control.Applicative
9
9
10import Control.Monad ((<=<))
10import Control.Monad ((<=<))
11import qualified Data.ByteString.Lazy.Char8 as BSL
11import qualified Data.ByteString.Lazy.Char8 as BSL
12−import qualified Data.Map as M
12+import qualified Data.HashMap.Strict as HashMap
13import Data.Text (Text, unpack)
13import Data.Text (Text, unpack)
14import Test.Hspec
14import Test.Hspec
15import Test.QuickCheck
15import Test.QuickCheck
@@ -32,13 +32,13 @@ genericSpec = describe "Default (generic) instances" $ do
32
32
33 it "contains the record names" $ property $ \(x :: SimpleRec) -> do
33 it "contains the record names" $ property $ \(x :: SimpleRec) -> do
34 let f = unForm $ toForm x
34 let f = unForm $ toForm x
35− M.member "rec1" f `shouldBe` True
35+ HashMap.member "rec1" f `shouldBe` True
36− M.member "rec2" f `shouldBe` True
36+ HashMap.member "rec2" f `shouldBe` True
37
37
38 it "contains the correct record values" $ property $ \(x :: SimpleRec) -> do
38 it "contains the correct record values" $ property $ \(x :: SimpleRec) -> do
39 let f = unForm $ toForm x
39 let f = unForm $ toForm x
40− M.lookup "rec1" f `shouldBe` Just [rec1 x]
40+ HashMap.lookup "rec1" f `shouldBe` Just [rec1 x]
41− (parseQueryParams <$> M.lookup "rec2" f) `shouldBe` Just (Right [rec2 x])
41+ (parseQueryParams <$> HashMap.lookup "rec2" f) `shouldBe` Just (Right [rec2 x])
42
42
43 context "for sum types" $ do
43 context "for sum types" $ do
44
44
@@ -46,11 +46,11 @@ genericSpec = describe "Default (generic) instances" $ do
46 let f = unForm $ toForm x
46 let f = unForm $ toForm x
47 case x of
47 case x of
48 SSRLeft _ _ -> do
48 SSRLeft _ _ -> do
49− parseQueryParams <$> M.lookup "left1" f `shouldBe` Just (Right [left1 x])
49+ parseQueryParams <$> HashMap.lookup "left1" f `shouldBe` Just (Right [left1 x])
50− parseQueryParams <$> M.lookup "left2" f `shouldBe` Just (Right [left2 x])
50+ parseQueryParams <$> HashMap.lookup "left2" f `shouldBe` Just (Right [left2 x])
51 SSRRight _ _ -> do
51 SSRRight _ _ -> do
52− parseQueryParams <$> M.lookup "right1" f `shouldBe` Just (Right [right1 x])
52+ parseQueryParams <$> HashMap.lookup "right1" f `shouldBe` Just (Right [right1 x])
53− parseQueryParams <$> M.lookup "right2" f `shouldBe` Just (Right [right2 x])
53+ parseQueryParams <$> HashMap.lookup "right2" f `shouldBe` Just (Right [right2 x])
54
54
55
55
56 context "FromForm" $ do
56 context "FromForm" $ do
test/Web/Internal/TestInstances.hs+2−2
@@ -12,7 +12,7 @@ module Web.Internal.TestInstances
12import Control.Applicative
12import Control.Applicative
13import qualified Data.ByteString.Lazy.Char8 as BSL
13import qualified Data.ByteString.Lazy.Char8 as BSL
14import Data.Char
14import Data.Char
15−import qualified Data.Map as M
15+import qualified Data.HashMap.Strict as HashMap
16import qualified Data.Text as T
16import qualified Data.Text as T
17import qualified Data.Text.Lazy as L
17import qualified Data.Text.Lazy as L
18import Data.Time
18import Data.Time
@@ -114,4 +114,4 @@ newtype NoEmptyKeyForm =
114instance Arbitrary NoEmptyKeyForm where
114instance Arbitrary NoEmptyKeyForm where
115 arbitrary = NoEmptyKeyForm . removeEmptyKeys <$> arbitrary
115 arbitrary = NoEmptyKeyForm . removeEmptyKeys <$> arbitrary
116 where
116 where
117− removeEmptyKeys (Form m) = Form (M.delete "" m)
117+ removeEmptyKeys (Form m) = Form (HashMap.delete "" m)