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 #-}
@@ -162,8 +162,9 @@ import Data.Swagger.Internal.TypeShape (GenericHasSimpleShape,
162
162
GenericShape )
163
163
import Data.Text (Text , dropEnd , toLower )
164
164
import qualified Data.Text as T
165
- import Data.Version (Version )
166
- import Formatting (bprint , build , fconst , int , sformat , stext , (%) )
165
+ import Data.Version (Version (.. ), parseVersion , showVersion )
166
+ import Formatting (bprint , build , fconst , int , sformat , shown , stext ,
167
+ (%) )
167
168
import qualified Formatting.Buildable
168
169
import Generics.SOP.TH (deriveGeneric )
169
170
import GHC.Generics (Generic , Rep )
@@ -187,7 +188,6 @@ import Cardano.Wallet.API.V1.Generic (jsendErrorGenericParseJSON,
187
188
jsendErrorGenericToJSON )
188
189
import Cardano.Wallet.API.V1.Swagger.Example (Example , example ,
189
190
genExample )
190
- import Cardano.Wallet.Orphans.Aeson ()
191
191
import Cardano.Wallet.Types.UtxoStatistics
192
192
import Cardano.Wallet.Util (mkJsonKey , showApiUtcTime )
193
193
@@ -196,7 +196,6 @@ import qualified Pos.Binary.Class as Bi
196
196
import qualified Pos.Chain.Txp as Txp
197
197
import qualified Pos.Chain.Update as Core
198
198
import qualified Pos.Client.Txp.Util as Core
199
- import Pos.Core (addressF )
200
199
import qualified Pos.Core as Core
201
200
import Pos.Crypto (PublicKey (.. ), decodeHash , hashHexF )
202
201
import qualified Pos.Crypto.Signing as Core
@@ -207,7 +206,9 @@ import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..),
207
206
buildSafe , buildSafeList , buildSafeMaybe ,
208
207
deriveSafeBuildable , plainOrSecureF )
209
208
import Pos.Util.Servant (Flaggable (.. ))
209
+ import Test.Pos.Chain.Update.Arbitrary ()
210
210
import Test.Pos.Core.Arbitrary ()
211
+ import Text.ParserCombinators.ReadP (readP_to_S )
211
212
212
213
-- | Declare generic schema, while documenting properties
213
214
-- For instance:
@@ -313,7 +314,7 @@ instance Bounded a => Bounded (V1 a) where
313
314
minBound = V1 $ minBound @ a
314
315
maxBound = V1 $ maxBound @ a
315
316
316
- instance Buildable a => Buildable (V1 a ) where
317
+ instance {-# OVERLAPPABLE #-} Buildable a => Buildable (V1 a ) where
317
318
build (V1 x) = bprint build x
318
319
319
320
instance Buildable (SecureLog a ) => Buildable (SecureLog (V1 a )) where
@@ -322,7 +323,6 @@ instance Buildable (SecureLog a) => Buildable (SecureLog (V1 a)) where
322
323
instance (Buildable a , Buildable b ) => Buildable (a , b ) where
323
324
build (a, b) = bprint (" (" % build% " , " % build% " )" ) a b
324
325
325
-
326
326
--
327
327
-- Benign instances
328
328
--
@@ -381,8 +381,17 @@ instance ToSchema (V1 Core.Coin) where
381
381
& type_ .~ SwaggerNumber
382
382
& maximum_ .~ Just (fromIntegral Core. maxCoinVal)
383
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
+
384
393
instance ToJSON (V1 Core. Address ) where
385
- toJSON (V1 c) = String $ sformat addressF c
394
+ toJSON (V1 c) = String $ sformat Core. addressF c
386
395
387
396
instance FromJSON (V1 Core. Address ) where
388
397
parseJSON (String a) = case Core. decodeTextAddress a of
@@ -2346,17 +2355,36 @@ instance BuildableSafeGen SlotDuration where
2346
2355
data NodeSettings = NodeSettings {
2347
2356
setSlotDuration :: ! SlotDuration
2348
2357
, setSoftwareInfo :: ! (V1 Core. SoftwareVersion )
2349
- , setProjectVersion :: ! Version
2358
+ , setProjectVersion :: ! ( V1 Version )
2350
2359
, setGitRevision :: ! Text
2351
2360
} deriving (Show , Eq , Generic )
2352
2361
2353
2362
#if !(MIN_VERSION_swagger2(2,2,2))
2354
2363
-- See note [Version Orphan]
2355
- instance ToSchema Version where
2364
+ instance ToSchema ( V1 Version ) where
2356
2365
declareNamedSchema _ =
2357
- pure $ NamedSchema (Just " Version " ) $ mempty
2366
+ pure $ NamedSchema (Just " V1Version " ) $ mempty
2358
2367
& type_ .~ SwaggerString
2359
2368
2369
+ instance Buildable (V1 Version ) where
2370
+ build (V1 v) = bprint shown v
2371
+
2372
+ instance Buildable (SecureLog (V1 Version )) where
2373
+ build (SecureLog x) = Formatting.Buildable. build x
2374
+
2375
+ instance ToJSON (V1 Version ) where
2376
+ toJSON (V1 v) = toJSON (showVersion v)
2377
+
2378
+ instance FromJSON (V1 Version ) where
2379
+ parseJSON (String v) = case readP_to_S parseVersion (T. unpack v) of
2380
+ (reverse -> ((ver,_): _)) -> pure (V1 ver)
2381
+ _ -> mempty
2382
+ parseJSON x = typeMismatch " Not a valid Version" x
2383
+
2384
+ instance Arbitrary (V1 Version ) where
2385
+ arbitrary = fmap V1 arbitrary
2386
+
2387
+
2360
2388
-- Note [Version Orphan]
2361
2389
-- I have opened a PR to add an instance of 'Version' to the swagger2
2362
2390
-- library. When the PR is merged, we can delete the instance here and remove the warning from the file.
0 commit comments