git.iko.soy/typeable/http-api-data
on master help sign in
Use Data.Text.Read to parse numbers
60ea922Nickolay Kudasov
Web/HttpApiData.hs+20−13
@@ -10,6 +10,8 @@ module Web.HttpApiData (
10 readEitherUrlPiece,
10 readEitherUrlPiece,
11) where
11) where
12
12
13+import Control.Arrow ((+++))
14+
13import Data.Monoid
15import Data.Monoid
14import Data.ByteString (ByteString)
16import Data.ByteString (ByteString)
15
17
@@ -17,6 +19,7 @@ import Data.Int
17import Data.Word
19import Data.Word
18
20
19import Data.Text (Text)
21import Data.Text (Text)
22+import Data.Text.Read (signed, decimal, rational, Reader)
20import Data.Text.Encoding (encodeUtf8, decodeUtf8)
23import Data.Text.Encoding (encodeUtf8, decodeUtf8)
21import qualified Data.Text as Text
24import qualified Data.Text as Text
22
25
@@ -69,6 +72,10 @@ readMaybeUrlPiece = readMaybe . Text.unpack
69readEitherUrlPiece :: Read a => Text -> Either Text a
72readEitherUrlPiece :: Read a => Text -> Either Text a
70readEitherUrlPiece = parseMaybeUrlPiece readMaybeUrlPiece
73readEitherUrlPiece = parseMaybeUrlPiece readMaybeUrlPiece
71
74
75+-- | Run @'Reader'@ as HTTP API data parser.
76+runReader :: Reader a -> Text -> Either Text a
77+runReader reader = (Text.pack +++ fst) . reader
78+
72instance ToHttpApiData Bool where toUrlPiece = showUrlPiece
79instance ToHttpApiData Bool where toUrlPiece = showUrlPiece
73instance ToHttpApiData Double where toUrlPiece = showUrlPiece
80instance ToHttpApiData Double where toUrlPiece = showUrlPiece
74instance ToHttpApiData Float where toUrlPiece = showUrlPiece
81instance ToHttpApiData Float where toUrlPiece = showUrlPiece
@@ -87,19 +94,19 @@ instance ToHttpApiData [Char] where toUrlPiece = Text.pack
87instance ToHttpApiData Text where toUrlPiece = id
94instance ToHttpApiData Text where toUrlPiece = id
88
95
89instance FromHttpApiData Bool where parseUrlPiece = readEitherUrlPiece
96instance FromHttpApiData Bool where parseUrlPiece = readEitherUrlPiece
90−instance FromHttpApiData Double where parseUrlPiece = readEitherUrlPiece
97+instance FromHttpApiData Double where parseUrlPiece = runReader rational
91−instance FromHttpApiData Float where parseUrlPiece = readEitherUrlPiece
98+instance FromHttpApiData Float where parseUrlPiece = runReader rational
92−instance FromHttpApiData Int where parseUrlPiece = readEitherUrlPiece
99+instance FromHttpApiData Int where parseUrlPiece = runReader (signed decimal)
93−instance FromHttpApiData Int8 where parseUrlPiece = readEitherUrlPiece
100+instance FromHttpApiData Int8 where parseUrlPiece = runReader (signed decimal)
94−instance FromHttpApiData Int16 where parseUrlPiece = readEitherUrlPiece
101+instance FromHttpApiData Int16 where parseUrlPiece = runReader (signed decimal)
95−instance FromHttpApiData Int32 where parseUrlPiece = readEitherUrlPiece
102+instance FromHttpApiData Int32 where parseUrlPiece = runReader (signed decimal)
96−instance FromHttpApiData Int64 where parseUrlPiece = readEitherUrlPiece
103+instance FromHttpApiData Int64 where parseUrlPiece = runReader (signed decimal)
97−instance FromHttpApiData Integer where parseUrlPiece = readEitherUrlPiece
104+instance FromHttpApiData Integer where parseUrlPiece = runReader (signed decimal)
98−instance FromHttpApiData Word where parseUrlPiece = readEitherUrlPiece
105+instance FromHttpApiData Word where parseUrlPiece = runReader decimal
99−instance FromHttpApiData Word8 where parseUrlPiece = readEitherUrlPiece
106+instance FromHttpApiData Word8 where parseUrlPiece = runReader decimal
100−instance FromHttpApiData Word16 where parseUrlPiece = readEitherUrlPiece
107+instance FromHttpApiData Word16 where parseUrlPiece = runReader decimal
101−instance FromHttpApiData Word32 where parseUrlPiece = readEitherUrlPiece
108+instance FromHttpApiData Word32 where parseUrlPiece = runReader decimal
102−instance FromHttpApiData Word64 where parseUrlPiece = readEitherUrlPiece
109+instance FromHttpApiData Word64 where parseUrlPiece = runReader decimal
103instance FromHttpApiData [Char] where parseUrlPiece = Right . Text.unpack
110instance FromHttpApiData [Char] where parseUrlPiece = Right . Text.unpack
104instance FromHttpApiData Text where parseUrlPiece = Right
111instance FromHttpApiData Text where parseUrlPiece = Right
105
112