git.iko.soy/typeable/http-api-data
on master help sign in
Add parse*Maybe helpers
commit68a9b35566cfc2474ed4efc0c36e3a46f8692ef7 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateWed 7 Oct 2015, 16:24 (11 years ago) parent6a475a5
3 files changed+29−10
Web/HttpApiData/Internal.hs+21 Web/PathPieces/Internal.hs+2−2 test/Spec.hs+6−8
Web/HttpApiData/Internal.hs+21
@@ -64,6 +64,27 @@ class FromHttpApiData a where
6464 parseQueryParam :: Text -> Either Text a
6565 parseQueryParam = parseUrlPiece
6666
67+-- | Parse URL path piece in a @'Maybe'@.
68+--
69+-- >>> parseUrlPieceMaybe "12" :: Maybe Int
70+-- Just 12
71+parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a
72+parseUrlPieceMaybe = either (const Nothing) Just . parseUrlPiece
73+
74+-- | Parse HTTP header value in a @'Maybe'@.
75+--
76+-- >>> parseHeaderMaybe "hello" :: Maybe Text
77+-- Just "hello"
78+parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a
79+parseHeaderMaybe = either (const Nothing) Just . parseHeader
80+
81+-- | Parse query param value in a @'Maybe'@.
82+--
83+-- >>> parseQueryParamMaybe "true" :: Maybe Bool
84+-- Just True
85+parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a
86+parseQueryParamMaybe = either (const Nothing) Just . parseQueryParam
87+
6788-- | Default parsing error.
6889defaultParseError :: Text -> Either Text a
6990defaultParseError input = Left ("could not parse: `" <> input <> "'")
Web/PathPieces/Internal.hs+2−2
@@ -25,7 +25,7 @@ import Data.Void (Void)
2525#endif
2626
2727import Web.HttpApiData
28−import Web.HttpApiData.Internal (parseMaybeTextData)
28+import Web.HttpApiData.Internal (parseMaybeTextData, parseUrlPieceMaybe)
2929
3030-- | Convert Haskell values to and from route piece.
3131class PathPiece s where
@@ -112,7 +112,7 @@ defaultToPathPiece = toUrlPiece
112112
113113-- | Convert value from route piece using @'FromHttpApiData'@ implementation.
114114defaultFromPathPiece :: FromHttpApiData a => S.Text -> Maybe a
115−defaultFromPathPiece = either (const Nothing) Just . parseUrlPiece
115+defaultFromPathPiece = parseUrlPieceMaybe
116116
117117-- | Convert value to route piece using @'PathPiece'@ instance for its parameter.
118118defaultToPathPiece1 :: (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
test/Spec.hs+6−8
@@ -17,6 +17,7 @@ import Test.Hspec.QuickCheck(prop)
1717import Test.QuickCheck
1818
1919import Web.HttpApiData
20+import Web.HttpApiData.Internal
2021
2122instance Arbitrary T.Text where
2223 arbitrary = T.pack <$> arbitrary
@@ -63,13 +64,13 @@ instance ToHttpApiData a => ToHttpApiData (RandomCase a) where
6364instance FromHttpApiData a => FromHttpApiData (RandomCase a) where
6465 parseUrlPiece s = RandomCase [] <$> parseUrlPiece s
6566
66−-- | Check case insensitivity for @fromUrlPiece@.
67+-- | Check case insensitivity for @parseUrlPiece@.
6768checkUrlPieceI :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
6869checkUrlPieceI _ = checkUrlPiece (Proxy :: Proxy (RandomCase a))
6970
7071spec :: Spec
7172spec = do
72− describe "toUrlPiece <=> fromUrlPiece" $ do
73+ describe "toUrlPiece <=> parseUrlPiece" $ do
7374 checkUrlPiece (Proxy :: Proxy ()) "()"
7475 checkUrlPiece (Proxy :: Proxy Char) "Char"
7576 checkUrlPieceI (Proxy :: Proxy Bool) "Bool"
@@ -97,12 +98,9 @@ spec = do
9798 checkUrlPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
9899
99100 it "bad integers are rejected" $ do
100− fromUrlPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
101+ parseUrlPieceMaybe (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
101102
102103 it "bounds checking works" $ do
103− fromUrlPiece (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
104− fromUrlPiece (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
105− where
106− fromUrlPiece :: FromHttpApiData a => T.Text -> Maybe a
107− fromUrlPiece = either (const Nothing) Just . parseUrlPiece
104+ parseUrlPieceMaybe (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
105+ parseUrlPieceMaybe (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
108106