module TD.Data.BusinessMessage
(BusinessMessage(..)) 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 BusinessMessage
= BusinessMessage
{ BusinessMessage -> Maybe Message
message :: Maybe Message.Message
, BusinessMessage -> Maybe Message
reply_to_message :: Maybe Message.Message
}
deriving (BusinessMessage -> BusinessMessage -> Bool
(BusinessMessage -> BusinessMessage -> Bool)
-> (BusinessMessage -> BusinessMessage -> Bool)
-> Eq BusinessMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BusinessMessage -> BusinessMessage -> Bool
== :: BusinessMessage -> BusinessMessage -> Bool
$c/= :: BusinessMessage -> BusinessMessage -> Bool
/= :: BusinessMessage -> BusinessMessage -> Bool
Eq, Int -> BusinessMessage -> ShowS
[BusinessMessage] -> ShowS
BusinessMessage -> String
(Int -> BusinessMessage -> ShowS)
-> (BusinessMessage -> String)
-> ([BusinessMessage] -> ShowS)
-> Show BusinessMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BusinessMessage -> ShowS
showsPrec :: Int -> BusinessMessage -> ShowS
$cshow :: BusinessMessage -> String
show :: BusinessMessage -> String
$cshowList :: [BusinessMessage] -> ShowS
showList :: [BusinessMessage] -> ShowS
Show)
instance I.ShortShow BusinessMessage where
shortShow :: BusinessMessage -> String
shortShow BusinessMessage
{ message :: BusinessMessage -> Maybe Message
message = Maybe Message
message_
, reply_to_message :: BusinessMessage -> Maybe Message
reply_to_message = Maybe Message
reply_to_message_
}
= String
"BusinessMessage"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"message" String -> Maybe Message -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Message
message_
, String
"reply_to_message" String -> Maybe Message -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Message
reply_to_message_
]
instance AT.FromJSON BusinessMessage where
parseJSON :: Value -> Parser BusinessMessage
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
"businessMessage" -> Value -> Parser BusinessMessage
parseBusinessMessage Value
v
String
_ -> Parser BusinessMessage
forall a. Monoid a => a
mempty
where
parseBusinessMessage :: A.Value -> AT.Parser BusinessMessage
parseBusinessMessage :: Value -> Parser BusinessMessage
parseBusinessMessage = String
-> (Object -> Parser BusinessMessage)
-> Value
-> Parser BusinessMessage
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BusinessMessage" ((Object -> Parser BusinessMessage)
-> Value -> Parser BusinessMessage)
-> (Object -> Parser BusinessMessage)
-> Value
-> Parser BusinessMessage
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Message
message_ <- Object
o Object -> Key -> Parser (Maybe Message)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"message"
Maybe Message
reply_to_message_ <- Object
o Object -> Key -> Parser (Maybe Message)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"reply_to_message"
BusinessMessage -> Parser BusinessMessage
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BusinessMessage -> Parser BusinessMessage)
-> BusinessMessage -> Parser BusinessMessage
forall a b. (a -> b) -> a -> b
$ BusinessMessage
{ message :: Maybe Message
message = Maybe Message
message_
, reply_to_message :: Maybe Message
reply_to_message = Maybe Message
reply_to_message_
}
parseJSON Value
_ = Parser BusinessMessage
forall a. Monoid a => a
mempty