packages feed

http-api-data 0.2.2 → 0.2.3

raw patch · 4 files changed

+71/−10 lines, 4 files

Files

CHANGELOG.md view
@@ -1,3 +1,7 @@+0.2.3+---+* Add more parser helpers for `Bounded` `Enum` types.+ 0.2.2 --- 
Web/HttpApiData.hs view
@@ -27,10 +27,17 @@   toQueryParams,   parseQueryParams, +  -- * Parsers for @'Bounded'@ @'Enum'@s+  parseBoundedUrlPiece,+  parseBoundedQueryParam,+  parseBoundedHeader,+  parseBoundedEnumOf,+  parseBoundedEnumOfI,+  parseBoundedTextData,+   -- * Other helpers   showTextData,   readTextData,-  parseBoundedTextData, ) where  import Web.HttpApiData.Internal
Web/HttpApiData/Internal.hs view
@@ -44,6 +44,9 @@ #endif  -- | Convert value to HTTP API data.+--+-- __WARNING__: Do not derive this using @DeriveAnyClass@ as the generated+-- instance will loop indefinitely. class ToHttpApiData a where   {-# MINIMAL toUrlPiece | toQueryParam #-}   -- | Convert to URL path piece.@@ -59,6 +62,9 @@   toQueryParam = toUrlPiece  -- | Parse value from HTTP API data.+--+-- __WARNING__: Do not derive this using @DeriveAnyClass@ as the generated+-- instance will loop indefinitely. class FromHttpApiData a where   {-# MINIMAL parseUrlPiece | parseQueryParam #-}   -- | Parse URL path piece.@@ -280,10 +286,54 @@ -- Right Foo parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a #endif-parseBoundedTextData = parseMaybeTextData (flip lookup values . T.toLower)-  where-    values = map (showTextData &&& id) [minBound..maxBound]+parseBoundedTextData = parseBoundedEnumOfI showTextData +-- | Lookup values based on a precalculated mapping of their representations.+lookupBoundedEnumOf :: (Bounded a, Enum a, Eq b) => (a -> b) -> b -> Maybe a+lookupBoundedEnumOf f = flip lookup (map (f &&& id) [minBound..maxBound])++-- | Parse values based on a precalculated mapping of their @'Text'@ representation.+--+-- >>> parseBoundedEnumOf toUrlPiece "true" :: Either Text Bool+-- Right True+--+-- For case sensitive parser see 'parseBoundedEnumOfI'.+parseBoundedEnumOf :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a+parseBoundedEnumOf = parseMaybeTextData . lookupBoundedEnumOf++-- | /Case insensitive/.+--+-- Parse values case insensitively based on a precalculated mapping+-- of their @'Text'@ representations.+--+-- >>> parseBoundedEnumOfI toUrlPiece "FALSE" :: Either Text Bool+-- Right False+--+-- For case sensitive parser see 'parseBoundedEnumOf'.+parseBoundedEnumOfI :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a+parseBoundedEnumOfI f = parseBoundedEnumOf (T.toLower . f) . T.toLower++-- | /Case insensitive/.+--+-- Parse values case insensitively based on @'ToHttpApiData'@ instance.+-- Uses @'toUrlPiece'@ to get possible values.+parseBoundedUrlPiece :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a+parseBoundedUrlPiece = parseBoundedEnumOfI toUrlPiece++-- | /Case insensitive/.+--+-- Parse values case insensitively based on @'ToHttpApiData'@ instance.+-- Uses @'toQueryParam'@ to get possible values.+parseBoundedQueryParam :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a+parseBoundedQueryParam = parseBoundedEnumOfI toQueryParam++-- | Parse values based on @'ToHttpApiData'@ instance.+-- Uses @'toHeader'@ to get possible values.+parseBoundedHeader :: (ToHttpApiData a, Bounded a, Enum a) => ByteString -> Either Text a+parseBoundedHeader bs = case lookupBoundedEnumOf toHeader bs of+  Nothing -> defaultParseError (decodeUtf8 bs)+  Just x  -> return x+ -- | Parse URL piece using @'Read'@ instance. -- -- Use for types which do not involve letters:@@ -442,8 +492,8 @@   parseUrlPiece _ = Left "Void cannot be parsed!" #endif -instance FromHttpApiData Bool     where parseUrlPiece = parseBoundedTextData-instance FromHttpApiData Ordering where parseUrlPiece = parseBoundedTextData+instance FromHttpApiData Bool     where parseUrlPiece = parseBoundedUrlPiece+instance FromHttpApiData Ordering where parseUrlPiece = parseBoundedUrlPiece instance FromHttpApiData Double   where parseUrlPiece = runReader rational instance FromHttpApiData Float    where parseUrlPiece = runReader rational instance FromHttpApiData Int      where parseUrlPiece = parseBounded (signed decimal)
http-api-data.cabal view
@@ -1,5 +1,5 @@ name:            http-api-data-version:         0.2.2+version:         0.2.3 license:         BSD3 license-file:    LICENSE author:          Nickolay Kudasov <nickolay.kudasov@gmail.com>@@ -21,11 +21,11 @@   default: False  library-    build-depends:   base             >= 4       && < 5-                   , text             >= 0.5+    build-depends:   base               >= 4.6    && < 4.10+                   , text               >= 0.5                    , bytestring                    , time-                   , time-locale-compat+                   , time-locale-compat >=0.1.1.0 && <0.2     if flag(use-text-show)       cpp-options: -DUSE_TEXT_SHOW       build-depends: text-show        >= 2