11
11
{-# LANGUAGE StandaloneDeriving #-}
12
12
{-# LANGUAGE StrictData #-}
13
13
{-# LANGUAGE TemplateHaskell #-}
14
-
14
+ {-# LANGUAGE ViewPatterns #-}
15
15
-- The hlint parser fails on the `pattern` function, so we disable the
16
16
-- language extension here.
17
17
{-# LANGUAGE NoPatternSynonyms #-}
@@ -161,8 +161,9 @@ import Data.Swagger.Internal.TypeShape (GenericHasSimpleShape,
161
161
GenericShape )
162
162
import Data.Text (Text , dropEnd , toLower )
163
163
import qualified Data.Text as T
164
- import Data.Version (Version )
165
- import Formatting (bprint , build , fconst , int , sformat , stext , (%) )
164
+ import Data.Version (Version (.. ), parseVersion , showVersion )
165
+ import Formatting (bprint , build , fconst , int , sformat , shown , stext ,
166
+ (%) )
166
167
import qualified Formatting.Buildable
167
168
import Generics.SOP.TH (deriveGeneric )
168
169
import GHC.Generics (Generic , Rep )
@@ -186,7 +187,6 @@ import Cardano.Wallet.API.V1.Generic (jsendErrorGenericParseJSON,
186
187
jsendErrorGenericToJSON )
187
188
import Cardano.Wallet.API.V1.Swagger.Example (Example , example ,
188
189
genExample )
189
- import Cardano.Wallet.Orphans.Aeson ()
190
190
import Cardano.Wallet.Types.UtxoStatistics
191
191
import Cardano.Wallet.Util (mkJsonKey , showApiUtcTime )
192
192
@@ -195,7 +195,6 @@ import qualified Pos.Binary.Class as Bi
195
195
import qualified Pos.Chain.Txp as Txp
196
196
import qualified Pos.Chain.Update as Core
197
197
import qualified Pos.Client.Txp.Util as Core
198
- import Pos.Core (addressF )
199
198
import qualified Pos.Core as Core
200
199
import Pos.Crypto (Hash , PublicKey (.. ), decodeHash , hashHexF )
201
200
import qualified Pos.Crypto.Signing as Core
@@ -206,7 +205,10 @@ import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..),
206
205
buildSafe , buildSafeList , buildSafeMaybe ,
207
206
deriveSafeBuildable , plainOrSecureF )
208
207
import Pos.Util.Servant (Flaggable (.. ))
208
+
209
+ import Test.Pos.Chain.Update.Arbitrary ()
209
210
import Test.Pos.Core.Arbitrary ()
211
+ import Text.ParserCombinators.ReadP (readP_to_S )
210
212
211
213
-- | Declare generic schema, while documenting properties
212
214
-- For instance:
@@ -312,7 +314,7 @@ instance Bounded a => Bounded (V1 a) where
312
314
minBound = V1 $ minBound @ a
313
315
maxBound = V1 $ maxBound @ a
314
316
315
- instance Buildable a => Buildable (V1 a ) where
317
+ instance {-# OVERLAPPABLE #-} Buildable a => Buildable (V1 a ) where
316
318
build (V1 x) = bprint build x
317
319
318
320
instance Buildable (SecureLog a ) => Buildable (SecureLog (V1 a )) where
@@ -321,7 +323,6 @@ instance Buildable (SecureLog a) => Buildable (SecureLog (V1 a)) where
321
323
instance (Buildable a , Buildable b ) => Buildable (a , b ) where
322
324
build (a, b) = bprint (" (" % build% " , " % build% " )" ) a b
323
325
324
-
325
326
--
326
327
-- Benign instances
327
328
--
@@ -380,8 +381,17 @@ instance ToSchema (V1 Core.Coin) where
380
381
& type_ .~ SwaggerNumber
381
382
& maximum_ .~ Just (fromIntegral Core. maxCoinVal)
382
383
384
+ instance ToHttpApiData Core. Coin where
385
+ toQueryParam = pretty . Core. coinToInteger
386
+
387
+ instance FromHttpApiData Core. Coin where
388
+ parseUrlPiece p = do
389
+ c <- Core. Coin <$> parseQueryParam p
390
+ Core. checkCoin c
391
+ pure c
392
+
383
393
instance ToJSON (V1 Core. Address ) where
384
- toJSON (V1 c) = String $ sformat addressF c
394
+ toJSON (V1 c) = String $ sformat Core. addressF c
385
395
386
396
instance FromJSON (V1 Core. Address ) where
387
397
parseJSON (String a) = case Core. decodeTextAddress a of
@@ -2405,17 +2415,36 @@ instance BuildableSafeGen SlotDuration where
2405
2415
data NodeSettings = NodeSettings {
2406
2416
setSlotDuration :: ! SlotDuration
2407
2417
, setSoftwareInfo :: ! (V1 Core. SoftwareVersion )
2408
- , setProjectVersion :: ! Version
2418
+ , setProjectVersion :: ! ( V1 Version )
2409
2419
, setGitRevision :: ! Text
2410
2420
} deriving (Show , Eq , Generic )
2411
2421
2412
2422
#if !(MIN_VERSION_swagger2(2,2,2))
2413
2423
-- See note [Version Orphan]
2414
- instance ToSchema Version where
2424
+ instance ToSchema ( V1 Version ) where
2415
2425
declareNamedSchema _ =
2416
- pure $ NamedSchema (Just " Version " ) $ mempty
2426
+ pure $ NamedSchema (Just " V1Version " ) $ mempty
2417
2427
& type_ .~ SwaggerString
2418
2428
2429
+ instance Buildable (V1 Version ) where
2430
+ build (V1 v) = bprint shown v
2431
+
2432
+ instance Buildable (SecureLog (V1 Version )) where
2433
+ build (SecureLog x) = Formatting.Buildable. build x
2434
+
2435
+ instance ToJSON (V1 Version ) where
2436
+ toJSON (V1 v) = toJSON (showVersion v)
2437
+
2438
+ instance FromJSON (V1 Version ) where
2439
+ parseJSON (String v) = case readP_to_S parseVersion (T. unpack v) of
2440
+ (reverse -> ((ver,_): _)) -> pure (V1 ver)
2441
+ _ -> mempty
2442
+ parseJSON x = typeMismatch " Not a valid Version" x
2443
+
2444
+ instance Arbitrary (V1 Version ) where
2445
+ arbitrary = fmap V1 arbitrary
2446
+
2447
+
2419
2448
-- Note [Version Orphan]
2420
2449
-- I have opened a PR to add an instance of 'Version' to the swagger2
2421
2450
-- library. When the PR is merged, we can delete the instance here and remove the warning from the file.
0 commit comments