git.iko.soy/typeable/http-api-data
on master help sign in
Change uses of String to Text.
dd1baa5(cdep)illabout
src/Web/HttpApiData/Internal/FormUrlEncoded.hs+10−10
@@ -16,7 +16,7 @@ module Web.HttpApiData.Internal.FormUrlEncoded where
16import Control.Applicative
16import Control.Applicative
17#endif
17#endif
18
18
19−import Control.Arrow (first, left, second)
19+import Control.Arrow (first, second)
20import Control.Monad.State
20import Control.Monad.State
21import qualified Data.ByteString.Lazy as BSL
21import qualified Data.ByteString.Lazy as BSL
22import qualified Data.ByteString.Lazy.UTF8 as BSLUTF8
22import qualified Data.ByteString.Lazy.UTF8 as BSLUTF8
@@ -95,9 +95,9 @@ instance (ToHttpApiData f) => GToForm (K1 i f) where
95-- | A type that can be converted from @application/x-www-form-urlencoded@,
95-- | A type that can be converted from @application/x-www-form-urlencoded@,
96-- with the possibility of failure.
96-- with the possibility of failure.
97class FromForm a where
97class FromForm a where
98− fromForm :: Form -> Either String a
98+ fromForm :: Form -> Either T.Text a
99 default fromForm :: (Generic a, GFromForm (Rep a))
99 default fromForm :: (Generic a, GFromForm (Rep a))
100− => Form -> Either String a
100+ => Form -> Either T.Text a
101 fromForm = genericFromForm
101 fromForm = genericFromForm
102
102
103instance FromForm Form where fromForm = return
103instance FromForm Form where fromForm = return
@@ -105,11 +105,11 @@ instance FromForm [(T.Text, T.Text)] where fromForm = return . M.toList . unForm
105instance FromForm (M.Map T.Text T.Text) where fromForm = return . unForm
105instance FromForm (M.Map T.Text T.Text) where fromForm = return . unForm
106
106
107genericFromForm :: (Generic a, GFromForm (Rep a))
107genericFromForm :: (Generic a, GFromForm (Rep a))
108− => Form -> Either String a
108+ => Form -> Either T.Text a
109genericFromForm f = to <$> gFromForm f
109genericFromForm f = to <$> gFromForm f
110
110
111class GFromForm (f :: * -> *) where
111class GFromForm (f :: * -> *) where
112− gFromForm :: Form -> Either String (f x)
112+ gFromForm :: Form -> Either T.Text (f x)
113
113
114instance (GFromForm f, GFromForm g)
114instance (GFromForm f, GFromForm g)
115 => GFromForm (f :*: g) where
115 => GFromForm (f :*: g) where
@@ -126,8 +126,8 @@ instance (GFromForm f, GFromForm g)
126instance (Selector sel, FromHttpApiData f)
126instance (Selector sel, FromHttpApiData f)
127 => GFromForm (M1 S sel (K1 i f)) where
127 => GFromForm (M1 S sel (K1 i f)) where
128 gFromForm f = case M.lookup sel (unForm f) of
128 gFromForm f = case M.lookup sel (unForm f) of
129− Nothing -> Left . T.unpack $ "Could not find key " <> sel
129+ Nothing -> Left $ "Could not find key " <> sel
130− Just v -> left T.unpack $ M1 . K1 <$> parseQueryParam v
130+ Just v -> M1 . K1 <$> parseQueryParam v
131 where sel = T.pack $ selName (Proxy3 :: Proxy3 sel g p)
131 where sel = T.pack $ selName (Proxy3 :: Proxy3 sel g p)
132
132
133instance (GFromForm f) => GFromForm (M1 D x f) where
133instance (GFromForm f) => GFromForm (M1 D x f) where
@@ -145,19 +145,19 @@ encodeForm xs =
145 encodePair (k, v) = escape k <> "=" <> escape v
145 encodePair (k, v) = escape k <> "=" <> escape v
146 in BSL.intercalate "&" $ map encodePair $ toList xs
146 in BSL.intercalate "&" $ map encodePair $ toList xs
147
147
148−decodeForm :: BSL.ByteString -> Either String Form
148+decodeForm :: BSL.ByteString -> Either T.Text Form
149decodeForm "" = return mempty
149decodeForm "" = return mempty
150decodeForm q = do
150decodeForm q = do
151 let xs :: [T.Text]
151 let xs :: [T.Text]
152 xs = T.splitOn "&" . decodeUtf8With lenientDecode . mconcat . BSL.toChunks $ q
152 xs = T.splitOn "&" . decodeUtf8With lenientDecode . mconcat . BSL.toChunks $ q
153− parsePair :: T.Text -> Either String (T.Text, T.Text)
153+ parsePair :: T.Text -> Either T.Text (T.Text, T.Text)
154 parsePair p =
154 parsePair p =
155 case T.splitOn "=" p of
155 case T.splitOn "=" p of
156 [k,v] -> return ( unescape k
156 [k,v] -> return ( unescape k
157 , unescape v
157 , unescape v
158 )
158 )
159 [k] -> return ( unescape k, "" )
159 [k] -> return ( unescape k, "" )
160− _ -> Left $ "not a valid pair: " <> T.unpack p
160+ _ -> Left $ "not a valid pair: " <> p
161 unescape :: T.Text -> T.Text
161 unescape :: T.Text -> T.Text
162 unescape = T.pack . unEscapeString . T.unpack . T.intercalate "%20" . T.splitOn "+"
162 unescape = T.pack . unEscapeString . T.unpack . T.intercalate "%20" . T.splitOn "+"
163 toForm <$> mapM parsePair xs
163 toForm <$> mapM parsePair xs
test/Web/HttpApiData/Internal/FormUrlEncodedSpec.hs+6−5
@@ -7,8 +7,9 @@ module Web.HttpApiData.Internal.FormUrlEncodedSpec (spec) where
7import Control.Applicative
7import Control.Applicative
8#endif
8#endif
9
9
10−import Test.Hspec
11import qualified Data.Map as M
10import qualified Data.Map as M
11+import Data.Text (Text, unpack)
12+import Test.Hspec
12import Test.QuickCheck
13import Test.QuickCheck
13
14
14import Web.HttpApiData.Internal.FormUrlEncoded
15import Web.HttpApiData.Internal.FormUrlEncoded
@@ -57,16 +58,16 @@ genericSpec = describe "Default (generic) instances" $ do
57
58
58 it "is the right inverse of ToForm" $ property $ \x (y :: Int) -> do
59 it "is the right inverse of ToForm" $ property $ \x (y :: Int) -> do
59 let f1 = Form $ M.fromList [("rec1", x), ("rec2", toUrlPiece y)]
60 let f1 = Form $ M.fromList [("rec1", x), ("rec2", toUrlPiece y)]
60− Right r1 = fromForm f1 :: Either String SimpleRec
61+ Right r1 = fromForm f1 :: Either Text SimpleRec
61 toForm r1 `shouldBe` f1
62 toForm r1 `shouldBe` f1
62 let f2 = Form $ M.fromList [("right1", x), ("right2", toUrlPiece y)]
63 let f2 = Form $ M.fromList [("right1", x), ("right2", toUrlPiece y)]
63− Right r2 = fromForm f2 :: Either String SimpleSumRec
64+ Right r2 = fromForm f2 :: Either Text SimpleSumRec
64 toForm r2 `shouldBe` f2
65 toForm r2 `shouldBe` f2
65
66
66 it "returns the underlying error" $ do
67 it "returns the underlying error" $ do
67 let f = Form $ M.fromList [("rec1", "anything"), ("rec2", "bad")]
68 let f = Form $ M.fromList [("rec1", "anything"), ("rec2", "bad")]
68− Left e = fromForm f :: Either String SimpleRec
69+ Left e = fromForm f :: Either Text SimpleRec
69− e `shouldContain` "input does not start with a digit"
70+ unpack e `shouldContain` "input does not start with a digit"
70
71
71urlEncoding :: Spec
72urlEncoding :: Spec
72urlEncoding = describe "urlEncoding" $ do
73urlEncoding = describe "urlEncoding" $ do