Skip to content

Commit

Permalink
Merge pull request #58 from brendanhay/feature/base64-invalid-encoding
Browse files Browse the repository at this point in the history
Invalid Base64 De/serialisation
  • Loading branch information
brendanhay authored Feb 11, 2017
2 parents 1b80508 + 30a6050 commit e5b42bb
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 88 deletions.
3 changes: 1 addition & 2 deletions core/gogol-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ library
ghc-options: -Wall

exposed-modules:
Network.Google.Data.Base64
Network.Google.Data.Bytes
, Network.Google.Data.JSON
, Network.Google.Data.Numeric
, Network.Google.Data.Time
Expand All @@ -59,7 +59,6 @@ library
, http-media >= 0.6
, http-types >= 0.8.6
, lens >= 4.4
, memory >= 0.8
, resourcet >= 1.1
, scientific >= 0.3
, servant >= 0.4.4
Expand Down
53 changes: 0 additions & 53 deletions core/src/Network/Google/Data/Base64.hs

This file was deleted.

49 changes: 49 additions & 0 deletions core/src/Network/Google/Data/Bytes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- |
-- Module : Network.Google.Data.Bytes
-- Copyright : (c) 2013-2016 Brendan Hay
-- License : Mozilla Public License, v. 2.0.
-- Maintainer : Brendan Hay <[email protected]>
-- Stability : provisional
-- Portability : non-portable (GHC extensions)
--
module Network.Google.Data.Bytes
( Bytes (..)
, _Bytes
) where

