Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move helpers functions out of Api.Types
- Loading branch information
Showing
3 changed files
with
91 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters