git.iko.soy/typeable/http-api-data
on master help sign in
Add UUID instances
commite8c7d07a943b1e4f7053d6b1ddd8c0b9e2b46273 authorOleg Grenrus <oleg.grenrus@iki.fi> dateFri 23 Sep 2016, 19:45 (10 years ago) parentdde6af3
4 files changed+28−1
CHANGELOG.md+6 http-api-data.cabal+3−1 src/Web/Internal/HttpApiData.hs+9 test/Web/Internal/HttpApiDataSpec.hs+10
CHANGELOG.md+6
@@ -1,3 +1,9 @@
1+0.3.1
2+---
3+
4+* Minor changes
5+ * Add instances for `Data.UUID`
6+
170.3
28---
39* Major changes:
http-api-data.cabal+3−1
@@ -1,5 +1,5 @@
11name: http-api-data
2−version: 0.3
2+version: 0.3.1
33license: BSD3
44license-file: LICENSE
55author: Nickolay Kudasov <nickolay.kudasov@gmail.com>
@@ -33,6 +33,7 @@ library
3333 , time-locale-compat >=0.1.1.0 && <0.2
3434 , unordered-containers
3535 , uri-bytestring
36+ , uuid >= 1.3.11 && <1.4
3637 if flag(use-text-show)
3738 cpp-options: -DUSE_TEXT_SHOW
3839 build-depends: text-show >= 2
@@ -64,6 +65,7 @@ test-suite spec
6465 , text
6566 , time
6667 , bytestring
68+ , uuid
6769
6870test-suite doctest
6971 ghc-options: -Wall
src/Web/Internal/HttpApiData.hs+9
@@ -46,6 +46,8 @@ import Text.ParserCombinators.ReadP (readP_to_S)
4646import TextShow (TextShow, showt)
4747#endif
4848
49+import qualified Data.UUID as UUID
50+
4951-- $setup
5052-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
5153-- >>> instance FromHttpApiData BasicAuthToken where parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h; parseQueryParam p = BasicAuthToken <$> parseQueryParam p
@@ -577,3 +579,10 @@ instance (FromHttpApiData a, FromHttpApiData b) => FromHttpApiData (Either a b)
577579 Left _ <!> y = y
578580 x <!> _ = x
579581
582+instance ToHttpApiData UUID.UUID where
583+ toUrlPiece = UUID.toText
584+ toHeader = UUID.toASCIIBytes
585+
586+instance FromHttpApiData UUID.UUID where
587+ parseUrlPiece = maybe (Left "invalid UUID") Right . UUID.fromText
588+ parseHeader = maybe (Left "invalid UUID") Right . UUID.fromASCIIBytes
test/Web/Internal/HttpApiDataSpec.hs+10
@@ -2,6 +2,7 @@
22{-# LANGUAGE ScopedTypeVariables #-}
33module Web.Internal.HttpApiDataSpec (spec) where
44
5+import Control.Applicative
56import Data.Int
67import Data.Word
78import Data.Time
@@ -9,6 +10,7 @@ import qualified Data.Text as T
910import qualified Data.Text.Lazy as L
1011import qualified Data.ByteString as BS
1112import Data.Version
13+import qualified Data.UUID as UUID
1214
1315import Data.Proxy
1416
@@ -30,6 +32,10 @@ import Web.Internal.TestInstances
3032checkUrlPiece :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
3133checkUrlPiece _ name = prop name (toUrlPiece <=> parseUrlPiece :: a -> Bool)
3234
35+-- | Check with given generator
36+checkUrlPiece' :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a) => Gen a -> String -> Spec
37+checkUrlPiece' gen name = prop name $ forAll gen (toUrlPiece <=> parseUrlPiece)
38+
3339-- | Check case insensitivity for @parseUrlPiece@.
3440checkUrlPieceI :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Arbitrary a) => Proxy a -> String -> Spec
3541checkUrlPieceI _ = checkUrlPiece (Proxy :: Proxy (RandomCase a))
@@ -61,6 +67,7 @@ spec = do
6167 checkUrlPiece (Proxy :: Proxy UTCTime) "UTCTime"
6268 checkUrlPiece (Proxy :: Proxy NominalDiffTime) "NominalDiffTime"
6369 checkUrlPiece (Proxy :: Proxy Version) "Version"
70+ checkUrlPiece' uuidGen "UUID"
6471
6572 checkUrlPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
6673 checkUrlPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
@@ -80,3 +87,6 @@ spec = do
8087
8188 it "invalid utf8 is handled" $ do
8289 parseHeaderMaybe (BS.pack [128]) `shouldBe` (Nothing :: Maybe T.Text)
90+
91+uuidGen :: Gen UUID.UUID
92+uuidGen = UUID.fromWords <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary