@@ -16,7 +16,7 @@ module Web.HttpApiData.Internal.FormUrlEncoded where
16import Control.Applicative
16import Control.Applicative
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
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
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
111class GFromForm (f :: * -> *) where
111class GFromForm (f :: * -> *) where
112− gFromForm :: Form -> Either String (f x)
112+ gFromForm :: Form -> Either T.Text (f x)
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)
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
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
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