import Control.Lens (Iso', iso)
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.ByteString (ByteString)
import Data.Data (Data, Typeable)
import Data.Hashable
import qualified Data.Text.Encoding as Text
import GHC.Generics (Generic)
import Network.Google.Data.JSON (parseJSONText, toJSONText)
import Web.HttpApiData (FromHttpApiData (..),
ToHttpApiData (..))
-- | Binary data.
--
-- This data is passed to/from the serialisation routines as-is, and any
-- particular encoding or decoding (say, base64) is left up to the caller.
newtype Bytes = Bytes { unBytes :: ByteString }
deriving (Eq, Show, Read, Ord, Data, Typeable, Generic)

instance Hashable Bytes

_Bytes :: Iso' Bytes ByteString
_Bytes = iso unBytes Bytes

instance ToHttpApiData Bytes where
toQueryParam = Text.decodeUtf8 . unBytes
toHeader = unBytes

instance FromHttpApiData Bytes where
parseQueryParam = pure . Bytes . Text.encodeUtf8
parseHeader = pure . Bytes

instance FromJSON Bytes where parseJSON = parseJSONText "Bytes"
instance ToJSON Bytes where toJSON = toJSONText
2 changes: 1 addition & 1 deletion core/src/Network/Google/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import Servant.API as Export hiding (Headers, getResponse)
import Servant.Utils.Links as Export hiding (Link)
import Web.HttpApiData as Export (FromHttpApiData (..), ToHttpApiData (..))

import Network.Google.Data.Base64 as Export
import Network.Google.Data.Bytes as Export
import Network.Google.Data.JSON as Export
import Network.Google.Data.Numeric as Export
import Network.Google.Data.Time as Export
Expand Down
3 changes: 3 additions & 0 deletions gen/.ghci
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:set -XOverloadedStrings

:set prompt "> "
6 changes: 3 additions & 3 deletions gen/gogol-gen.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ executable gogol-gen
ghc-options: -Wall -threaded

build-depends:
aeson
aeson >= 1
, attoparsec
, base >= 4.8
, bifunctors
Expand All @@ -55,8 +55,8 @@ executable gogol-gen
, errors >= 2.1.2
, formatting
, hashable
, haskell-src-exts >= 1.17.1
, hindent
, haskell-src-exts == 1.17.1
, hindent < 5
, lens
, mtl
, optparse-applicative
Expand Down
11 changes: 0 additions & 11 deletions gen/src/Gen/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@

module Gen.Orphans where

import Data.Aeson
import Data.Bifunctor
import Data.Hashable
import qualified Data.HashMap.Strict as Map
import Data.String
import qualified Data.Text as Text
import Language.Haskell.Exts.Build
import Language.Haskell.Exts.Pretty
import Language.Haskell.Exts.Syntax

instance Hashable Name

instance IsString Name where fromString = name
instance IsString QName where fromString = UnQual . name
instance IsString QOp where fromString = op . sym

instance ToJSON a => ToJSON (Map.HashMap Name a) where
toJSON = toJSON
. Map.fromList
. map (first (Text.pack . prettyPrint))
. Map.toList
4 changes: 2 additions & 2 deletions gen/src/Gen/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ internalLit = \case
Nat -> TyApp (TyCon "Textual") (TyCon "Nat")
Float -> TyApp (TyCon "Textual") (TyCon "Double")
Double -> TyApp (TyCon "Textual") (TyCon "Double")
Byte -> TyCon "Base64"
Byte -> TyCon "Bytes"
UInt32 -> TyApp (TyCon "Textual") (TyCon "Word32")
UInt64 -> TyApp (TyCon "Textual") (TyCon "Word64")
Int32 -> TyApp (TyCon "Textual") (TyCon "Int32")
Expand Down Expand Up @@ -622,7 +622,7 @@ iso = \case
TLit Duration -> Just (var "_Duration")
TLit Float -> Just (var "_Coerce")
TLit Double -> Just (var "_Coerce")
TLit Byte -> Just (var "_Base64")
TLit Byte -> Just (var "_Bytes")
TLit UInt32 -> Just (var "_Coerce")
TLit UInt64 -> Just (var "_Coerce")
TLit Int32 -> Just (var "_Coerce")
Expand Down
1 change: 0 additions & 1 deletion gen/src/Gen/Types/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LText
import Data.Text.Manipulate
import Gen.Orphans ()
import Gen.Types.Help
import Gen.Types.Id
import Gen.Types.NS
Expand Down
28 changes: 15 additions & 13 deletions gen/src/Gen/Types/Id.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ import Control.Applicative
import Control.Monad
import Data.Aeson hiding (Bool, String)
import qualified Data.Attoparsec.Text as A
import Data.Bifunctor (first)
import qualified Data.CaseInsensitive as CI
import Data.Foldable (foldl')
import Data.Function (on)
import Data.Hashable
import qualified Data.HashMap.Strict as Map
import Data.List (intersperse)
import Data.List (elemIndex, nub, sortOn)
import Data.Semigroup hiding (Sum)
Expand All @@ -73,9 +71,7 @@ import qualified Data.Text as Text
import qualified Data.Text.Lazy.Builder as Build
import Data.Text.Manipulate
import Formatting
import Gen.Orphans ()
import Gen.Text
import Gen.Types.Map
import GHC.Generics (Generic)
import Language.Haskell.Exts.Build
import Language.Haskell.Exts.Syntax (Exp, Name (..))
Expand Down Expand Up @@ -164,22 +160,28 @@ instance IsString Global where
instance FromJSON Global where
parseJSON = withText "global" (pure . mkGlobal)

instance FromJSONKey Global where
fromJSONKey = FromJSONKeyText mkGlobal

instance ToJSON Global where
toJSON = toJSON . global

instance FromJSON v => FromJSON (Map Global v) where
parseJSON = fmap (Map.fromList . map (first mkGlobal) . Map.toList)
. parseJSON

gid :: Format a (Global -> a)
gid = later (Build.fromText . global)

newtype Local = Local { local :: Text }
deriving (Eq, Ord, Show, Generic, Hashable, FromJSON, ToJSON, IsString)

instance FromJSON v => FromJSON (Map Local v) where
parseJSON = fmap (Map.fromList . map (first Local) . Map.toList)
. parseJSON
deriving
( Eq
, Ord
, Show
, Generic
, Hashable
, FromJSON
, ToJSON
, FromJSONKey
, ToJSONKey
, IsString
)

lid :: Format a (Local -> a)
lid = later (Build.fromText . local)
Expand Down
1 change: 0 additions & 1 deletion gen/src/Gen/Types/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import Data.Semigroup ((<>))
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Text.Manipulate
import Gen.Orphans ()
import Gen.Text
import Gen.TH
import Gen.Types.Help
Expand Down
3 changes: 2 additions & 1 deletion gen/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Control.Error
import Control.Lens hiding ((<.>))
import Control.Monad.State
import Data.List (nub, sort)
import Data.Monoid ((<>))
import Data.String
import qualified Data.Text as Text
import qualified Filesystem as FS
Expand Down Expand Up @@ -152,7 +153,7 @@ main = do
let anx = _optAnnexes </> fromText modelName <.> "json"
p <- isFile anx
if not p
then say ("Skipping '" % stext % "' due to mimsing annex configuration.")
then say ("Skipping '" % stext % "' due to missing annex configuration.")
modelName
else do
s <- sequence
Expand Down
2 changes: 2 additions & 0 deletions gen/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ flags: {}

extra-deps:
- text-regex-replace-0.1.1.1
- haskell-src-exts-1.17.1
- hindent-4.6.4

packages:
- '.'

0 comments on commit e5b42bb

Please sign in to comment.