git.iko.soy/typeable/http-api-data
on master help sign in
Use attoparsec-iso8601
commit6bc47a8a46e5c4e0676390e4b74deecd67179fc5 authorOleg Grenrus <oleg.grenrus@iki.fi> dateThu 20 Apr 2017, 21:34 (9 years ago) parente321382
4 files changed+34−12
CHANGELOG.md+9 http-api-data.cabal+3−1 src/Web/Internal/HttpApiData.hs+19−9 stack.yaml+3−2
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+
1100.3.6
211---
312
http-api-data.cabal+3−1
@@ -37,7 +37,9 @@ flag use-text-show
3737library
3838 hs-source-dirs: src/
3939 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
4143 , bytestring
4244 , containers
4345 , hashable
src/Web/Internal/HttpApiData.hs+19−9
@@ -58,6 +58,9 @@ import Data.Data (Data)
5858import qualified Data.ByteString.Builder as BS
5959import qualified Network.HTTP.Types as H
6060
61+import qualified Data.Attoparsec.Text as Atto
62+import qualified Data.Attoparsec.Time as Atto
63+
6164
6265-- $setup
6366-- >>> data BasicAuthToken = BasicAuthToken Text deriving (Show)
@@ -577,27 +580,25 @@ instance FromHttpApiData L.Text where parseUrlPiece = Right . L.fromStrict
577580-- |
578581-- >>> toGregorian <$> parseUrlPiece "2016-12-01"
579582-- Right (2016,12,1)
580−instance FromHttpApiData Day where parseUrlPiece = readTextData
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))
583+instance FromHttpApiData Day where parseUrlPiece = runAtto Atto.day
586584
587585-- |
588586-- >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
589587-- 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
591589
592590-- |
593591-- >>> parseUrlPiece "2015-10-03T14:55:01+0000" :: Either Text ZonedTime
594592-- Right 2015-10-03 14:55:01 +0000
595−instance FromHttpApiData ZonedTime where parseUrlPiece = timeParseUrlPiece "%H:%M:%S%z"
593+--
594+-- >>> parseQueryParam "2016-12-31T01:00:00Z" :: Either Text ZonedTime
595+-- Right 2016-12-31 01:00:00 +0000
596+instance FromHttpApiData ZonedTime where parseUrlPiece = runAtto Atto.zonedTime
596597
597598-- |
598599-- >>> parseUrlPiece "2015-10-03T00:14:24Z" :: Either Text UTCTime
599600-- 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
601602
602603instance FromHttpApiData NominalDiffTime where parseUrlPiece = fmap fromInteger . parseUrlPiece
603604
@@ -650,3 +651,12 @@ instance FromHttpApiData a => FromHttpApiData (LenientData a) where
650651 parseUrlPiece = Right . LenientData . parseUrlPiece
651652 parseHeader = Right . LenientData . parseHeader
652653 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
22
33packages:
44- '.'
@@ -7,5 +7,6 @@ flags:
77 http-api-data:
88 use-text-show: false
99
10−extra-deps: []
10+extra-deps:
11+- attoparsec-iso8601-1.0.0.0
1112extra-package-dbs: []