git.iko.soy/typeable/http-api-data
on master help sign in
Change uses of String to Text.
commitdd1baa5690b9b448cbd175e1c968988bac97bff3 author(cdep)illabout <cdep.illabout@gmail.com> dateFri 26 Aug 2016, 13:55 (10 years ago) parent97f200f
2 files changed+16−15
src/Web/HttpApiData/Internal/FormUrlEncoded.hs+10−10 test/Web/HttpApiData/Internal/FormUrlEncodedSpec.hs+6−5
src/Web/HttpApiData/Internal/FormUrlEncoded.hs+10−10
@@ -16,7 +16,7 @@ module Web.HttpApiData.Internal.FormUrlEncoded where
1616import Control.Applicative
1717#endif
1818
19−import Control.Arrow (first, left, second)
19+import Control.Arrow (first, second)
2020import Control.Monad.State
2121import qualified Data.ByteString.Lazy as BSL
2222import qualified Data.ByteString.Lazy.UTF8 as BSLUTF8
@@ -95,9 +95,9 @@ instance (ToHttpApiData f) => GToForm (K1 i f) where
9595-- | A type that can be converted from @application/x-www-form-urlencoded@,
9696-- with the possibility of failure.
9797class FromForm a where
98− fromForm :: Form -> Either String a
98+ fromForm :: Form -> Either T.Text a
9999 default fromForm :: (Generic a, GFromForm (Rep a))
100− => Form -> Either String a
100+ => Form -> Either T.Text a
101101 fromForm = genericFromForm
102102
103103instance FromForm Form where fromForm = return
@@ -105,11 +105,11 @@ instance FromForm [(T.Text, T.Text)] where fromForm = return . M.toList . unForm
105105instance FromForm (M.Map T.Text T.Text) where fromForm = return . unForm
106106
107107genericFromForm :: (Generic a, GFromForm (Rep a))
108− => Form -> Either String a
108+ => Form -> Either T.Text a
109109genericFromForm f = to <$> gFromForm f
110110
111111class GFromForm (f :: * -> *) where
112− gFromForm :: Form -> Either String (f x)
112+ gFromForm :: Form -> Either T.Text (f x)
113113
114114instance (GFromForm f, GFromForm g)
115115 => GFromForm (f :*: g) where
@@ -126,8 +126,8 @@ instance (GFromForm f, GFromForm g)
126126instance (Selector sel, FromHttpApiData f)
127127 => GFromForm (M1 S sel (K1 i f)) where
128128 gFromForm f = case M.lookup sel (unForm f) of
129− Nothing -> Left . T.unpack $ "Could not find key " <> sel
130− Just v -> left T.unpack $ M1 . K1 <$> parseQueryParam v
129+ Nothing -> Left $ "Could not find key " <> sel
130+ Just v -> M1 . K1 <$> parseQueryParam v
131131 where sel = T.pack $ selName (Proxy3 :: Proxy3 sel g p)
132132
133133instance (GFromForm f) => GFromForm (M1 D x f) where
@@ -145,19 +145,19 @@ encodeForm xs =
145145 encodePair (k, v) = escape k <> "=" <> escape v
146146 in BSL.intercalate "&" $ map encodePair $ toList xs
147147
148−decodeForm :: BSL.ByteString -> Either String Form
148+decodeForm :: BSL.ByteString -> Either T.Text Form
149149decodeForm "" = return mempty
150150decodeForm q = do
151151 let xs :: [T.Text]
152152 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)
154154 parsePair p =
155155 case T.splitOn "=" p of
156156 [k,v] -> return ( unescape k
157157 , unescape v
158158 )
159159 [k] -> return ( unescape k, "" )
160− _ -> Left $ "not a valid pair: " <> T.unpack p
160+ _ -> Left $ "not a valid pair: " <> p
161161 unescape :: T.Text -> T.Text
162162 unescape = T.pack . unEscapeString . T.unpack . T.intercalate "%20" . T.splitOn "+"
163163 toForm <$> mapM parsePair xs
test/Web/HttpApiData/Internal/FormUrlEncodedSpec.hs+6−5
@@ -7,8 +7,9 @@ module Web.HttpApiData.Internal.FormUrlEncodedSpec (spec) where
77import Control.Applicative
88#endif
99
10−import Test.Hspec
1110import qualified Data.Map as M
11+import Data.Text (Text, unpack)
12+import Test.Hspec
1213import Test.QuickCheck
1314
1415import Web.HttpApiData.Internal.FormUrlEncoded
@@ -57,16 +58,16 @@ genericSpec = describe "Default (generic) instances" $ do
5758
5859 it "is the right inverse of ToForm" $ property $ \x (y :: Int) -> do
5960 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
6162 toForm r1 `shouldBe` f1
6263 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
6465 toForm r2 `shouldBe` f2
6566
6667 it "returns the underlying error" $ do
6768 let f = Form $ M.fromList [("rec1", "anything"), ("rec2", "bad")]
68− Left e = fromForm f :: Either String SimpleRec
69− e `shouldContain` "input does not start with a digit"
69+ Left e = fromForm f :: Either Text SimpleRec
70+ unpack e `shouldContain` "input does not start with a digit"
7071
7172urlEncoding :: Spec
7273urlEncoding = describe "urlEncoding" $ do