on develop help sign in
Removing not-so-'smart' de/serialisation of base64 bytes streams
commit30a60507511eba4e86ff0e8d250bc1eef9cea12a authorBrendan Hay <brendan.g.hay@gmail.com> dateSat 11 Feb 2017, 11:14 (10 years ago) parent3f8bd78
5 files changed+53−58
core/gogol-core.cabal+1−2 core/src/Network/Google/Data/Base64.hs−53 core/src/Network/Google/Data/Bytes.hs+49 core/src/Network/Google/Prelude.hs+1−1 gen/src/Gen/Syntax.hs+2−2
core/gogol-core.cabal+1−2
@@ -36,7 +36,7 @@ library
3636 ghc-options: -Wall
3737
3838 exposed-modules:
39− Network.Google.Data.Base64
39+ Network.Google.Data.Bytes
4040 , Network.Google.Data.JSON
4141 , Network.Google.Data.Numeric
4242 , Network.Google.Data.Time
@@ -59,7 +59,6 @@ library
5959 , http-media >= 0.6
6060 , http-types >= 0.8.6
6161 , lens >= 4.4
62− , memory >= 0.8
6362 , resourcet >= 1.1
6463 , scientific >= 0.3
6564 , servant >= 0.4.4
core/src/Network/Google/Data/Base64.hsdeleted−53
@@ -1,53 +0,0 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4−
5−-- |
6−-- Module : Network.Google.Data.Base64
7−-- Copyright : (c) 2013-2016 Brendan Hay
8−-- License : Mozilla Public License, v. 2.0.
9−-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
10−-- Stability : provisional
11−-- Portability : non-portable (GHC extensions)
12−--
13−module Network.Google.Data.Base64
14− ( Base64 (..)
15− , _Base64
16− ) where
17−
18−import Control.Lens (Iso', iso)
19−import Data.Aeson (FromJSON (..), ToJSON (..))
20−import qualified Data.ByteArray.Encoding as BA
21−import Data.ByteString (ByteString)
22−import qualified Data.ByteString.Char8 as BS8
23−import Data.Data (Data, Typeable)
24−import Data.Hashable
25−import qualified Data.Text.Encoding as Text
26−import GHC.Generics (Generic)
27−import Network.Google.Data.JSON (parseJSONText, toJSONText)
28−import Web.HttpApiData (FromHttpApiData (..),
29− ToHttpApiData (..))
30−
31−-- | Base64 encoded binary data.
32−--
33−-- Encoding\/decoding is automatically deferred to serialisation and deserialisation
34−-- respectively.
35−newtype Base64 = Base64 { unBase64 :: ByteString }
36− deriving (Eq, Read, Ord, Data, Typeable, Generic)
37−
38−instance Hashable Base64
39−
40−_Base64 :: Iso' Base64 ByteString
41−_Base64 = iso unBase64 Base64
42−
43−instance ToHttpApiData Base64 where
44− toQueryParam = Text.decodeUtf8 . toHeader
45− toHeader = BA.convertToBase BA.Base64 . unBase64
46−
47−instance FromHttpApiData Base64 where
48− parseQueryParam = parseHeader . Text.encodeUtf8
49− parseHeader = either fail (pure . Base64) . BA.convertFromBase BA.Base64
50−
51−instance Show Base64 where show = show . BS8.unpack . unBase64
52−instance FromJSON Base64 where parseJSON = parseJSONText "Base64"
53−instance ToJSON Base64 where toJSON = toJSONText
core/src/Network/Google/Data/Bytes.hsnew file+49
@@ -0,0 +1,49 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
5+-- |
6+-- Module : Network.Google.Data.Bytes
7+-- Copyright : (c) 2013-2016 Brendan Hay
8+-- License : Mozilla Public License, v. 2.0.
9+-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
10+-- Stability : provisional
11+-- Portability : non-portable (GHC extensions)
12+--
13+module Network.Google.Data.Bytes
14+ ( Bytes (..)
15+ , _Bytes
16+ ) where
17+
18+import Control.Lens (Iso', iso)
19+import Data.Aeson (FromJSON (..), ToJSON (..))
20+import Data.ByteString (ByteString)
21+import Data.Data (Data, Typeable)
22+import Data.Hashable
23+import qualified Data.Text.Encoding as Text
24+import GHC.Generics (Generic)
25+import Network.Google.Data.JSON (parseJSONText, toJSONText)
26+import Web.HttpApiData (FromHttpApiData (..),
27+ ToHttpApiData (..))
28+-- | Binary data.
29+--
30+-- This data is passed to/from the serialisation routines as-is, and any
31+-- particular encoding or decoding (say, base64) is left up to the caller.
32+newtype Bytes = Bytes { unBytes :: ByteString }
33+ deriving (Eq, Show, Read, Ord, Data, Typeable, Generic)
34+
35+instance Hashable Bytes
36+
37+_Bytes :: Iso' Bytes ByteString
38+_Bytes = iso unBytes Bytes
39+
40+instance ToHttpApiData Bytes where
41+ toQueryParam = Text.decodeUtf8 . unBytes
42+ toHeader = unBytes
43+
44+instance FromHttpApiData Bytes where
45+ parseQueryParam = pure . Bytes . Text.encodeUtf8
46+ parseHeader = pure . Bytes
47+
48+instance FromJSON Bytes where parseJSON = parseJSONText "Bytes"
49+instance ToJSON Bytes where toJSON = toJSONText
core/src/Network/Google/Prelude.hs+1−1
@@ -31,7 +31,7 @@ import Servant.API as Export hiding (Headers, getResponse)
3131import Servant.Utils.Links as Export hiding (Link)
3232import Web.HttpApiData as Export (FromHttpApiData (..), ToHttpApiData (..))
3333
34−import Network.Google.Data.Base64 as Export
34+import Network.Google.Data.Bytes as Export
3535import Network.Google.Data.JSON as Export
3636import Network.Google.Data.Numeric as Export
3737import Network.Google.Data.Time as Export
gen/src/Gen/Syntax.hs+2−2
@@ -588,7 +588,7 @@ internalLit = \case
588588 Nat -> TyApp (TyCon "Textual") (TyCon "Nat")
589589 Float -> TyApp (TyCon "Textual") (TyCon "Double")
590590 Double -> TyApp (TyCon "Textual") (TyCon "Double")
591− Byte -> TyCon "Base64"
591+ Byte -> TyCon "Bytes"
592592 UInt32 -> TyApp (TyCon "Textual") (TyCon "Word32")
593593 UInt64 -> TyApp (TyCon "Textual") (TyCon "Word64")
594594 Int32 -> TyApp (TyCon "Textual") (TyCon "Int32")
@@ -622,7 +622,7 @@ iso = \case
622622 TLit Duration -> Just (var "_Duration")
623623 TLit Float -> Just (var "_Coerce")
624624 TLit Double -> Just (var "_Coerce")
625− TLit Byte -> Just (var "_Base64")
625+ TLit Byte -> Just (var "_Bytes")
626626 TLit UInt32 -> Just (var "_Coerce")
627627 TLit UInt64 -> Just (var "_Coerce")
628628 TLit Int32 -> Just (var "_Coerce")