{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}
module GitHub.Types.Base.UserRef where

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

------------------------------------------------------------------------------
-- UserRef

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


instance FromJSON UserRef where
    parseJSON :: Value -> Parser UserRef
parseJSON (Object Object
x) = Text -> Maybe Text -> UserRef
UserRef
        (Text -> Maybe Text -> UserRef)
-> Parser Text -> Parser (Maybe Text -> UserRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
x Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
        Parser (Maybe Text -> UserRef)
-> Parser (Maybe Text) -> Parser UserRef
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"email"

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


instance ToJSON UserRef where
    toJSON :: UserRef -> Value
toJSON UserRef{Maybe Text
Text
userRefEmail :: Maybe Text
userRefName :: Text
userRefEmail :: UserRef -> Maybe Text
userRefName :: UserRef -> Text
..} = [Pair] -> Value
object
        [ Key
"name"  Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
userRefName
        , Key
"email" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
userRefEmail
        ]


instance Arbitrary UserRef where
    arbitrary :: Gen UserRef
arbitrary = Text -> Maybe Text -> UserRef
UserRef
        (Text -> Maybe Text -> UserRef)
-> Gen Text -> Gen (Maybe Text -> UserRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Text
forall a. Arbitrary a => Gen a
arbitrary
        Gen (Maybe Text -> UserRef) -> Gen (Maybe Text) -> Gen UserRef
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe Text)
forall a. Arbitrary a => Gen a
arbitrary