git.iko.soy/typeable/http-api-data
on master help sign in
Rename encode/decode to urlEncode/urlDecode
commit02fadd3940033f0bea3775344d6a923f3bf53a62 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateTue 30 Aug 2016, 16:46 (10 years ago) parent173ef33
3 files changed+37−37
src/Web/FormUrlEncoded.hs+4−4 src/Web/Internal/FormUrlEncoded.hs+25−25 test/Web/Internal/FormUrlEncodedSpec.hs+8−8
src/Web/FormUrlEncoded.hs+4−4
@@ -10,11 +10,11 @@ module Web.FormUrlEncoded (
1010 FromFormKey(..),
1111
1212 -- * Encoding and decoding @'Form'@s
13− encodeAsForm,
14− decodeAsForm,
13+ urlEncodeAsForm,
14+ urlDecodeAsForm,
1515
16− encodeForm,
17− decodeForm,
16+ urlEncodeForm,
17+ urlDecodeForm,
1818
1919 -- * 'Generic's
2020 genericToForm,
src/Web/Internal/FormUrlEncoded.hs+25−25
@@ -147,7 +147,7 @@ instance FromFormKey a => FromFormKey (Product a) where parseFormKey = fmap Prod
147147
148148-- | The contents of a form, not yet URL-encoded.
149149--
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'.
151151newtype Form = Form { unForm :: HashMap Text [Text] }
152152 deriving (Eq, Read, Generic, Monoid)
153153
@@ -342,31 +342,31 @@ instance (GFromForm f) => GFromForm (M1 C x f) where
342342--
343343-- Key-value pairs get encoded to @key=value@ and separated by @&@:
344344--
345−-- >>> encodeForm [("name", "Julian"), ("lastname", "Arni")]
345+-- >>> urlEncodeForm [("name", "Julian"), ("lastname", "Arni")]
346346-- "lastname=Arni&name=Julian"
347347--
348348-- Keys with empty values get encoded to just @key@ (without the @=@ sign):
349349--
350−-- >>> encodeForm [("is_test", "")]
350+-- >>> urlEncodeForm [("is_test", "")]
351351-- "is_test"
352352--
353353-- Empty keys are allowed too:
354354--
355−-- >>> encodeForm [("", "foobar")]
355+-- >>> urlEncodeForm [("", "foobar")]
356356-- "=foobar"
357357--
358358-- 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).
360360--
361−-- >>> encodeForm [("", "")]
361+-- >>> urlEncodeForm [("", "")]
362362-- ""
363363--
364364-- Everything is escaped with @'escapeURIString' 'isUnreserved'@:
365365--
366−-- >>> encodeForm [("fullname", "Andres Löh")]
366+-- >>> urlEncodeForm [("fullname", "Andres Löh")]
367367-- "fullname=Andres%20L%C3%B6h"
368−encodeForm :: Form -> BSL.ByteString
369−encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
368+urlEncodeForm :: Form -> BSL.ByteString
369+urlEncodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList xs
370370 where
371371 escape = Text.encodeUtf8 . Text.pack . escapeURIString isUnreserved . Text.unpack
372372
@@ -378,35 +378,35 @@ encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList
378378--
379379-- Key-value pairs get decoded normally:
380380--
381−-- >>> decodeForm "name=Greg&lastname=Weber"
381+-- >>> urlDecodeForm "name=Greg&lastname=Weber"
382382-- Right (fromList [("lastname","Weber"),("name","Greg")])
383383--
384384-- Keys with no values get decoded to pairs with empty values.
385385--
386−-- >>> decodeForm "is_test"
386+-- >>> urlDecodeForm "is_test"
387387-- Right (fromList [("is_test","")])
388388--
389389-- Empty keys are allowed:
390390--
391−-- >>> decodeForm "=foobar"
391+-- >>> urlDecodeForm "=foobar"
392392-- Right (fromList [("","foobar")])
393393--
394394-- The empty string gets decoded into an empty 'Form':
395395--
396−-- >>> decodeForm ""
396+-- >>> urlDecodeForm ""
397397-- Right (fromList [])
398398--
399399-- Everything is un-escaped with 'unEscapeString':
400400--
401−-- >>> decodeForm "fullname=Andres%20L%C3%B6h"
401+-- >>> urlDecodeForm "fullname=Andres%20L%C3%B6h"
402402-- Right (fromList [("fullname","Andres L\246h")])
403403--
404404-- Improperly formed strings result in an error:
405405--
406−-- >>> decodeForm "this=has=too=many=equals"
406+-- >>> urlDecodeForm "this=has=too=many=equals"
407407-- Left "not a valid pair: this=has=too=many=equals"
408−decodeForm :: BSL.ByteString -> Either Text Form
409−decodeForm bs = toForm <$> traverse parsePair pairs
408+urlDecodeForm :: BSL.ByteString -> Either Text Form
409+urlDecodeForm bs = toForm <$> traverse parsePair pairs
410410 where
411411 pairs = map (Text.decodeUtf8With lenientDecode . BSL.toStrict) (BSL8.split '&' bs)
412412
@@ -425,23 +425,23 @@ data Proxy3 a b c = Proxy3
425425-- @application/x-www-form-urlencoded@ 'BSL.ByteString' directly to a datatype
426426-- that has an instance of 'FromForm'.
427427--
428−-- This is effectively @'fromForm' '<=<' 'decodeForm'@.
428+-- This is effectively @'fromForm' '<=<' 'urlDecodeForm'@.
429429--
430−-- >>> decodeAsForm "name=Dennis&age=22" :: Either Text Person
430+-- >>> urlDecodeAsForm "name=Dennis&age=22" :: Either Text Person
431431-- Right (Person {name = "Dennis", age = 22})
432−decodeAsForm :: FromForm a => BSL.ByteString -> Either Text a
433−decodeAsForm = fromForm <=< decodeForm
432+urlDecodeAsForm :: FromForm a => BSL.ByteString -> Either Text a
433+urlDecodeAsForm = fromForm <=< urlDecodeForm
434434
435435-- | This is a convenience function for encoding a datatype that has instance
436436-- of 'ToForm' directly to a @application/x-www-form-urlencoded@
437437-- 'BSL.ByteString'.
438438--
439−-- This is effectively @'encodeForm' . 'toForm'@.
439+-- This is effectively @'urlEncodeForm' . 'toForm'@.
440440--
441−-- >>> encodeAsForm Person {name = "Dennis", age = 22}
441+-- >>> urlEncodeAsForm Person {name = "Dennis", age = 22}
442442-- "age=22&name=Dennis"
443−encodeAsForm :: ToForm a => a -> BSL.ByteString
444−encodeAsForm = encodeForm . toForm
443+urlEncodeAsForm :: ToForm a => a -> BSL.ByteString
444+urlEncodeAsForm = urlEncodeForm . toForm
445445
446446-- | Find all values corresponding to a given key in a 'Form'.
447447--
test/Web/Internal/FormUrlEncodedSpec.hs+8−8
@@ -77,14 +77,14 @@ genericSpec = describe "Default (generic) instances" $ do
7777urlEncoding :: Spec
7878urlEncoding = describe "urlEncoding" $ do
7979
80− it "decodeForm (encodeForm x) == Right x" $ property $ \(NoEmptyKeyForm x) -> do
81− decodeForm (encodeForm x) `shouldBe` Right x
80+ it "urlDecodeForm (urlEncodeForm x) == Right x" $ property $ \(NoEmptyKeyForm x) -> do
81+ urlDecodeForm (urlEncodeForm x) `shouldBe` Right x
8282
83− it "decodeAsForm == (fromForm <=< decodeForm)" $ property $ \(x :: BSL.ByteString) -> do
84− (decodeAsForm x :: Either Text Form) `shouldBe` (fromForm <=< decodeForm) x
83+ it "urlDecodeAsForm == (fromForm <=< urlDecodeForm)" $ property $ \(x :: BSL.ByteString) -> do
84+ (urlDecodeAsForm x :: Either Text Form) `shouldBe` (fromForm <=< urlDecodeForm) x
8585
86− it "encodeAsForm == encodeForm . toForm" $ property $ \(x :: Form) -> do
87− encodeAsForm x `shouldBe` (encodeForm . toForm) x
86+ it "urlEncodeAsForm == urlEncodeForm . toForm" $ property $ \(x :: Form) -> do
87+ urlEncodeAsForm x `shouldBe` (urlEncodeForm . toForm) x
8888
89− it "decodeForm \"\" == Right mempty" $ do
90− decodeForm "" `shouldBe` Right mempty
89+ it "urlDecodeForm \"\" == Right mempty" $ do
90+ urlDecodeForm "" `shouldBe` Right mempty