on develop help sign in
Replacing usage of Servant.Common.Text with http-api-data

This is backwards compatible to avoid requiring a restrictive servant bound, or CPP. Fixes #21.

commit51d29976850e87cb9f17ce569c190ba73476b1e1 authorBrendan Hay <brendan.g.hay@gmail.com> dateTue 10 May 2016, 16:32 (10 years ago) parent48c3cb5
2 files changed+29−36
core/gogol-core.cabal+2 core/src/Network/Google/Data/Time.hs+27−36
core/gogol-core.cabal+2
@@ -46,12 +46,14 @@ library
4646 aeson >= 0.8
4747 , attoparsec >= 0.11.3
4848 , base >= 4.7
49+ , bifunctors >= 0.1
4950 , bytestring >= 0.9
5051 , case-insensitive >= 1.2
5152 , conduit >= 1.1
5253 , dlist >= 0.7
5354 , exceptions >= 0.6
5455 , hashable >= 1.2
56+ , http-api-data >= 0.2
5557 , http-client >= 0.4.4
5658 , http-media >= 0.6
5759 , http-types >= 0.8.6
core/src/Network/Google/Data/Time.hs+27−36
@@ -22,49 +22,53 @@ module Network.Google.Data.Time
2222
2323import Control.Lens
2424import Data.Aeson
25−import Data.Aeson.Encode
26−import qualified Data.Aeson.Types as Aeson
25+import qualified Data.Aeson.Types as Aeson
2726import Data.Attoparsec.Text
28−import Data.Bits ((.&.))
29−import Data.Char (ord)
30−import Data.Data (Data, Typeable)
31−import Data.Text (Text)
32−import qualified Data.Text as Text
33−import qualified Data.Text.Lazy as LText
34−import qualified Data.Text.Lazy.Builder as Build
27+import Data.Bifunctor (first, second)
28+import Data.Bits ((.&.))
29+import Data.Char (ord)
30+import Data.Data (Data, Typeable)
31+import Data.Text (Text)
32+import qualified Data.Text as Text
3533import Data.Time
3634import GHC.Generics
37−import Servant.Common.Text
35+import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))
3836
3937-- | This SHOULD be a time in the format of hh:mm:ss. It is
4038-- recommended that you use the "date-time" format instead of "time"
4139-- unless you need to transfer only the time part.
42−newtype Time' = Time' TimeOfDay
40+newtype Time' = Time' { unTime :: TimeOfDay }
4341 deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
4442
4543_Time :: Iso' Time' TimeOfDay
46−_Time = iso (\(Time' t) -> t) Time'
44+_Time = iso unTime Time'
45+
46+instance ToHttpApiData Time' where
47+ toQueryParam = Text.pack . show . unTime
48+
49+instance FromHttpApiData Time' where
50+ parseQueryParam = second Time' . parseText timeParser
4751
4852-- | This SHOULD be a date in the format of YYYY-MM-DD. It is
4953-- recommended that you use the "date-time" format instead of "date"
5054-- unless you need to transfer only the date part.
51−newtype Date' = Date' Day
52− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
55+newtype Date' = Date' { unDate :: Day }
56+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToHttpApiData, FromHttpApiData)
5357
5458_Date :: Iso' Date' Day
55−_Date = iso (\(Date' t) -> t) Date'
59+_Date = iso unDate Date'
5660
5761-- | This SHOULD be a date in ISO 8601 format of YYYY-MM-
5862-- DDThh:mm:ssZ in UTC time. This is the recommended form of date/timestamp.
59−newtype DateTime' = DateTime' UTCTime
60− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
63+newtype DateTime' = DateTime' { unDateTime :: UTCTime }
64+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToHttpApiData, FromHttpApiData)
6165
6266_DateTime :: Iso' DateTime' UTCTime
63−_DateTime = iso (\(DateTime' t) -> t) DateTime'
67+_DateTime = iso unDateTime DateTime'
6468
65−instance FromText Time' where fromText = fmap Time' . parseText timeParser
66−instance FromText Date' where fromText = fmap Date' . parseText dayParser
67−instance FromText DateTime' where fromText = Aeson.parseMaybe parseJSON . String
69+instance ToJSON Time' where toJSON = String . toQueryParam
70+instance ToJSON Date' where toJSON = String . toQueryParam
71+instance ToJSON DateTime' where toJSON = toJSON . unDateTime
6872
6973instance FromJSON Time' where
7074 parseJSON = fmap Time' . withText "time" (run timeParser)
@@ -75,21 +79,8 @@ instance FromJSON Date' where
7579instance FromJSON DateTime' where
7680 parseJSON = fmap DateTime' . parseJSON
7781
78−-- FIXME: Revisit once aeson-0.10 is more widely available.
79−instance ToText Time' where toText = Text.pack . show . view _Time
80−instance ToText Date' where toText = Text.pack . show . view _Date
81−instance ToText DateTime' where toText = encodeText
82−
83−-- FIXME: Revisit once aeson-0.10 is more widely available.
84−instance ToJSON Time' where toJSON = String . toText
85−instance ToJSON Date' where toJSON = String . toText
86−instance ToJSON DateTime' where toJSON = toJSON . view _DateTime
87−
88−parseText :: Parser a -> Text -> Maybe a
89−parseText p = either (const Nothing) Just . parseOnly p
90−
91−encodeText :: ToJSON a => a -> Text
92−encodeText = LText.toStrict . Build.toLazyText . encodeToTextBuilder . toJSON
82+parseText :: Parser a -> Text -> Either Text a
83+parseText p = first Text.pack . parseOnly p
9384
9485-- | Parse a time of the form @HH:MM:SS@.
9586timeParser :: Parser TimeOfDay