git.iko.soy/typeable/http-api-data
on master help sign in
Merge github.com:MomemtumMori/path-pieces
24d2890Michael Snoyman
Web/PathPieces.hs+11−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-}
1+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-}
2module Web.PathPieces
2module Web.PathPieces
3 ( PathPiece (..)
3 ( PathPiece (..)
4 , PathMultiPiece (..)
4 , PathMultiPiece (..)
@@ -67,6 +67,16 @@ instance PathPiece Day where
67 _ -> Nothing
67 _ -> Nothing
68 toPathPiece = S.pack . show
68 toPathPiece = S.pack . show
69
69
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+
70class PathMultiPiece s where
80class PathMultiPiece s where
71 fromPathMultiPiece :: [S.Text] -> Maybe s
81 fromPathMultiPiece :: [S.Text] -> Maybe s
72 toPathMultiPiece :: s -> [S.Text]
82 toPathMultiPiece :: s -> [S.Text]
test/main.hs+12−7
@@ -27,17 +27,22 @@ spec = do
27 Nothing -> null p
27 Nothing -> null p
28 Just pConverted -> p == pConverted
28 Just pConverted -> p == pConverted
29
29
30− prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->
30+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
31 case (fromPathPiece . toPathPiece) p of
31 case (fromPathPiece . toPathPiece) p of
32 Nothing -> T.null p
32 Nothing -> T.null p
33 Just pConverted -> p == pConverted
33 Just pConverted -> p == pConverted
34
34
35− prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->
35+ prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
36 case (fromPathPiece . toPathPiece) p of
36 case (fromPathPiece . toPathPiece) p of
37 Nothing -> False
37 Nothing -> False
38 Just pConverted -> p == pConverted
38 Just pConverted -> p == pConverted
39
39
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) ->
41 case (fromPathPiece . toPathPiece) p of
46 case (fromPathPiece . toPathPiece) p of
42 Nothing -> False
47 Nothing -> False
43 Just pConverted -> p == pConverted
48 Just pConverted -> p == pConverted
@@ -46,7 +51,7 @@ spec = do
46 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
51 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
47 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
52 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
48
53
49− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->
54+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
50 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
55 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
51
56
52
57
@@ -56,12 +61,12 @@ spec = do
56 Nothing -> null p
61 Nothing -> null p
57 Just pConverted -> p == pConverted
62 Just pConverted -> p == pConverted
58
63
59− prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->
64+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
60 case (fromPathPiece . toPathPiece) p of
65 case (fromPathPiece . toPathPiece) p of
61 Nothing -> T.null p
66 Nothing -> T.null p
62 Just pConverted -> p == pConverted
67 Just pConverted -> p == pConverted
63
68
64− prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->
69+ prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
65 case (fromPathPiece . toPathPiece) p of
70 case (fromPathPiece . toPathPiece) p of
66 Nothing -> p < 0
71 Nothing -> p < 0
67 Just pConverted -> p == pConverted
72 Just pConverted -> p == pConverted
@@ -70,7 +75,7 @@ spec = do
70 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
75 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
71 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
76 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
72
77
73− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->
78+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
74 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
79 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
75
80
76 it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")
81 it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")