git.iko.soy/typeable/http-api-data
on master help sign in
Add instances for Either a b
commit46c925b51bfd262416e972e720f5963b8824229c authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateTue 6 Oct 2015, 02:09 (11 years ago) parent1aef3eb
3 files changed+32−1
Web/HttpApiData.hs+20 Web/PathPieces.hs+10 test/main.hs+2−1
Web/HttpApiData.hs+20
@@ -180,6 +180,15 @@ instance ToHttpApiData a => ToHttpApiData (Maybe a) where
180180 toUrlPiece (Just x) = "Just " <> toUrlPiece x
181181 toUrlPiece Nothing = "Nothing"
182182
183+-- |
184+-- >>> toUrlPiece (Left "err" :: Either String Int)
185+-- "Left err"
186+-- >>> toUrlPiece (Right 3 :: Either String Int)
187+-- "Right 3"
188+instance (ToHttpApiData a, ToHttpApiData b) => ToHttpApiData (Either a b) where
189+ toUrlPiece (Left x) = "Left " <> toUrlPiece x
190+ toUrlPiece (Right x) = "Right " <> toUrlPiece x
191+
183192-- |
184193-- >>> parseUrlPiece "_" :: Either Text ()
185194-- Right ()
@@ -246,6 +255,17 @@ instance FromHttpApiData a => FromHttpApiData (Maybe a) where
246255 Nothing -> defaultParseError s
247256 Just x -> Just <$> parseUrlPiece x
248257
258+-- |
259+-- >>> parseUrlPiece "Right 123" :: Either Text (Either String Int)
260+-- Right (Right 123)
261+instance (FromHttpApiData a, FromHttpApiData b) => FromHttpApiData (Either a b) where
262+ parseUrlPiece s =
263+ case T.stripPrefix "Right " s of
264+ Just r -> Right <$> parseUrlPiece r
265+ _ -> case T.stripPrefix "Left " s of
266+ Just l -> Left <$> parseUrlPiece l
267+ _ -> defaultParseError s
268+
249269-- $examples
250270--
251271-- Booleans:
Web/PathPieces.hs+10
@@ -98,6 +98,10 @@ instance PathPiece a => PathPiece (Maybe a) where
9898 toPathPiece = unsafeToPathPiece1
9999 fromPathPiece = unsafeFromPathPiece1
100100
101+instance (PathPiece a, PathPiece b) => PathPiece (Either a b) where
102+ toPathPiece = unsafeToPathPiece2
103+ fromPathPiece = unsafeFromPathPiece2
104+
101105-- | Wrapped @'PathPiece'@ value.
102106newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
103107
@@ -113,6 +117,12 @@ unsafeToPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a)
113117unsafeFromPathPiece1 :: forall f a. (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
114118unsafeFromPathPiece1 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) -> f a)) . parseUrlPiece
115119
120+unsafeToPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, ToHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => f a b -> S.Text
121+unsafeToPathPiece2 = toUrlPiece . (unsafeCoerce :: f a b -> f (WrappedPathPiece a) (WrappedPathPiece b))
122+
123+unsafeFromPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, FromHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => S.Text -> Maybe (f a b)
124+unsafeFromPathPiece2 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) (WrappedPathPiece b) -> f a b)) . parseUrlPiece
125+
116126-- | Convert Haskell values to and from sequence of route pieces.
117127class PathMultiPiece s where
118128 -- | Convert from sequence of route pieces.
test/main.hs+2−1
@@ -63,7 +63,8 @@ spec = do
6363 prop "toPathPiece <=> fromPathPiece Day" $ \(p :: Day) -> (toPathPiece <=> fromPathPiece) p
6464 prop "toPathPiece <=> fromPathPiece Version" $ \(p :: Version) -> (toPathPiece <=> fromPathPiece) p
6565
66− prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) -> (toPathPiece <=> fromPathPiece) p
66+ prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) -> (toPathPiece <=> fromPathPiece) p
67+ prop "toPathPiece <=> fromPathPiece Either Integer Text" $ \(p::Either Integer T.Text) -> (toPathPiece <=> fromPathPiece) p
6768
6869 describe "PathMultiPiece" $ do
6970 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) -> (toPathMultiPiece <=> fromPathMultiPiece) p