{-# LANGUAGE StrictData #-}
module GitHub.Types.Base.RepoOwner where

import           Control.Applicative       ((<|>))
import           Data.Aeson                (FromJSON (..), ToJSON (..))
import           Test.QuickCheck.Arbitrary (Arbitrary (..))
import qualified Test.QuickCheck.Gen       as Gen

import           GitHub.Types.Base.User
import           GitHub.Types.Base.UserRef

------------------------------------------------------------------------------
-- RepoOwner

data RepoOwner
    = RepoOwnerUser User
    | RepoOwnerUserRef UserRef
    deriving (RepoOwner -> RepoOwner -> Bool
(RepoOwner -> RepoOwner -> Bool)
-> (RepoOwner -> RepoOwner -> Bool) -> Eq RepoOwner
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RepoOwner -> RepoOwner -> Bool
$c/= :: RepoOwner -> RepoOwner -> Bool
== :: RepoOwner -> RepoOwner -> Bool
$c== :: RepoOwner -> RepoOwner -> Bool
Eq, Int -> RepoOwner -> ShowS
[RepoOwner] -> ShowS
RepoOwner -> String
(Int -> RepoOwner -> ShowS)
-> (RepoOwner -> String)
-> ([RepoOwner] -> ShowS)
-> Show RepoOwner
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RepoOwner] -> ShowS
$cshowList :: [RepoOwner] -> ShowS
show :: RepoOwner -> String
$cshow :: RepoOwner -> String
showsPrec :: Int -> RepoOwner -> ShowS
$cshowsPrec :: Int -> RepoOwner -> ShowS
Show, ReadPrec [RepoOwner]
ReadPrec RepoOwner
Int -> ReadS RepoOwner
ReadS [RepoOwner]
(Int -> ReadS RepoOwner)
-> ReadS [RepoOwner]
-> ReadPrec RepoOwner
-> ReadPrec [RepoOwner]
-> Read RepoOwner
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RepoOwner]
$creadListPrec :: ReadPrec [RepoOwner]
readPrec :: ReadPrec RepoOwner
$creadPrec :: ReadPrec RepoOwner
readList :: ReadS [RepoOwner]
$creadList :: ReadS [RepoOwner]
readsPrec :: Int -> ReadS RepoOwner
$creadsPrec :: Int -> ReadS RepoOwner
Read)


instance FromJSON RepoOwner where
    parseJSON :: Value -> Parser RepoOwner
parseJSON Value
x =
          User -> RepoOwner
RepoOwnerUser    (User -> RepoOwner) -> Parser User -> Parser RepoOwner
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser User
forall a. FromJSON a => Value -> Parser a
parseJSON Value
x
      Parser RepoOwner -> Parser RepoOwner -> Parser RepoOwner
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> UserRef -> RepoOwner
RepoOwnerUserRef (UserRef -> RepoOwner) -> Parser UserRef -> Parser RepoOwner
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser UserRef
forall a. FromJSON a => Value -> Parser a
parseJSON Value
x


instance ToJSON RepoOwner where
    toJSON :: RepoOwner -> Value
toJSON (RepoOwnerUser    User
x) = User -> Value
forall a. ToJSON a => a -> Value
toJSON User
x
    toJSON (RepoOwnerUserRef UserRef
x) = UserRef -> Value
forall a. ToJSON a => a -> Value
toJSON UserRef
x


instance Arbitrary RepoOwner where
    arbitrary :: Gen RepoOwner
arbitrary = [Gen RepoOwner] -> Gen RepoOwner
forall a. [Gen a] -> Gen a
Gen.oneof
      [ User -> RepoOwner
RepoOwnerUser    (User -> RepoOwner) -> Gen User -> Gen RepoOwner
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen User
forall a. Arbitrary a => Gen a
arbitrary
      , UserRef -> RepoOwner
RepoOwnerUserRef (UserRef -> RepoOwner) -> Gen UserRef -> Gen RepoOwner
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen UserRef
forall a. Arbitrary a => Gen a
arbitrary
      ]