@@ -11,6 +11,7 @@ module Web.HttpApiData.Internal where
1111
1212#if __GLASGOW_HASKELL__ < 710
1313import Control.Applicative
14+import Data.Traversable (Traversable(traverse))
1415#endif
1516import Control.Arrow ((&&&))
1617
@@ -71,6 +72,38 @@ class FromHttpApiData a where
7172 parseQueryParam :: Text -> Either Text a
7273 parseQueryParam = parseUrlPiece
7374
75+-- | Convert multiple values to a list of URL pieces.
76+--
77+-- >>> toUrlPieces [1, 2, 3]
78+-- ["1","2","3"]
79+toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text
80+toUrlPieces = fmap toUrlPiece
81+
82+-- | Parse multiple URL pieces.
83+--
84+-- >>> parseUrlPieces ["true", "false"] :: Either Text [Bool]
85+-- Right [True,False]
86+-- >>> parseUrlPieces ["123", "hello", "world"] :: Either Text [Int]
87+-- Left "could not parse: `hello' (input does not start with a digit)"
88+parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
89+parseUrlPieces = traverse parseUrlPiece
90+
91+-- | Convert multiple values to a list of query parameter values.
92+--
93+-- >>> toQueryParams [fromGregorian 2015 10 03, fromGregorian 2015 12 01]
94+-- ["2015-10-03","2015-12-01"]
95+toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text
96+toQueryParams = fmap toQueryParam
97+
98+-- | Parse multiple query parameters.
99+--
100+-- >>> parseQueryParams ["1", "2", "3"] :: Either Text [Int]
101+-- Right [1,2,3]
102+-- >>> parseQueryParams ["64", "128", "256"] :: Either Text [Word8]
103+-- Left "out of bounds: `256' (should be between 0 and 255)"
104+parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
105+parseQueryParams = traverse parseQueryParam
106+
74107-- | Parse URL path piece in a @'Maybe'@.
75108--
76109-- >>> parseUrlPieceMaybe "12" :: Maybe Int
@@ -271,7 +304,7 @@ readTextData = parseMaybeTextData (readMaybe . T.unpack)
271304runReader :: Reader a -> Text -> Either Text a
272305runReader reader input =
273306 case reader input of
274− Left err -> Left (T.pack err)
307+ Left err -> Left ("could not parse: `" <> input <> "' (" <> T.pack err <> ")")
275308 Right (x, rest)
276309 | T.null rest -> Right x
277310 | otherwise -> defaultParseError input