module TD.Data.MessageSender
(MessageSender(..)) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
data MessageSender
= MessageSenderUser
{ MessageSender -> Maybe Int
user_id :: Maybe Int
}
| MessageSenderChat
{ MessageSender -> Maybe Int
chat_id :: Maybe Int
}
deriving (MessageSender -> MessageSender -> Bool
(MessageSender -> MessageSender -> Bool)
-> (MessageSender -> MessageSender -> Bool) -> Eq MessageSender
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessageSender -> MessageSender -> Bool
== :: MessageSender -> MessageSender -> Bool
$c/= :: MessageSender -> MessageSender -> Bool
/= :: MessageSender -> MessageSender -> Bool
Eq, Int -> MessageSender -> ShowS
[MessageSender] -> ShowS
MessageSender -> String
(Int -> MessageSender -> ShowS)
-> (MessageSender -> String)
-> ([MessageSender] -> ShowS)
-> Show MessageSender
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessageSender -> ShowS
showsPrec :: Int -> MessageSender -> ShowS
$cshow :: MessageSender -> String
show :: MessageSender -> String
$cshowList :: [MessageSender] -> ShowS
showList :: [MessageSender] -> ShowS
Show)
instance I.ShortShow MessageSender where
shortShow :: MessageSender -> String
shortShow MessageSenderUser
{ user_id :: MessageSender -> Maybe Int
user_id = Maybe Int
user_id_
}
= String
"MessageSenderUser"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
]
shortShow MessageSenderChat
{ chat_id :: MessageSender -> Maybe Int
chat_id = Maybe Int
chat_id_
}
= String
"MessageSenderChat"
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_
]
instance AT.FromJSON MessageSender where
parseJSON :: Value -> Parser MessageSender
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
"messageSenderUser" -> Value -> Parser MessageSender
parseMessageSenderUser Value
v
String
"messageSenderChat" -> Value -> Parser MessageSender
parseMessageSenderChat Value
v
String
_ -> Parser MessageSender
forall a. Monoid a => a
mempty
where
parseMessageSenderUser :: A.Value -> AT.Parser MessageSender
parseMessageSenderUser :: Value -> Parser MessageSender
parseMessageSenderUser = String
-> (Object -> Parser MessageSender)
-> Value
-> Parser MessageSender
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageSenderUser" ((Object -> Parser MessageSender) -> Value -> Parser MessageSender)
-> (Object -> Parser MessageSender)
-> Value
-> Parser MessageSender
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"user_id"
MessageSender -> Parser MessageSender
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageSender -> Parser MessageSender)
-> MessageSender -> Parser MessageSender
forall a b. (a -> b) -> a -> b
$ MessageSenderUser
{ user_id :: Maybe Int
user_id = Maybe Int
user_id_
}
parseMessageSenderChat :: A.Value -> AT.Parser MessageSender
parseMessageSenderChat :: Value -> Parser MessageSender
parseMessageSenderChat = String
-> (Object -> Parser MessageSender)
-> Value
-> Parser MessageSender
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageSenderChat" ((Object -> Parser MessageSender) -> Value -> Parser MessageSender)
-> (Object -> Parser MessageSender)
-> Value
-> Parser MessageSender
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"
MessageSender -> Parser MessageSender
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageSender -> Parser MessageSender)
-> MessageSender -> Parser MessageSender
forall a b. (a -> b) -> a -> b
$ MessageSenderChat
{ chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
}
parseJSON Value
_ = Parser MessageSender
forall a. Monoid a => a
mempty
instance AT.ToJSON MessageSender where
toJSON :: MessageSender -> Value
toJSON MessageSenderUser
{ user_id :: MessageSender -> Maybe Int
user_id = Maybe Int
user_id_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"messageSenderUser"
, Key
"user_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
user_id_
]
toJSON MessageSenderChat
{ chat_id :: MessageSender -> Maybe Int
chat_id = Maybe Int
chat_id_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"messageSenderChat"
, Key
"chat_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
chat_id_
]