module TD.Data.MessageOrigin
(MessageOrigin(..)) 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 MessageOrigin
= MessageOriginUser
{ MessageOrigin -> Maybe Int
sender_user_id :: Maybe Int
}
| MessageOriginHiddenUser
{ MessageOrigin -> Maybe Text
sender_name :: Maybe T.Text
}
| MessageOriginChat
{ MessageOrigin -> Maybe Int
sender_chat_id :: Maybe Int
, MessageOrigin -> Maybe Text
author_signature :: Maybe T.Text
}
| MessageOriginChannel
{ MessageOrigin -> Maybe Int
chat_id :: Maybe Int
, MessageOrigin -> Maybe Int
message_id :: Maybe Int
, author_signature :: Maybe T.Text
}
deriving (MessageOrigin -> MessageOrigin -> Bool
(MessageOrigin -> MessageOrigin -> Bool)
-> (MessageOrigin -> MessageOrigin -> Bool) -> Eq MessageOrigin
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessageOrigin -> MessageOrigin -> Bool
== :: MessageOrigin -> MessageOrigin -> Bool
$c/= :: MessageOrigin -> MessageOrigin -> Bool
/= :: MessageOrigin -> MessageOrigin -> Bool
Eq, Int -> MessageOrigin -> ShowS
[MessageOrigin] -> ShowS
MessageOrigin -> String
(Int -> MessageOrigin -> ShowS)
-> (MessageOrigin -> String)
-> ([MessageOrigin] -> ShowS)
-> Show MessageOrigin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessageOrigin -> ShowS
showsPrec :: Int -> MessageOrigin -> ShowS
$cshow :: MessageOrigin -> String
show :: MessageOrigin -> String
$cshowList :: [MessageOrigin] -> ShowS
showList :: [MessageOrigin] -> ShowS
Show)
instance I.ShortShow MessageOrigin where
shortShow :: MessageOrigin -> String
shortShow MessageOriginUser
{ sender_user_id :: MessageOrigin -> Maybe Int
sender_user_id = Maybe Int
sender_user_id_
}
= String
"MessageOriginUser"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"sender_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sender_user_id_
]
shortShow MessageOriginHiddenUser
{ sender_name :: MessageOrigin -> Maybe Text
sender_name = Maybe Text
sender_name_
}
= String
"MessageOriginHiddenUser"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"sender_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
sender_name_
]
shortShow MessageOriginChat
{ sender_chat_id :: MessageOrigin -> Maybe Int
sender_chat_id = Maybe Int
sender_chat_id_
, author_signature :: MessageOrigin -> Maybe Text
author_signature = Maybe Text
author_signature_
}
= String
"MessageOriginChat"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"sender_chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sender_chat_id_
, String
"author_signature" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
author_signature_
]
shortShow MessageOriginChannel
{ chat_id :: MessageOrigin -> Maybe Int
chat_id = Maybe Int
chat_id_
, message_id :: MessageOrigin -> Maybe Int
message_id = Maybe Int
message_id_
, author_signature :: MessageOrigin -> Maybe Text
author_signature = Maybe Text
author_signature_
}
= String
"MessageOriginChannel"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
, String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
, String
"author_signature" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
author_signature_
]
instance AT.FromJSON MessageOrigin where
parseJSON :: Value -> Parser MessageOrigin
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
"messageOriginUser" -> Value -> Parser MessageOrigin
parseMessageOriginUser Value
v
String
"messageOriginHiddenUser" -> Value -> Parser MessageOrigin
parseMessageOriginHiddenUser Value
v
String
"messageOriginChat" -> Value -> Parser MessageOrigin
parseMessageOriginChat Value
v
String
"messageOriginChannel" -> Value -> Parser MessageOrigin
parseMessageOriginChannel Value
v
String
_ -> Parser MessageOrigin
forall a. Monoid a => a
mempty
where
parseMessageOriginUser :: A.Value -> AT.Parser MessageOrigin
parseMessageOriginUser :: Value -> Parser MessageOrigin
parseMessageOriginUser = String
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageOriginUser" ((Object -> Parser MessageOrigin) -> Value -> Parser MessageOrigin)
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
sender_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"sender_user_id"
MessageOrigin -> Parser MessageOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageOrigin -> Parser MessageOrigin)
-> MessageOrigin -> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ MessageOriginUser
{ sender_user_id :: Maybe Int
sender_user_id = Maybe Int
sender_user_id_
}
parseMessageOriginHiddenUser :: A.Value -> AT.Parser MessageOrigin
parseMessageOriginHiddenUser :: Value -> Parser MessageOrigin
parseMessageOriginHiddenUser = String
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageOriginHiddenUser" ((Object -> Parser MessageOrigin) -> Value -> Parser MessageOrigin)
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
sender_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"sender_name"
MessageOrigin -> Parser MessageOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageOrigin -> Parser MessageOrigin)
-> MessageOrigin -> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ MessageOriginHiddenUser
{ sender_name :: Maybe Text
sender_name = Maybe Text
sender_name_
}
parseMessageOriginChat :: A.Value -> AT.Parser MessageOrigin
parseMessageOriginChat :: Value -> Parser MessageOrigin
parseMessageOriginChat = String
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageOriginChat" ((Object -> Parser MessageOrigin) -> Value -> Parser MessageOrigin)
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
sender_chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"sender_chat_id"
Maybe Text
author_signature_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"author_signature"
MessageOrigin -> Parser MessageOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageOrigin -> Parser MessageOrigin)
-> MessageOrigin -> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ MessageOriginChat
{ sender_chat_id :: Maybe Int
sender_chat_id = Maybe Int
sender_chat_id_
, author_signature :: Maybe Text
author_signature = Maybe Text
author_signature_
}
parseMessageOriginChannel :: A.Value -> AT.Parser MessageOrigin
parseMessageOriginChannel :: Value -> Parser MessageOrigin
parseMessageOriginChannel = String
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageOriginChannel" ((Object -> Parser MessageOrigin) -> Value -> Parser MessageOrigin)
-> (Object -> Parser MessageOrigin)
-> Value
-> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"chat_id"
Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"message_id"
Maybe Text
author_signature_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"author_signature"
MessageOrigin -> Parser MessageOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageOrigin -> Parser MessageOrigin)
-> MessageOrigin -> Parser MessageOrigin
forall a b. (a -> b) -> a -> b
$ MessageOriginChannel
{ chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
, message_id :: Maybe Int
message_id = Maybe Int
message_id_
, author_signature :: Maybe Text
author_signature = Maybe Text
author_signature_
}
parseJSON Value
_ = Parser MessageOrigin
forall a. Monoid a => a
mempty