Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive {To,From}JSON instances for ApiEncryptMetadata. #4588

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions lib/api/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module Cardano.Wallet.Api.Types
, ApiCoinSelectionOutput (..)
, ApiCoinSelectionWithdrawal (..)
, ApiEncryptMetadata (..)
, ApiEncryptMetadataMethod (..)
, ApiConstructTransaction (..)
, ApiConstructTransactionData (..)
, ApiCosignerIndex (..)
Expand Down Expand Up @@ -193,7 +194,6 @@ module Cardano.Wallet.Api.Types
, ApiDecodeTransactionPostData (..)
, fromApiDecodeTransactionPostData
, toApiDecodeTransactionPostData
, EncryptMetadataMethod (..)

-- * API Types (Byron)
, ApiByronWallet (..)
Expand Down Expand Up @@ -1238,22 +1238,14 @@ data ApiMultiDelegationAction
deriving (Eq, Generic, Show)
deriving anyclass NFData

data EncryptMetadataMethod = AES256CBC
deriving (Eq, Generic, Show)
data ApiEncryptMetadataMethod = Basic
deriving (Bounded, Enum, Eq, Generic, Show)
deriving anyclass NFData

instance ToJSON EncryptMetadataMethod where
toJSON AES256CBC = "basic"
instance FromJSON EncryptMetadataMethod where
parseJSON = withText "base" $ \txt ->
if txt == "basic" then
pure AES256CBC
else
fail "'basic' is expected."
deriving (FromJSON, ToJSON) via DefaultSum ApiEncryptMetadataMethod

data ApiEncryptMetadata = ApiEncryptMetadata
{ passphrase :: ApiT (Passphrase "lenient")
, enc :: Maybe EncryptMetadataMethod
, enc :: Maybe ApiEncryptMetadataMethod
}
deriving (Eq, Generic, Show)
deriving (FromJSON, ToJSON) via DefaultRecord ApiEncryptMetadata
Expand Down
43 changes: 43 additions & 0 deletions lib/unit/test/data/Cardano/Wallet/Api/ApiEncryptMetadata.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/unit/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ import Cardano.Wallet.Api.Types
, ApiDelegationAction (..)
, ApiDeregisterPool (..)
, ApiEncryptMetadata (..)
, ApiEncryptMetadataMethod (..)
, ApiEra (..)
, ApiEraInfo (..)
, ApiExternalCertificate (..)
Expand Down Expand Up @@ -251,7 +252,6 @@ import Cardano.Wallet.Api.Types
, ByronWalletFromXPrvPostData (..)
, ByronWalletPostData (..)
, ByronWalletPutPassphraseData (..)
, EncryptMetadataMethod (..)
, Iso8601Time (..)
, KeyFormat (..)
, NtpSyncingStatus (..)
Expand Down Expand Up @@ -804,6 +804,7 @@ spec = do
jsonTest @ApiCredential
jsonTest @ApiCredentialType
jsonTest @ApiDelegationAction
jsonTest @ApiEncryptMetadata
jsonTest @ApiEra
jsonTest @ApiEraInfo
jsonTest @ApiError
Expand Down Expand Up @@ -2399,7 +2400,11 @@ instance Arbitrary TxMetadataWithSchema where
<*> arbitrary

instance Arbitrary ApiEncryptMetadata where
arbitrary = ApiEncryptMetadata <$> arbitrary <*> pure (Just AES256CBC)
arbitrary = applyArbitrary2 ApiEncryptMetadata

instance Arbitrary ApiEncryptMetadataMethod where
arbitrary = arbitraryBoundedEnum
shrink = shrinkBoundedEnum

instance Arbitrary DRepID where
arbitrary = do
Expand Down
Loading