git.iko.soy/typeable/http-api-data
on master help sign in
Rename encode/decode to urlEncode/urlDecode
02fadd3Nickolay Kudasov
src/Web/FormUrlEncoded.hs+4−4
@@ -10,11 +10,11 @@ module Web.FormUrlEncoded (
10 FromFormKey(..),
10 FromFormKey(..),
11
11
12 -- * Encoding and decoding @'Form'@s
12 -- * Encoding and decoding @'Form'@s
13− encodeAsForm,
13+ urlEncodeAsForm,
14− decodeAsForm,
14+ urlDecodeAsForm,
15
15
16− encodeForm,
16+ urlEncodeForm,
17− decodeForm,
17+ urlDecodeForm,
18
18
19 -- * 'Generic's
19 -- * 'Generic's
20 genericToForm,
20 genericToForm,
src/Web/Internal/FormUrlEncoded.hs+25−25
@@ -147,7 +147,7 @@ instance FromFormKey a => FromFormKey (Product a) where parseFormKey = fmap Prod
147
147
148-- | The contents of a form, not yet URL-encoded.
148-- | The contents of a form, not yet URL-encoded.
149--
149--
150−-- 'Form' can be URL-encoded with 'encodeForm' and URL-decoded with 'decodeForm'.
150+-- 'Form' can be URL-encoded with 'urlEncodeForm' and URL-decoded with 'urlDecodeForm'.
151newtype Form = Form { unForm :: HashMap Text [Text] }
151newtype Form = Form { unForm :: HashMap Text [Text] }
152 deriving (Eq, Read, Generic, Monoid)
152 deriving (Eq, Read, Generic, Monoid)
153
153
@@ -342,31 +342,31 @@ instance (GFromForm f) => GFromForm (M1 C x f) where
342--
342--
343-- Key-value pairs get encoded to @key=value@ and separated by @&@:
343-- Key-value pairs get encoded to @key=value@ and separated by @&@:
344--
344--
345−-- >>> encodeForm [("name", "Julian"), ("lastname", "Arni")]
345+-- >>> urlEncodeForm [("name", "Julian"), ("lastname", "Arni")]
346-- "lastname=Arni&name=Julian"
346-- "lastname=Arni&name=Julian"
347--
347--
348-- Keys with empty values get encoded to just @key@ (without the @=@ sign):
348-- Keys with empty values get encoded to just @key@ (without the @=@ sign):
349--
349--
350−-- >>> encodeForm [("is_test", "")]
350+-- >>> urlEncodeForm [("is_test", "")]
351-- "is_test"
351-- "is_test"
352--
352--
353-- Empty keys are allowed too:
353-- Empty keys are allowed too:
354--
354--
355−-- >>> encodeForm [("", "foobar")]
355+-- >>> urlEncodeForm [("", "foobar")]
356-- "=foobar"
356-- "=foobar"
357--
357--
358-- However, if not key and value are empty, the key-value pair is ignored.
358-- However, if not key and value are empty, the key-value pair is ignored.
359−-- (This prevents @'decodeForm' . 'encodeForm'@ from being a true isomorphism).
359+-- (This prevents @'urlDecodeForm' . 'urlEncodeForm'@ from being a true isomorphism).
360--
360--
361−-- >>> encodeForm [("", "")]
361+-- >>> urlEncodeForm [("", "")]
362-- ""
362-- ""
363--
363--
364-- Everything is escaped with @'escapeURIString' 'isUnreserved'@:
364-- Everything is escaped with @'escapeURIString' 'isUnreserved'@:
365--
365--
366−-- >>> encodeForm [("fullname", "Andres Löh")]
366+-- >>> urlEncodeForm [("fullname", "Andres Löh")]
367-- "fullname=Andres%20L%C3%B6h"
367-- "fullname=Andres%20L%C3%B6h"
368−encodeForm :: Form -> BSL.ByteString
368+urlEncodeForm :: Form -> BSL.ByteString
369−encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
369+urlEncodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
370 where
370 where
371 escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
371 escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
372
372
@@ -378,35 +378,35 @@ encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList
378--
378--
379-- Key-value pairs get decoded normally:
379-- Key-value pairs get decoded normally:
380--
380--
381−-- >>> decodeForm "name=Greg&lastname=Weber"
381+-- >>> urlDecodeForm "name=Greg&lastname=Weber"
382-- Right (fromList [("lastname","Weber"),("name","Greg")])
382-- Right (fromList [("lastname","Weber"),("name","Greg")])
383--
383--
384-- Keys with no values get decoded to pairs with empty values.
384-- Keys with no values get decoded to pairs with empty values.
385--
385--
386−-- >>> decodeForm "is_test"
386+-- >>> urlDecodeForm "is_test"
387-- Right (fromList [("is_test","")])
387-- Right (fromList [("is_test","")])
388--
388--
389-- Empty keys are allowed:
389-- Empty keys are allowed:
390--
390--
391−-- >>> decodeForm "=foobar"
391+-- >>> urlDecodeForm "=foobar"
392-- Right (fromList [("","foobar")])
392-- Right (fromList [("","foobar")])
393--
393--
394-- The empty string gets decoded into an empty 'Form':
394-- The empty string gets decoded into an empty 'Form':
395--
395--
396−-- >>> decodeForm ""
396+-- >>> urlDecodeForm ""
397-- Right (fromList [])
397-- Right (fromList [])
398--
398--
399-- Everything is un-escaped with 'unEscapeString':
399-- Everything is un-escaped with 'unEscapeString':
400--
400--
401−-- >>> decodeForm "fullname=Andres%20L%C3%B6h"
401+-- >>> urlDecodeForm "fullname=Andres%20L%C3%B6h"
402-- Right (fromList [("fullname","Andres L\246h")])
402-- Right (fromList [("fullname","Andres L\246h")])
403--
403--
404-- Improperly formed strings result in an error:
404-- Improperly formed strings result in an error:
405--
405--
406−-- >>> decodeForm "this=has=too=many=equals"
406+-- >>> urlDecodeForm "this=has=too=many=equals"
407-- Left "not a valid pair: this=has=too=many=equals"
407-- Left "not a valid pair: this=has=too=many=equals"
408−decodeForm :: BSL.ByteString -> Either Text Form
408+urlDecodeForm :: BSL.ByteString -> Either Text Form
409−decodeForm bs = toForm <$> traverse parsePair pairs
409+urlDecodeForm bs = toForm <$> traverse parsePair pairs
410 where
410 where
411 pairs = map (Text.decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
411 pairs = map (Text.decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
412
412
@@ -425,23 +425,23 @@ data Proxy3 a b c = Proxy3
425-- @application/x-www-form-urlencoded@ 'BSL.ByteString' directly to a datatype
425-- @application/x-www-form-urlencoded@ 'BSL.ByteString' directly to a datatype
426-- that has an instance of 'FromForm'.
426-- that has an instance of 'FromForm'.
427--
427--
428−-- This is effectively @'fromForm' '<=<' 'decodeForm'@.
428+-- This is effectively @'fromForm' '<=<' 'urlDecodeForm'@.
429--
429--
430−-- >>> decodeAsForm "name=Dennis&age=22" :: Either Text Person
430+-- >>> urlDecodeAsForm "name=Dennis&age=22" :: Either Text Person
431-- Right (Person {name = "Dennis", age = 22})
431-- Right (Person {name = "Dennis", age = 22})
432−decodeAsForm :: FromForm a => BSL.ByteString -> Either Text a
432+urlDecodeAsForm :: FromForm a => BSL.ByteString -> Either Text a
433−decodeAsForm = fromForm <=< decodeForm
433+urlDecodeAsForm = fromForm <=< urlDecodeForm
434
434
435-- | This is a convenience function for encoding a datatype that has instance
435-- | This is a convenience function for encoding a datatype that has instance
436-- of 'ToForm' directly to a @application/x-www-form-urlencoded@
436-- of 'ToForm' directly to a @application/x-www-form-urlencoded@
437-- 'BSL.ByteString'.
437-- 'BSL.ByteString'.
438--
438--
439−-- This is effectively @'encodeForm' . 'toForm'@.
439+-- This is effectively @'urlEncodeForm' . 'toForm'@.
440--
440--
441−-- >>> encodeAsForm Person {name = "Dennis", age = 22}
441+-- >>> urlEncodeAsForm Person {name = "Dennis", age = 22}
442-- "age=22&name=Dennis"
442-- "age=22&name=Dennis"
443−encodeAsForm :: ToForm a => a -> BSL.ByteString
443+urlEncodeAsForm :: ToForm a => a -> BSL.ByteString
444−encodeAsForm = encodeForm . toForm
444+urlEncodeAsForm = urlEncodeForm . toForm
445
445
446-- | Find all values corresponding to a given key in a 'Form'.
446-- | Find all values corresponding to a given key in a 'Form'.
447--
447--
test/Web/Internal/FormUrlEncodedSpec.hs+8−8
@@ -77,14 +77,14 @@ genericSpec = describe "Default (generic) instances" $ do
77urlEncoding :: Spec
77urlEncoding :: Spec
78urlEncoding = describe "urlEncoding" $ do
78urlEncoding = describe "urlEncoding" $ do
79
79
80− it "decodeForm (encodeForm x) == Right x" $ property $ \(NoEmptyKeyForm x) -> do
80+ it "urlDecodeForm (urlEncodeForm x) == Right x" $ property $ \(NoEmptyKeyForm x) -> do
81− decodeForm (encodeForm x) `shouldBe` Right x
81+ urlDecodeForm (urlEncodeForm x) `shouldBe` Right x
82
82
83− it "decodeAsForm == (fromForm <=< decodeForm)" $ property $ \(x :: BSL.ByteString) -> do
83+ it "urlDecodeAsForm == (fromForm <=< urlDecodeForm)" $ property $ \(x :: BSL.ByteString) -> do
84− (decodeAsForm x :: Either Text Form) `shouldBe` (fromForm <=< decodeForm) x
84+ (urlDecodeAsForm x :: Either Text Form) `shouldBe` (fromForm <=< urlDecodeForm) x
85
85
86− it "encodeAsForm == encodeForm . toForm" $ property $ \(x :: Form) -> do
86+ it "urlEncodeAsForm == urlEncodeForm . toForm" $ property $ \(x :: Form) -> do
87− encodeAsForm x `shouldBe` (encodeForm . toForm) x
87+ urlEncodeAsForm x `shouldBe` (urlEncodeForm . toForm) x
88
88
89− it "decodeForm \"\" == Right mempty" $ do
89+ it "urlDecodeForm \"\" == Right mempty" $ do
90− decodeForm "" `shouldBe` Right mempty
90+ urlDecodeForm "" `shouldBe` Right mempty