{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}
module GitHub.Types.Events.PingEvent where

import           Data.Aeson                (FromJSON (..), ToJSON (..), object)
import           Data.Aeson.Types          (Value (..), (.:), (.:?), (.=))
import           Data.Text                 (Text)
import           Test.QuickCheck.Arbitrary (Arbitrary (..))

import           GitHub.Types.Base
import           GitHub.Types.Event


data PingEvent = PingEvent
    { PingEvent -> Maybe Installation
pingEventInstallation :: Maybe Installation
    , PingEvent -> Organization
pingEventOrganization :: Organization
    , PingEvent -> User
pingEventSender       :: User

    , PingEvent -> Hook
pingEventHook         :: Hook
    , PingEvent -> Int
pingEventHookId       :: Int
    , PingEvent -> Text
pingEventZen          :: Text
    } deriving (PingEvent -> PingEvent -> Bool
(PingEvent -> PingEvent -> Bool)
-> (PingEvent -> PingEvent -> Bool) -> Eq PingEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PingEvent -> PingEvent -> Bool
$c/= :: PingEvent -> PingEvent -> Bool
== :: PingEvent -> PingEvent -> Bool
$c== :: PingEvent -> PingEvent -> Bool
Eq, Int -> PingEvent -> ShowS
[PingEvent] -> ShowS
PingEvent -> String
(Int -> PingEvent -> ShowS)
-> (PingEvent -> String)
-> ([PingEvent] -> ShowS)
-> Show PingEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PingEvent] -> ShowS
$cshowList :: [PingEvent] -> ShowS
show :: PingEvent -> String
$cshow :: PingEvent -> String
showsPrec :: Int -> PingEvent -> ShowS
$cshowsPrec :: Int -> PingEvent -> ShowS
Show, ReadPrec [PingEvent]
ReadPrec PingEvent
Int -> ReadS PingEvent
ReadS [PingEvent]
(Int -> ReadS PingEvent)
-> ReadS [PingEvent]
-> ReadPrec PingEvent
-> ReadPrec [PingEvent]
-> Read PingEvent
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PingEvent]
$creadListPrec :: ReadPrec [PingEvent]
readPrec :: ReadPrec PingEvent
$creadPrec :: ReadPrec PingEvent
readList :: ReadS [PingEvent]
$creadList :: ReadS [PingEvent]
readsPrec :: Int -> ReadS PingEvent
$creadsPrec :: Int -> ReadS PingEvent
Read)

instance Event PingEvent where
    typeName :: TypeName PingEvent
typeName = Text -> TypeName PingEvent
forall a. Text -> TypeName a
TypeName Text
"PingEvent"
    eventName :: EventName PingEvent
eventName = Text -> EventName PingEvent
forall a. Text -> EventName a
EventName Text
"ping"

instance FromJSON PingEvent where
    parseJSON :: Value -> Parser PingEvent
parseJSON (Object Object
x) = Maybe Installation
-> Organization -> User -> Hook -> Int -> Text -> PingEvent
PingEvent
        (Maybe Installation
 -> Organization -> User -> Hook -> Int -> Text -> PingEvent)
-> Parser (Maybe Installation)
-> Parser
     (Organization -> User -> Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
x Object -> Key -> Parser (Maybe Installation)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"installation"
        Parser (Organization -> User -> Hook -> Int -> Text -> PingEvent)
-> Parser Organization
-> Parser (User -> Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Organization
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"organization"
        Parser (User -> Hook -> Int -> Text -> PingEvent)
-> Parser User -> Parser (Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser User
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"sender"

        Parser (Hook -> Int -> Text -> PingEvent)
-> Parser Hook -> Parser (Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Hook
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hook"
        Parser (Int -> Text -> PingEvent)
-> Parser Int -> Parser (Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hook_id"
        Parser (Text -> PingEvent) -> Parser Text -> Parser PingEvent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"zen"

    parseJSON Value
_ = String -> Parser PingEvent
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"PingEvent"

instance ToJSON PingEvent where
    toJSON :: PingEvent -> Value
toJSON PingEvent{Int
Maybe Installation
Text
Hook
Organization
User
pingEventZen :: Text
pingEventHookId :: Int
pingEventHook :: Hook
pingEventSender :: User
pingEventOrganization :: Organization
pingEventInstallation :: Maybe Installation
pingEventZen :: PingEvent -> Text
pingEventHookId :: PingEvent -> Int
pingEventHook :: PingEvent -> Hook
pingEventSender :: PingEvent -> User
pingEventOrganization :: PingEvent -> Organization
pingEventInstallation :: PingEvent -> Maybe Installation
..} = [Pair] -> Value
object
        [ Key
"installation" Key -> Maybe Installation -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Installation
pingEventInstallation
        , Key
"organization" Key -> Organization -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Organization
pingEventOrganization
        , Key
"sender"       Key -> User -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= User
pingEventSender

        , Key
"hook"         Key -> Hook -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Hook
pingEventHook
        , Key
"hook_id"      Key -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
pingEventHookId
        , Key
"zen"          Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
pingEventZen
        ]


instance Arbitrary PingEvent where
    arbitrary :: Gen PingEvent
arbitrary = Maybe Installation
-> Organization -> User -> Hook -> Int -> Text -> PingEvent
PingEvent
        (Maybe Installation
 -> Organization -> User -> Hook -> Int -> Text -> PingEvent)
-> Gen (Maybe Installation)
-> Gen (Organization -> User -> Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Maybe Installation)
forall a. Arbitrary a => Gen a
arbitrary
        Gen (Organization -> User -> Hook -> Int -> Text -> PingEvent)
-> Gen Organization
-> Gen (User -> Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Organization
forall a. Arbitrary a => Gen a
arbitrary
        Gen (User -> Hook -> Int -> Text -> PingEvent)
-> Gen User -> Gen (Hook -> Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen User
forall a. Arbitrary a => Gen a
arbitrary

        Gen (Hook -> Int -> Text -> PingEvent)
-> Gen Hook -> Gen (Int -> Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Hook
forall a. Arbitrary a => Gen a
arbitrary
        Gen (Int -> Text -> PingEvent)
-> Gen Int -> Gen (Text -> PingEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Int
forall a. Arbitrary a => Gen a
arbitrary
        Gen (Text -> PingEvent) -> Gen Text -> Gen PingEvent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Text
forall a. Arbitrary a => Gen a
arbitrary