module TD.Data.BotCommand
( BotCommand(..)
, defaultBotCommand
) 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 BotCommand
= BotCommand
{ BotCommand -> Maybe Text
command :: Maybe T.Text
, BotCommand -> Maybe Text
description :: Maybe T.Text
}
deriving (BotCommand -> BotCommand -> Bool
(BotCommand -> BotCommand -> Bool)
-> (BotCommand -> BotCommand -> Bool) -> Eq BotCommand
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BotCommand -> BotCommand -> Bool
== :: BotCommand -> BotCommand -> Bool
$c/= :: BotCommand -> BotCommand -> Bool
/= :: BotCommand -> BotCommand -> Bool
Eq, Int -> BotCommand -> ShowS
[BotCommand] -> ShowS
BotCommand -> String
(Int -> BotCommand -> ShowS)
-> (BotCommand -> String)
-> ([BotCommand] -> ShowS)
-> Show BotCommand
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BotCommand -> ShowS
showsPrec :: Int -> BotCommand -> ShowS
$cshow :: BotCommand -> String
show :: BotCommand -> String
$cshowList :: [BotCommand] -> ShowS
showList :: [BotCommand] -> ShowS
Show)
instance I.ShortShow BotCommand where
shortShow :: BotCommand -> String
shortShow BotCommand
{ command :: BotCommand -> Maybe Text
command = Maybe Text
command_
, description :: BotCommand -> Maybe Text
description = Maybe Text
description_
}
= String
"BotCommand"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"command" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
command_
, String
"description" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
description_
]
instance AT.FromJSON BotCommand where
parseJSON :: Value -> Parser BotCommand
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
"botCommand" -> Value -> Parser BotCommand
parseBotCommand Value
v
String
_ -> Parser BotCommand
forall a. Monoid a => a
mempty
where
parseBotCommand :: A.Value -> AT.Parser BotCommand
parseBotCommand :: Value -> Parser BotCommand
parseBotCommand = String
-> (Object -> Parser BotCommand) -> Value -> Parser BotCommand
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotCommand" ((Object -> Parser BotCommand) -> Value -> Parser BotCommand)
-> (Object -> Parser BotCommand) -> Value -> Parser BotCommand
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
command_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"command"
Maybe Text
description_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"description"
BotCommand -> Parser BotCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotCommand -> Parser BotCommand)
-> BotCommand -> Parser BotCommand
forall a b. (a -> b) -> a -> b
$ BotCommand
{ command :: Maybe Text
command = Maybe Text
command_
, description :: Maybe Text
description = Maybe Text
description_
}
parseJSON Value
_ = Parser BotCommand
forall a. Monoid a => a
mempty
instance AT.ToJSON BotCommand where
toJSON :: BotCommand -> Value
toJSON BotCommand
{ command :: BotCommand -> Maybe Text
command = Maybe Text
command_
, description :: BotCommand -> Maybe Text
description = Maybe Text
description_
}
= [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
"botCommand"
, Key
"command" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
command_
, Key
"description" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
description_
]
defaultBotCommand :: BotCommand
defaultBotCommand :: BotCommand
defaultBotCommand =
BotCommand
{ command :: Maybe Text
command = Maybe Text
forall a. Maybe a
Nothing
, description :: Maybe Text
description = Maybe Text
forall a. Maybe a
Nothing
}