git.iko.soy/typeable/http-api-data
on master help sign in
Add to/fromUrlPieces helpers
commit63eb4504aa162823eb5c71132a8e692fe4c1335b authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 12 Oct 2015, 12:29 (11 years ago) parentcdc4b93
2 files changed+20
Web/HttpApiData.hs+4 Web/HttpApiData/Internal.hs+16
Web/HttpApiData.hs+4
@@ -19,6 +19,10 @@ module Web.HttpApiData (
1919 parseHeaderWithPrefix,
2020 parseQueryParamWithPrefix,
2121
22+ -- * Multiple URL pieces
23+ toUrlPieces,
24+ parseUrlPieces,
25+
2226 -- * Other helpers
2327 showTextData,
2428 readTextData,
Web/HttpApiData/Internal.hs+16
@@ -71,6 +71,22 @@ class FromHttpApiData a where
7171 parseQueryParam :: Text -> Either Text a
7272 parseQueryParam = parseUrlPiece
7373
74+-- | Convert multiple values to a list of URL pieces.
75+--
76+-- >>> toUrlPieces [1, 2, 3]
77+-- ["1","2","3"]
78+toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text
79+toUrlPieces = fmap toUrlPiece
80+
81+-- | Parse multiple URL pieces.
82+--
83+-- >>> parseUrlPieces ["true", "false"] :: Either Text [Bool]
84+-- Right [True,False]
85+-- >>> parseUrlPieces ["123", "hello", "world"] :: Either Text [Int]
86+-- Left "input does not start with a digit"
87+parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
88+parseUrlPieces = traverse parseUrlPiece
89+
7490-- | Parse URL path piece in a @'Maybe'@.
7591--
7692-- >>> parseUrlPieceMaybe "12" :: Maybe Int