git.iko.soy/typeable/http-api-data
on master help sign in
Use attoparsec-iso8601
6bc47a8Oleg Grenrus
CHANGELOG.md+9
@@ -1,3 +1,12 @@
1+0.3.7
2+---
3+
4+* Minor change
5+ * Use [`attoparsec-iso8601`](http://hackage.haskell.org/package/attoparsec-iso8601)
6+ for parsing of time types. Now the accepted formats are the same as by `aeson`,
7+ i.e. parsers are more lenient
8+ (see [#41](https://github.com/fizruk/http-api-data/pull/41)).
9+
10.3.6
100.3.6
2---
11---
3
12
http-api-data.cabal+3−1
@@ -37,7 +37,9 @@ flag use-text-show
37library
37library
38 hs-source-dirs: src/
38 hs-source-dirs: src/
39 include-dirs: include/
39 include-dirs: include/
40− build-depends: base >= 4.7 && < 4.10
40+ build-depends: base >= 4.7 && < 4.10
41+ , attoparsec >= 0.13.0.1 && < 0.14
42+ , attoparsec-iso8601 >= 1.0.0.0 && < 1.1
41 , bytestring
43 , bytestring
42 , containers
44 , containers
43 , hashable
45 , hashable
src/Web/Internal/HttpApiData.hs+19−9
@@ -58,6 +58,9 @@ import Data.Data (Data)
58import qualified Data.ByteString.Builder as BS
58import qualified Data.ByteString.Builder as BS
59import qualified Network.HTTP.Types as H
59import qualified Network.HTTP.Types as H
60
60
61+import qualified Data.Attoparsec.Text as Atto
62+import qualified Data.Attoparsec.Time as Atto
63+
61
64
62-- $setup
65-- $setup
63-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
66-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
@@ -577,27 +580,25 @@ instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
577-- |
580-- |
578-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
581-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
579-- Right (2016,12,1)
582-- Right (2016,12,1)
580−instance FromHttpApiData Day where parseUrlPiece = readTextData
583+instance FromHttpApiData Day where parseUrlPiece = runAtto Atto.day
581−
582−timeParseUrlPiece :: ParseTime t => String -> Text -> Either Text t
583−timeParseUrlPiece fmt = parseMaybeTextData (timeParseUrlPieceMaybe . T.unpack)
584− where
585− timeParseUrlPieceMaybe = parseTime defaultTimeLocale (iso8601DateFormat (Just fmt))
586
584
587-- |
585-- |
588-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
586-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
589-- Right 2015-10-03 14:55:01
587-- Right 2015-10-03 14:55:01
590−instance FromHttpApiData LocalTime where parseUrlPiece = timeParseUrlPiece "%H:%M:%S"
588+instance FromHttpApiData LocalTime where parseUrlPiece = runAtto Atto.localTime
591
589
592-- |
590-- |
593-- >>> parseUrlPiece "2015-10-03T14:55:01+0000" :: Either Text ZonedTime
591-- >>> parseUrlPiece "2015-10-03T14:55:01+0000" :: Either Text ZonedTime
594-- Right 2015-10-03 14:55:01 +0000
592-- Right 2015-10-03 14:55:01 +0000
593+--
594+-- >>> parseQueryParam "2016-12-31T01:00:00Z" :: Either Text ZonedTime
595+-- Right 2016-12-31 01:00:00 +0000
595−instance FromHttpApiData ZonedTime where parseUrlPiece = timeParseUrlPiece "%H:%M:%S%z"
596+instance FromHttpApiData ZonedTime where parseUrlPiece = runAtto Atto.zonedTime
596
597
597-- |
598-- |
598-- >>> parseUrlPiece "2015-10-03T00:14:24Z" :: Either Text UTCTime
599-- >>> parseUrlPiece "2015-10-03T00:14:24Z" :: Either Text UTCTime
599-- Right 2015-10-03 00:14:24 UTC
600-- Right 2015-10-03 00:14:24 UTC
600−instance FromHttpApiData UTCTime where parseUrlPiece = timeParseUrlPiece "%H:%M:%SZ"
601+instance FromHttpApiData UTCTime where parseUrlPiece = runAtto Atto.utcTime
601
602
602instance FromHttpApiData NominalDiffTime where parseUrlPiece = fmap fromInteger . parseUrlPiece
603instance FromHttpApiData NominalDiffTime where parseUrlPiece = fmap fromInteger . parseUrlPiece
603
604
@@ -650,3 +651,12 @@ instance FromHttpApiData a => FromHttpApiData (LenientData a) where
650 parseUrlPiece = Right . LenientData . parseUrlPiece
651 parseUrlPiece = Right . LenientData . parseUrlPiece
651 parseHeader = Right . LenientData . parseHeader
652 parseHeader = Right . LenientData . parseHeader
652 parseQueryParam = Right . LenientData . parseQueryParam
653 parseQueryParam = Right . LenientData . parseQueryParam
654+
655+-------------------------------------------------------------------------------
656+-- Attoparsec helpers
657+-------------------------------------------------------------------------------
658+
659+runAtto :: Atto.Parser a -> Text -> Either Text a
660+runAtto p t = case Atto.parseOnly (p <* Atto.endOfInput) t of
661+ Left err -> Left (T.pack err)
662+ Right x -> Right x
stack.yaml+3−2
@@ -1,4 +1,4 @@
1−resolver: nightly-2016-12-31
1+resolver: nightly-2017-04-01
2
2
3packages:
3packages:
4- '.'
4- '.'
@@ -7,5 +7,6 @@ flags:
7 http-api-data:
7 http-api-data:
8 use-text-show: false
8 use-text-show: false
9
9
10−extra-deps: []
10+extra-deps:
11+- attoparsec-iso8601-1.0.0.0
11extra-package-dbs: []
12extra-package-dbs: []