{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}
module GitHub.Types.Events.LabelEvent 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 LabelEvent = LabelEvent
    { LabelEvent -> Maybe Installation
labelEventInstallation :: Maybe Installation
    , LabelEvent -> Organization
labelEventOrganization :: Organization
    , LabelEvent -> Repository
labelEventRepository   :: Repository
    , LabelEvent -> User
labelEventSender       :: User

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

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

instance FromJSON LabelEvent where
    parseJSON :: Value -> Parser LabelEvent
parseJSON (Object Object
x) = Maybe Installation
-> Organization
-> Repository
-> User
-> Text
-> Label
-> LabelEvent
LabelEvent
        (Maybe Installation
 -> Organization
 -> Repository
 -> User
 -> Text
 -> Label
 -> LabelEvent)
-> Parser (Maybe Installation)
-> Parser
     (Organization -> Repository -> User -> Text -> Label -> LabelEvent)
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 -> Repository -> User -> Text -> Label -> LabelEvent)
-> Parser Organization
-> Parser (Repository -> User -> Text -> Label -> LabelEvent)
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 (Repository -> User -> Text -> Label -> LabelEvent)
-> Parser Repository
-> Parser (User -> Text -> Label -> LabelEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Repository
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"repository"
        Parser (User -> Text -> Label -> LabelEvent)
-> Parser User -> Parser (Text -> Label -> LabelEvent)
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 (Text -> Label -> LabelEvent)
-> Parser Text -> Parser (Label -> LabelEvent)
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
"action"
        Parser (Label -> LabelEvent) -> Parser Label -> Parser LabelEvent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Label
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"label"

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

instance ToJSON LabelEvent where
    toJSON :: LabelEvent -> Value
toJSON LabelEvent{Maybe Installation
Text
Label
Organization
User
Repository
labelEventLabel :: Label
labelEventAction :: Text
labelEventSender :: User
labelEventRepository :: Repository
labelEventOrganization :: Organization
labelEventInstallation :: Maybe Installation
labelEventLabel :: LabelEvent -> Label
labelEventAction :: LabelEvent -> Text
labelEventSender :: LabelEvent -> User
labelEventRepository :: LabelEvent -> Repository
labelEventOrganization :: LabelEvent -> Organization
labelEventInstallation :: LabelEvent -> Maybe Installation
..} = [Pair] -> Value
object
        [ Key
"installation"     Key -> Maybe Installation -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Installation
labelEventInstallation
        , Key
"organization"     Key -> Organization -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Organization
labelEventOrganization
        , Key
"repository"       Key -> Repository -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Repository
labelEventRepository
        , Key
"sender"           Key -> User -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= User
labelEventSender

        , Key
"action"           Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
labelEventAction
        , Key
"label"            Key -> Label -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Label
labelEventLabel
        ]


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

        Gen (Text -> Label -> LabelEvent)
-> Gen Text -> Gen (Label -> LabelEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Text
forall a. Arbitrary a => Gen a
arbitrary
        Gen (Label -> LabelEvent) -> Gen Label -> Gen LabelEvent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Label
forall a. Arbitrary a => Gen a
arbitrary