git.iko.soy/typeable/http-api-data
on master help sign in
Merge github.com:MomemtumMori/path-pieces

Conflicts: Web/PathPieces.hs

commit24d2890a3341df38f17cc39333dbf8a8027c31d9 authorMichael Snoyman <michael@snoyman.com> dateThu 10 Jul 2014, 09:56 (12 years ago) parent4922b9ba0e325c
2 files changed+23−8
Web/PathPieces.hs+11−1 test/main.hs+12−7
Web/PathPieces.hs+11−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-}
1+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-}
22module Web.PathPieces
33 ( PathPiece (..)
44 , PathMultiPiece (..)
@@ -67,6 +67,16 @@ instance PathPiece Day where
6767 _ -> Nothing
6868 toPathPiece = S.pack . show
6969
70+instance (PathPiece a) => PathPiece (Maybe a) where
71+ fromPathPiece s = case S.stripPrefix "Just " s of
72+ Just r -> Just `fmap` fromPathPiece r
73+ _ -> case s of
74+ "Nothing" -> Just Nothing
75+ _ -> Nothing
76+ toPathPiece m = case m of
77+ Just s -> "Just " `S.append` toPathPiece s
78+ _ -> "Nothing"
79+
7080class PathMultiPiece s where
7181 fromPathMultiPiece :: [S.Text] -> Maybe s
7282 toPathMultiPiece :: s -> [S.Text]
test/main.hs+12−7
@@ -27,17 +27,22 @@ spec = do
2727 Nothing -> null p
2828 Just pConverted -> p == pConverted
2929
30− prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->
30+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
3131 case (fromPathPiece . toPathPiece) p of
3232 Nothing -> T.null p
3333 Just pConverted -> p == pConverted
3434
35− prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->
35+ prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
3636 case (fromPathPiece . toPathPiece) p of
3737 Nothing -> False
3838 Just pConverted -> p == pConverted
3939
40− prop "toPathPiece <=> fromPathPiece String" $ \(p::Bool) ->
40+ prop "toPathPiece <=> fromPathPiece Bool" $ \(p::Bool) ->
41+ case (fromPathPiece . toPathPiece) p of
42+ Nothing -> False
43+ Just pConverted -> p == pConverted
44+
45+ prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) ->
4146 case (fromPathPiece . toPathPiece) p of
4247 Nothing -> False
4348 Just pConverted -> p == pConverted
@@ -46,7 +51,7 @@ spec = do
4651 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
4752 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
4853
49− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->
54+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
5055 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
5156
5257
@@ -56,12 +61,12 @@ spec = do
5661 Nothing -> null p
5762 Just pConverted -> p == pConverted
5863
59− prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->
64+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
6065 case (fromPathPiece . toPathPiece) p of
6166 Nothing -> T.null p
6267 Just pConverted -> p == pConverted
6368
64− prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->
69+ prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
6570 case (fromPathPiece . toPathPiece) p of
6671 Nothing -> p < 0
6772 Just pConverted -> p == pConverted
@@ -70,7 +75,7 @@ spec = do
7075 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
7176 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
7277
73− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->
78+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
7479 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
7580
7681 it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")