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

------------------------------------------------------------------------------
-- Branch

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


instance FromJSON Branch where
    parseJSON :: Value -> Parser Branch
parseJSON (Object Object
x) = Text -> Bool -> CommitRef -> Branch
Branch
        (Text -> Bool -> CommitRef -> Branch)
-> Parser Text -> Parser (Bool -> CommitRef -> Branch)
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 (Bool -> CommitRef -> Branch)
-> Parser Bool -> Parser (CommitRef -> Branch)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser Bool
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"protected"
        Parser (CommitRef -> Branch) -> Parser CommitRef -> Parser Branch
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Key -> Parser CommitRef
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"commit"

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


instance ToJSON Branch where
    toJSON :: Branch -> Value
toJSON Branch{Bool
Text
CommitRef
branchCommit :: CommitRef
branchProtected :: Bool
branchName :: Text
branchCommit :: Branch -> CommitRef
branchProtected :: Branch -> Bool
branchName :: Branch -> Text
..} = [Pair] -> Value
object
        [ Key
"name"      Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
branchName
        , Key
"protected" Key -> Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
branchProtected
        , Key
"commit"    Key -> CommitRef -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= CommitRef
branchCommit
        ]


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