git.iko.soy/typeable/http-api-data
on master help sign in
Fix some errors cases #7
commit4922b9bc55abfc450cd5dbd018bd62949298f10d authorMichael Snoyman <michael@snoyman.com> dateThu 10 Jul 2014, 09:53 (12 years ago) parent9de36e9
2 files changed+13−9
Web/PathPieces.hs+10−9 test/main.hs+3
Web/PathPieces.hs+10−9
@@ -1,4 +1,4 @@
1−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
1+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-}
22module Web.PathPieces
33 ( PathPiece (..)
44 , PathMultiPiece (..)
@@ -14,6 +14,7 @@ import qualified Data.Text as S
1414import qualified Data.Text.Lazy as L
1515import qualified Data.Text.Read
1616import Data.Time (Day)
17+import Control.Exception (assert)
1718
1819class PathPiece s where
1920 fromPathPiece :: S.Text -> Maybe s
@@ -34,29 +35,29 @@ instance PathPiece L.Text where
3435instance PathPiece Integer where
3536 fromPathPiece s =
3637 case Data.Text.Read.signed Data.Text.Read.decimal s of
37− Right (i, _) -> Just i
38− Left _ -> Nothing
38+ Right (i, "") -> Just i
39+ _ -> Nothing
3940 toPathPiece = S.pack . show
4041
4142instance PathPiece Int where
4243 fromPathPiece s =
4344 case Data.Text.Read.signed Data.Text.Read.decimal s of
44− Right (i, _) -> Just i
45− Left _ -> Nothing
45+ Right (i, "") -> Just i
46+ _ -> Nothing
4647 toPathPiece = S.pack . show
4748
4849instance PathPiece Bool where
4950 fromPathPiece t =
50− case reads $ S.unpack t of
51− [(a,"")] -> Just a
51+ case filter (null . snd) $ reads $ S.unpack t of
52+ (a, s):_ -> assert (null s) (Just a)
5253 _ -> Nothing
5354 toPathPiece = S.pack . show
5455
5556instance PathPiece Int64 where
5657 fromPathPiece s =
5758 case Data.Text.Read.signed Data.Text.Read.decimal s of
58− Right (i, _) -> Just i
59− Left _ -> Nothing
59+ Right (i, "") -> Just i
60+ _ -> Nothing
6061 toPathPiece = S.pack . show
6162
6263instance PathPiece Day where
test/main.hs+3
@@ -72,3 +72,6 @@ spec = do
7272
7373 prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->
7474 p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
75+
76+ it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")
77+ `shouldBe` (Nothing :: Maybe Int)