Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3707 [remove wallet orphan instances] into CO-372…
Browse files Browse the repository at this point in the history
…/TheGreatCleanup
  • Loading branch information
KtorZ committed Nov 2, 2018
2 parents 1996752 + 032e4fb commit 8f875ac
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 110 deletions.
4 changes: 0 additions & 4 deletions wallet-new/cardano-sl-wallet-new.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ library
Cardano.Wallet.Kernel.Util.StrictNonEmpty
Cardano.Wallet.Kernel.Util.StrictStateT
Cardano.Wallet.Kernel.Wallets
Cardano.Wallet.Orphans
Cardano.Wallet.Orphans.Aeson
Cardano.Wallet.Orphans.Arbitrary
Cardano.Wallet.Orphans.Bi
Cardano.Wallet.Server
Cardano.Wallet.Server.CLI
Cardano.Wallet.Server.Middlewares
Expand Down
3 changes: 1 addition & 2 deletions wallet-new/src/Cardano/Wallet/API/V1/Swagger/Example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Data.Swagger (Definitions, NamedSchema (..), Schema,
import Data.Swagger.Declare (Declare)
import Data.Typeable (typeOf)

import Cardano.Wallet.Orphans.Arbitrary ()
import Test.QuickCheck (Arbitrary (..), listOf1)
import Test.QuickCheck.Gen (Gen (..), resize)
import Test.QuickCheck.Random (mkQCGen)
Expand All @@ -21,7 +20,7 @@ class Arbitrary a => Example a where
example = arbitrary

instance Example ()
instance Example a => Example (NonEmpty a)
instance (Example a, Arbitrary (NonEmpty a) ) => Example (NonEmpty a)

-- NOTE: we don't want to see empty list examples in our swagger doc :)
instance Example a => Example [a] where
Expand Down
50 changes: 39 additions & 11 deletions wallet-new/src/Cardano/Wallet/API/V1/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskell #-}

{-# LANGUAGE ViewPatterns #-}
-- The hlint parser fails on the `pattern` function, so we disable the
-- language extension here.
{-# LANGUAGE NoPatternSynonyms #-}
Expand Down Expand Up @@ -163,8 +163,9 @@ import Data.Swagger.Internal.TypeShape (GenericHasSimpleShape,
GenericShape)
import Data.Text (Text, dropEnd, toLower)
import qualified Data.Text as T
import Data.Version (Version)
import Formatting (bprint, build, fconst, int, sformat, stext, (%))
import Data.Version (Version (..), parseVersion, showVersion)
import Formatting (bprint, build, fconst, int, sformat, shown, stext,
(%))
import qualified Formatting.Buildable
import Generics.SOP.TH (deriveGeneric)
import GHC.Generics (Generic, Rep)
Expand All @@ -188,7 +189,6 @@ import Cardano.Wallet.API.V1.Generic (jsendErrorGenericParseJSON,
jsendErrorGenericToJSON)
import Cardano.Wallet.API.V1.Swagger.Example (Example, example,
genExample)
import Cardano.Wallet.Orphans.Aeson ()
import Cardano.Wallet.Types.UtxoStatistics
import Cardano.Wallet.Util (mkJsonKey, showApiUtcTime)

Expand All @@ -197,7 +197,6 @@ import qualified Pos.Binary.Class as Bi
import qualified Pos.Chain.Txp as Txp
import qualified Pos.Chain.Update as Core
import qualified Pos.Client.Txp.Util as Core
import Pos.Core (addressF)
import qualified Pos.Core as Core
import Pos.Crypto (PublicKey (..), decodeHash, hashHexF)
import qualified Pos.Crypto.Signing as Core
Expand All @@ -208,7 +207,9 @@ import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..),
buildSafe, buildSafeList, buildSafeMaybe,
deriveSafeBuildable, plainOrSecureF)
import Pos.Util.Servant (Flaggable (..))
import Test.Pos.Chain.Update.Arbitrary ()
import Test.Pos.Core.Arbitrary ()
import Text.ParserCombinators.ReadP (readP_to_S)

-- | Declare generic schema, while documenting properties
-- For instance:
Expand Down Expand Up @@ -314,7 +315,7 @@ instance Bounded a => Bounded (V1 a) where
minBound = V1 $ minBound @a
maxBound = V1 $ maxBound @a

instance Buildable a => Buildable (V1 a) where
instance {-# OVERLAPPABLE #-} Buildable a => Buildable (V1 a) where
build (V1 x) = bprint build x

instance Buildable (SecureLog a) => Buildable (SecureLog (V1 a)) where
Expand All @@ -323,7 +324,6 @@ instance Buildable (SecureLog a) => Buildable (SecureLog (V1 a)) where
instance (Buildable a, Buildable b) => Buildable (a, b) where
build (a, b) = bprint ("("%build%", "%build%")") a b


--
-- Benign instances
--
Expand Down Expand Up @@ -382,8 +382,17 @@ instance ToSchema (V1 Core.Coin) where
& type_ .~ SwaggerNumber
& maximum_ .~ Just (fromIntegral Core.maxCoinVal)

instance ToHttpApiData Core.Coin where
toQueryParam = pretty . Core.coinToInteger

instance FromHttpApiData Core.Coin where
parseUrlPiece p = do
c <- Core.Coin <$> parseQueryParam p
Core.checkCoin c
pure c

instance ToJSON (V1 Core.Address) where
toJSON (V1 c) = String $ sformat addressF c
toJSON (V1 c) = String $ sformat Core.addressF c

instance FromJSON (V1 Core.Address) where
parseJSON (String a) = case Core.decodeTextAddress a of
Expand Down Expand Up @@ -2356,17 +2365,36 @@ instance BuildableSafeGen SlotDuration where
data NodeSettings = NodeSettings {
setSlotDuration :: !SlotDuration
, setSoftwareInfo :: !(V1 Core.SoftwareVersion)
, setProjectVersion :: !Version
, setProjectVersion :: !(V1 Version)
, setGitRevision :: !Text
} deriving (Show, Eq, Generic)

#if !(MIN_VERSION_swagger2(2,2,2))
-- See note [Version Orphan]
instance ToSchema Version where
instance ToSchema (V1 Version) where
declareNamedSchema _ =
pure $ NamedSchema (Just "Version") $ mempty
pure $ NamedSchema (Just "V1Version") $ mempty
& type_ .~ SwaggerString

instance Buildable (V1 Version) where
build (V1 v) = bprint shown v

instance Buildable (SecureLog (V1 Version)) where
build (SecureLog x) = Formatting.Buildable.build x

instance ToJSON (V1 Version) where
toJSON (V1 v) = toJSON (showVersion v)

instance FromJSON (V1 Version) where
parseJSON (String v) = case readP_to_S parseVersion (T.unpack v) of
(reverse -> ((ver,_):_)) -> pure (V1 ver)
_ -> mempty
parseJSON x = typeMismatch "Not a valid Version" x

instance Arbitrary (V1 Version) where
arbitrary = fmap V1 arbitrary


-- Note [Version Orphan]
-- I have opened a PR to add an instance of 'Version' to the swagger2
-- library. When the PR is merged, we can delete the instance here and remove the warning from the file.
Expand Down
1 change: 0 additions & 1 deletion wallet-new/src/Cardano/Wallet/Kernel/DB/Util/AcidState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import Cardano.Wallet.Kernel.DB.Util.IxSet (HasPrimKey, Indexable,
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as IxSet
import qualified Cardano.Wallet.Kernel.DB.Util.Zoomable as Z
import Cardano.Wallet.Kernel.Util.StrictStateT
import Cardano.Wallet.Orphans ()
import UTxO.Util (mustBeRight)

{-------------------------------------------------------------------------------
Expand Down
19 changes: 0 additions & 19 deletions wallet-new/src/Cardano/Wallet/Orphans.hs

This file was deleted.

11 changes: 0 additions & 11 deletions wallet-new/src/Cardano/Wallet/Orphans/Aeson.hs

This file was deleted.

52 changes: 0 additions & 52 deletions wallet-new/src/Cardano/Wallet/Orphans/Arbitrary.hs

This file was deleted.

3 changes: 0 additions & 3 deletions wallet-new/src/Cardano/Wallet/Orphans/Bi.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ getNodeSettings w = liftIO $
V1.NodeSettings
<$> (mkSlotDuration <$> Node.getNextEpochSlotDuration node)
<*> (V1 <$> Node.curSoftwareVersion node)
<*> pure version
<*> pure (V1 version)
<*> (mkGitRevision <$> Node.compileInfo node)
where
mkSlotDuration :: Millisecond -> V1.SlotDuration
Expand Down
1 change: 0 additions & 1 deletion wallet-new/test/Cardano/Wallet/WalletLayer/QuickCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Cardano.Wallet.WalletLayer.QuickCheck
import Universum

import Cardano.Wallet.Kernel.Diffusion (WalletDiffusion (..))
import Cardano.Wallet.Orphans.Arbitrary ()
import Cardano.Wallet.WalletLayer (ActiveWalletLayer (..),
DeleteAccountError (..), DeleteExternalWalletError (..),
DeleteWalletError (..), GetAccountError (..),
Expand Down
5 changes: 3 additions & 2 deletions wallet-new/test/MarshallingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import Universum
import Control.Lens (from, to)
import Data.Aeson
import qualified Data.ByteString as BS
import Data.SafeCopy hiding (Migrate)
import Data.SafeCopy hiding (Migrate, Version)
import Data.Serialize (runGet, runPut)
import Data.Time (UTCTime (..), fromGregorian)
import Data.Time.Clock.POSIX (POSIXTime)
import Data.Typeable (typeRep)
import Data.Version (Version)
import Pos.Client.Txp.Util (InputSelectionPolicy)
import Pos.Core.NetworkMagic (NetworkMagic (..))
import qualified Pos.Crypto as Crypto
Expand Down Expand Up @@ -45,7 +46,6 @@ import Cardano.Wallet.API.V1.Types
import Cardano.Wallet.Kernel.DB.HdWallet (HdRoot)
import Cardano.Wallet.Kernel.DB.InDb (InDb (..))
import qualified Cardano.Wallet.Kernel.Util.Strict as Strict
import Cardano.Wallet.Orphans ()
import qualified Cardano.Wallet.Util as Util

-- | Tests whether or not some instances (JSON, Bi, etc) roundtrips.
Expand Down Expand Up @@ -87,6 +87,7 @@ spec = parallel $ describe "Marshalling & Unmarshalling" $ do
aesonRoundtripProp @SyncThroughput Proxy
aesonRoundtripProp @AccountIndex Proxy
aesonRoundtripProp @(V1 AddressOwnership) Proxy
aesonRoundtripProp @(V1 Version) Proxy

-- HttpApiData roundtrips
httpApiDataRoundtripProp @AccountIndex Proxy
Expand Down
7 changes: 4 additions & 3 deletions wallet-new/test/SwaggerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import Cardano.Wallet.API.Response (ValidJSON)
import qualified Cardano.Wallet.API.V1 as V1
import Cardano.Wallet.API.V1.Swagger ()
import qualified Cardano.Wallet.API.V1.Swagger as Swagger
import Cardano.Wallet.Orphans.Aeson ()
import Cardano.Wallet.Orphans.Arbitrary ()
import Pos.Chain.Update (ApplicationName (..), SoftwareVersion (..))
import Pos.Util.CompileInfo (CompileTimeInfo (CompileTimeInfo),
gitRev)
Expand All @@ -31,7 +29,7 @@ import Pos.Util.CompileInfo (CompileTimeInfo (CompileTimeInfo),
import Data.Aeson (ToJSON)
import Servant.Swagger.Internal.Test (props)
import Servant.Swagger.Internal.TypeLevel (BodyTypes, Every, TMap)
import Test.QuickCheck (Arbitrary)
import Test.QuickCheck (Arbitrary, arbitrary)

-- Syntethic instances and orphans to be able to use `validateEveryToJSON`.
-- In the future, hopefully, we will never need these.
Expand All @@ -52,6 +50,9 @@ instance ToSchema NoContent where
& type_ .~ SwaggerArray
& maxLength .~ Just 0

instance Arbitrary NoContent where
arbitrary = pure NoContent

spec :: Spec
spec = modifyMaxSuccess (const 10) $ do
describe "Swagger Integration" $ do
Expand Down

0 comments on commit 8f875ac

Please sign in to comment.