http-api-data 0.1.0 → 0.1.1
raw patch · 4 files changed
+81/−16 lines, 4 filesdep +text-showPVP ok
version bump matches the API change (PVP)
Dependencies added: text-show
API changes (from Hackage documentation)
+ Web.HttpApiData.Internal: showt :: Show a => a -> Text
Files
- CHANGELOG.md +3/−0
- README.md +1/−0
- Web/HttpApiData/Internal.hs +69/−15
- http-api-data.cabal +8/−1
CHANGELOG.md view
@@ -1,1 +1,4 @@+0.1.1+--- +* Add `use-text-show` flag to optionally use more efficient `TextShow` instances
README.md view
@@ -1,5 +1,6 @@ # http-api-data +[](http://hackage.haskell.org/package/http-api-data) [](http://travis-ci.org/fizruk/http-api-data) This package defines typeclasses used for converting Haskell data types to and from HTTP API data.
Web/HttpApiData/Internal.hs view
@@ -34,6 +34,10 @@ import Text.Read (readMaybe) import Text.ParserCombinators.ReadP (readP_to_S) +#if USE_TEXT_SHOW+import TextShow (TextShow, showt)+#endif+ -- | Convert value to HTTP API data. class ToHttpApiData a where {-# MINIMAL toUrlPiece | toQueryParam #-}@@ -96,6 +100,27 @@ Nothing -> defaultParseError input Just val -> Right val +#if USE_TEXT_SHOW+-- | Convert to URL piece using @'TextShow'@ instance.+-- The result is always lower cased.+--+-- >>> showTextData True+-- "true"+--+-- This can be used as a default implementation for enumeration types:+--+-- @+-- data MyData = Foo | Bar | Baz deriving (Generic)+-- +-- instance TextShow MyData where+-- showt = genericShowt+--+-- instance ToHttpApiData MyData where+-- toUrlPiece = showTextData+-- @+showTextData :: TextShow a => a -> Text+showTextData = T.toLower . showt+#else -- | Convert to URL piece using @'Show'@ instance. -- The result is always lower cased. --@@ -109,8 +134,13 @@ -- >>> toUrlPiece Foo -- "foo" showTextData :: Show a => a -> Text-showTextData = T.toLower . T.pack . show+showTextData = T.toLower . showt +-- | Like @'show'@, but returns @'Text'@.+showt :: Show a => a -> Text+showt = T.pack . show+#endif+ -- | Parse given text case insensitive and return the rest of the input. -- -- >>> parseUrlPieceWithPrefix "Just " "just 10" :: Either Text Int@@ -131,6 +161,27 @@ where (prefix, rest) = T.splitAt (T.length pattern) input +#if USE_TEXT_SHOW+-- | Parse values case insensitively based on @'TextShow'@ instance.+--+-- >>> parseBoundedCaseInsensitiveTextData "true" :: Either Text Bool+-- Right True+-- >>> parseBoundedCaseInsensitiveTextData "FALSE" :: Either Text Bool+-- Right False+--+-- This can be used as a default implementation for enumeration types:+--+-- @+-- data MyData = Foo | Bar | Baz deriving (Show, Bounded, Enum, Generic)+--+-- instance TextShow MyData where+-- showt = genericShowt+--+-- instance FromHttpApiData MyData where+-- parseUrlPiece = parseBoundedCaseInsensitiveTextData+-- @+parseBoundedCaseInsensitiveTextData :: forall a. (TextShow a, Bounded a, Enum a) => Text -> Either Text a+#else -- | Parse values case insensitively based on @'Show'@ instance. -- -- >>> parseBoundedCaseInsensitiveTextData "true" :: Either Text Bool@@ -145,6 +196,7 @@ -- >>> parseUrlPiece "foo" :: Either Text MyData -- Right Foo parseBoundedCaseInsensitiveTextData :: forall a. (Show a, Bounded a, Enum a) => Text -> Either Text a+#endif parseBoundedCaseInsensitiveTextData = parseMaybeTextData (flip lookup values . T.toLower) where values = map (showTextData &&& id) [minBound..maxBound :: a]@@ -201,23 +253,25 @@ instance ToHttpApiData Bool where toUrlPiece = showTextData instance ToHttpApiData Ordering where toUrlPiece = showTextData-instance ToHttpApiData Double where toUrlPiece = showTextData-instance ToHttpApiData Float where toUrlPiece = showTextData-instance ToHttpApiData Int where toUrlPiece = showTextData-instance ToHttpApiData Int8 where toUrlPiece = showTextData-instance ToHttpApiData Int16 where toUrlPiece = showTextData-instance ToHttpApiData Int32 where toUrlPiece = showTextData-instance ToHttpApiData Int64 where toUrlPiece = showTextData-instance ToHttpApiData Integer where toUrlPiece = showTextData-instance ToHttpApiData Word where toUrlPiece = showTextData-instance ToHttpApiData Word8 where toUrlPiece = showTextData-instance ToHttpApiData Word16 where toUrlPiece = showTextData-instance ToHttpApiData Word32 where toUrlPiece = showTextData-instance ToHttpApiData Word64 where toUrlPiece = showTextData++instance ToHttpApiData Double where toUrlPiece = showt+instance ToHttpApiData Float where toUrlPiece = showt+instance ToHttpApiData Int where toUrlPiece = showt+instance ToHttpApiData Int8 where toUrlPiece = showt+instance ToHttpApiData Int16 where toUrlPiece = showt+instance ToHttpApiData Int32 where toUrlPiece = showt+instance ToHttpApiData Int64 where toUrlPiece = showt+instance ToHttpApiData Integer where toUrlPiece = showt+instance ToHttpApiData Word where toUrlPiece = showt+instance ToHttpApiData Word8 where toUrlPiece = showt+instance ToHttpApiData Word16 where toUrlPiece = showt+instance ToHttpApiData Word32 where toUrlPiece = showt+instance ToHttpApiData Word64 where toUrlPiece = showt+instance ToHttpApiData Day where toUrlPiece = T.pack . show+ instance ToHttpApiData String where toUrlPiece = T.pack instance ToHttpApiData Text where toUrlPiece = id instance ToHttpApiData L.Text where toUrlPiece = L.toStrict-instance ToHttpApiData Day where toUrlPiece = showTextData instance ToHttpApiData All where toUrlPiece = toUrlPiece . getAll instance ToHttpApiData Any where toUrlPiece = toUrlPiece . getAny
http-api-data.cabal view
@@ -1,5 +1,5 @@ name: http-api-data-version: 0.1.0+version: 0.1.1 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>@@ -16,11 +16,18 @@ CHANGELOG.md README.md +flag use-text-show+ description: Use @text-show@ library for efficient @'ToHttpApiData'@ implementations.+ default: False+ library build-depends: base >= 4 && < 5 , text >= 0.5 , bytestring , time+ if flag(use-text-show)+ cpp-options: -DUSE_TEXT_SHOW+ build-depends: text-show >= 2 exposed-modules: Web.HttpApiData Web.HttpApiData.Internal