module TD.Data.Messages
(Messages(..)) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.Message as Message
data Messages
= Messages
{ Messages -> Maybe Int
total_count :: Maybe Int
, Messages -> Maybe [Message]
messages :: Maybe [Message.Message]
}
deriving (Messages -> Messages -> Bool
(Messages -> Messages -> Bool)
-> (Messages -> Messages -> Bool) -> Eq Messages
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Messages -> Messages -> Bool
== :: Messages -> Messages -> Bool
$c/= :: Messages -> Messages -> Bool
/= :: Messages -> Messages -> Bool
Eq, Int -> Messages -> ShowS
[Messages] -> ShowS
Messages -> String
(Int -> Messages -> ShowS)
-> (Messages -> String) -> ([Messages] -> ShowS) -> Show Messages
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Messages -> ShowS
showsPrec :: Int -> Messages -> ShowS
$cshow :: Messages -> String
show :: Messages -> String
$cshowList :: [Messages] -> ShowS
showList :: [Messages] -> ShowS
Show)
instance I.ShortShow Messages where
shortShow :: Messages -> String
shortShow Messages
{ total_count :: Messages -> Maybe Int
total_count = Maybe Int
total_count_
, messages :: Messages -> Maybe [Message]
messages = Maybe [Message]
messages_
}
= String
"Messages"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"total_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_count_
, String
"messages" String -> Maybe [Message] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Message]
messages_
]
instance AT.FromJSON Messages where
parseJSON :: Value -> Parser Messages
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
"messages" -> Value -> Parser Messages
parseMessages Value
v
String
_ -> Parser Messages
forall a. Monoid a => a
mempty
where
parseMessages :: A.Value -> AT.Parser Messages
parseMessages :: Value -> Parser Messages
parseMessages = String -> (Object -> Parser Messages) -> Value -> Parser Messages
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Messages" ((Object -> Parser Messages) -> Value -> Parser Messages)
-> (Object -> Parser Messages) -> Value -> Parser Messages
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
total_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"total_count"
Maybe [Message]
messages_ <- Object
o Object -> Key -> Parser (Maybe [Message])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"messages"
Messages -> Parser Messages
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Messages -> Parser Messages) -> Messages -> Parser Messages
forall a b. (a -> b) -> a -> b
$ Messages
{ total_count :: Maybe Int
total_count = Maybe Int
total_count_
, messages :: Maybe [Message]
messages = Maybe [Message]
messages_
}
parseJSON Value
_ = Parser Messages
forall a. Monoid a => a
mempty