Skip to content

Commit

Permalink
move helpers functions out of Api.Types
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Aug 10, 2022
1 parent 34c1da5 commit 5ca6bb4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Expand Up @@ -189,6 +189,7 @@ library
Cardano.Wallet.Api
Cardano.Wallet.Api.Aeson.Variant
Cardano.Wallet.Api.Client
Cardano.Wallet.Api.Helpers
Cardano.Wallet.Api.Link
Cardano.Wallet.Api.Server
Cardano.Wallet.Api.Server.Tls
Expand Down
80 changes: 80 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Helpers.hs
@@ -0,0 +1,80 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.Wallet.Api.Helpers
( ApiT (..)
, eitherToParser
, hexText
, fromHexText
, toTextJSON
, fromTextJSON
)
where

import Prelude

import Cardano.Wallet.Primitive.AddressDerivation
( fromHex, hex )
import Cardano.Wallet.Util
( ShowFmt (..) )
import Control.DeepSeq
( NFData )
import Data.Aeson
( ToJSON (..), Value, withText )
import Data.Bifunctor
( Bifunctor (..) )
import Data.ByteString
( ByteString )
import Data.Hashable
( Hashable )
import Data.Text
( Text )
import Data.Text.Class
( FromText (..), ToText (toText) )
import GHC.Generics
( Generic )
import Quiet
( Quiet (..) )

import qualified Data.Aeson.Types as Aeson
import qualified Data.Text.Encoding as T

-- | Polymorphic wrapper type to put around primitive types and, 3rd party lib
-- types to avoid defining orphan instances and/or, undesirable instances on
-- primitive types. It helps to keep a nice separation of concerns between the
-- API layer and other modules.
newtype ApiT a =
ApiT
{ getApiT :: a
}
deriving ( Generic, Eq, Functor )
deriving newtype ( Semigroup, Monoid, Hashable )
deriving anyclass NFData
deriving Show via (Quiet (ApiT a))

deriving instance Ord a
=> Ord (ApiT a)

eitherToParser :: Show s => Either s a -> Aeson.Parser a
eitherToParser =
either (fail . show) pure

hexText :: ByteString -> Text
hexText = T.decodeLatin1 . hex

fromHexText :: Text -> Either String ByteString
fromHexText = fromHex . T.encodeUtf8

toTextJSON :: ToText a => ApiT a -> Value
toTextJSON = toJSON . toText . getApiT

fromTextJSON :: FromText a
=> String
-> Value
-> Aeson.Parser (ApiT a)
fromTextJSON n =
withText n (eitherToParser . bimap ShowFmt ApiT . fromText)
42 changes: 10 additions & 32 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -26,6 +26,8 @@
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -Wno-orphans #-}

-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
Expand Down Expand Up @@ -285,6 +287,14 @@ import Cardano.Mnemonic
)
import Cardano.Wallet.Api.Aeson.Variant
( variant, variants )
import Cardano.Wallet.Api.Helpers
( ApiT (..)
, eitherToParser
, fromHexText
, fromTextJSON
, hexText
, toTextJSON
)
import Cardano.Wallet.Api.Types.SchemaMetadata
( TxMetadataWithSchema )
import Cardano.Wallet.Primitive.AddressDerivation
Expand All @@ -294,8 +304,6 @@ import Cardano.Wallet.Primitive.AddressDerivation
, Index (..)
, NetworkDiscriminant (..)
, Role (..)
, fromHex
, hex
)
import Cardano.Wallet.Primitive.AddressDerivation.SharedKey
( purposeCIP1854 )
Expand Down Expand Up @@ -2002,17 +2010,6 @@ instance KnownDiscovery (SeqState network key) where
Polymorphic Types
-------------------------------------------------------------------------------}

-- | Polymorphic wrapper type to put around primitive types and, 3rd party lib
-- types to avoid defining orphan instances and/or, undesirable instances on
-- primitive types. It helps to keep a nice separation of concerns between the
-- API layer and other modules.
newtype ApiT a =
ApiT { getApiT :: a }
deriving (Generic, Eq, Functor)
deriving newtype (Semigroup, Monoid, Hashable)
deriving anyclass NFData
deriving Show via (Quiet (ApiT a))
deriving instance Ord a => Ord (ApiT a)

-- | Polymorphic wrapper for byte arrays, parameterised by the desired string
-- encoding.
Expand Down Expand Up @@ -4027,25 +4024,6 @@ explicitNothingRecordTypeOptions = defaultRecordTypeOptions
{ omitNothingFields = False
}

{-------------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------------}

eitherToParser :: Show s => Either s a -> Aeson.Parser a
eitherToParser = either (fail . show) pure

hexText :: ByteString -> Text
hexText = T.decodeLatin1 . hex

fromHexText :: Text -> Either String ByteString
fromHexText = fromHex . T.encodeUtf8

toTextJSON :: ToText a => ApiT a -> Value
toTextJSON = toJSON . toText . getApiT

fromTextJSON :: FromText a => String -> Value -> Aeson.Parser (ApiT a)
fromTextJSON n = withText n (eitherToParser . bimap ShowFmt ApiT . fromText)

{-------------------------------------------------------------------------------
User-Facing Address Encoding
-------------------------------------------------------------------------------}
Expand Down

0 comments on commit 5ca6bb4

Please sign in to comment.