module TD.Data.Currencies
  (Currencies(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

data Currencies
  = Currencies -- ^ Represents a list of currencies
    { Currencies -> Maybe [Text]
currencies :: Maybe [T.Text] -- ^ A list of ISO 4217 currency codes
    }
  deriving (Currencies -> Currencies -> Bool
(Currencies -> Currencies -> Bool)
-> (Currencies -> Currencies -> Bool) -> Eq Currencies
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Currencies -> Currencies -> Bool
== :: Currencies -> Currencies -> Bool
$c/= :: Currencies -> Currencies -> Bool
/= :: Currencies -> Currencies -> Bool
Eq, Int -> Currencies -> ShowS
[Currencies] -> ShowS
Currencies -> String
(Int -> Currencies -> ShowS)
-> (Currencies -> String)
-> ([Currencies] -> ShowS)
-> Show Currencies
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Currencies -> ShowS
showsPrec :: Int -> Currencies -> ShowS
$cshow :: Currencies -> String
show :: Currencies -> String
$cshowList :: [Currencies] -> ShowS
showList :: [Currencies] -> ShowS
Show)

instance I.ShortShow Currencies where
  shortShow :: Currencies -> String
shortShow Currencies
    { currencies :: Currencies -> Maybe [Text]
currencies = Maybe [Text]
currencies_
    }
      = String
"Currencies"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"currencies" String -> Maybe [Text] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Text]
currencies_
        ]

instance AT.FromJSON Currencies where
  parseJSON :: Value -> Parser Currencies
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"currencies" -> Value -> Parser Currencies
parseCurrencies Value
v
      String
_            -> Parser Currencies
forall a. Monoid a => a
mempty
    
    where
      parseCurrencies :: A.Value -> AT.Parser Currencies
      parseCurrencies :: Value -> Parser Currencies
parseCurrencies = String
-> (Object -> Parser Currencies) -> Value -> Parser Currencies
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Currencies" ((Object -> Parser Currencies) -> Value -> Parser Currencies)
-> (Object -> Parser Currencies) -> Value -> Parser Currencies
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Text]
currencies_ <- Object
o Object -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"currencies"
        Currencies -> Parser Currencies
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Currencies -> Parser Currencies)
-> Currencies -> Parser Currencies
forall a b. (a -> b) -> a -> b
$ Currencies
          { currencies :: Maybe [Text]
currencies = Maybe [Text]
currencies_
          }
  parseJSON Value
_ = Parser Currencies
forall a. Monoid a => a
mempty