{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}
module GitHub.Types.Base.Author 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 (..))

------------------------------------------------------------------------------
-- Author

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


instance FromJSON Author where
    parseJSON :: Value -> Parser Author
parseJSON (Object Object
x) = Text -> Text -> Maybe Text -> Author
Author
        (Text -> Text -> Maybe Text -> Author)
-> Parser Text -> Parser (Text -> Maybe Text -> Author)
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 (Text -> Maybe Text -> Author)
-> Parser Text -> Parser (Maybe Text -> Author)
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
"email"
        Parser (Maybe Text -> Author)
-> Parser (Maybe Text) -> Parser Author
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 (Maybe a)
.:? Key
"username"

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


instance ToJSON Author where
    toJSON :: Author -> Value
toJSON Author{Maybe Text
Text
authorUsername :: Maybe Text
authorEmail :: Text
authorName :: Text
authorUsername :: Author -> Maybe Text
authorEmail :: Author -> Text
authorName :: Author -> Text
..} = [Pair] -> Value
object
        [ Key
"name"     Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
authorName
        , Key
"email"    Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
authorEmail
        , Key
"username" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
authorUsername
        ]

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