@@ -21,6 +21,8 @@ module Web.HttpApiData (
2121 readEitherUrlPiece,
2222) where
2323
24+import Control.Applicative
25+
2426import Data.Monoid
2527import Data.ByteString (ByteString)
2628
@@ -141,11 +143,18 @@ instance ToHttpApiData Text where toUrlPiece = id
141143instance ToHttpApiData L.Text where toUrlPiece = L.toStrict
142144instance ToHttpApiData Day where toUrlPiece = showUrlPiece
143145
146+-- |
147+-- >>> toUrlPiece (Just "Hello")
148+-- "Just Hello"
149+instance ToHttpApiData a => ToHttpApiData (Maybe a) where
150+ toUrlPiece (Just x) = "Just " <> toUrlPiece x
151+ toUrlPiece Nothing = "Nothing"
152+
144153-- |
145154-- >>> parseUrlPiece "_" :: Either Text ()
146155-- Right ()
147156instance FromHttpApiData () where
148− parseUrlPiece "_" = pure ()
157+ parseUrlPiece "_" = return ()
149158 parseUrlPiece s = defaultParseError s
150159
151160instance FromHttpApiData Bool where parseUrlPiece = readEitherUrlPiece
@@ -167,6 +176,16 @@ instance FromHttpApiData Text where parseUrlPiece = Right
167176instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
168177instance FromHttpApiData Day where parseUrlPiece = readEitherUrlPiece
169178
179+-- |
180+-- >>> parseUrlPiece "Just 123" :: Either Text Int
181+-- Just 123
182+instance FromHttpApiData a => FromHttpApiData (Maybe a) where
183+ parseUrlPiece "Nothing" = return Nothing
184+ parseUrlPiece s =
185+ case T.stripPrefix "Just " s of
186+ Nothing -> defaultParseError s
187+ Just x -> Just <$> parseUrlPiece x
188+
170189-- $examples
171190--
172191-- Booleans: