{-# LANGUAGE NoImplicitPrelude  #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}

-- | Casa configuration types.


module Stack.Types.Casa
  ( CasaOptsMonoid (..)
  ) where

import           Data.Aeson.Types ( FromJSON (..) )
import           Data.Aeson.WarningParser
                   ( WithJSONWarnings, (..:?), withObjectWarnings )
import           Casa.Client ( CasaRepoPrefix )
import           Generics.Deriving.Monoid ( mappenddefault, memptydefault )
import           Stack.Prelude

-- | An uninterpreted representation of Casa configuration options.

-- Configurations may be "cascaded" using mappend (left-biased).

data CasaOptsMonoid = CasaOptsMonoid
  { CasaOptsMonoid -> FirstTrue
casaMonoidEnable :: !FirstTrue
  , CasaOptsMonoid -> First CasaRepoPrefix
casaMonoidRepoPrefix :: !(First CasaRepoPrefix)
  , CasaOptsMonoid -> First Int
casaMonoidMaxKeysPerRequest :: !(First Int)
  }
  deriving ((forall x. CasaOptsMonoid -> Rep CasaOptsMonoid x)
-> (forall x. Rep CasaOptsMonoid x -> CasaOptsMonoid)
-> Generic CasaOptsMonoid
forall x. Rep CasaOptsMonoid x -> CasaOptsMonoid
forall x. CasaOptsMonoid -> Rep CasaOptsMonoid x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CasaOptsMonoid -> Rep CasaOptsMonoid x
from :: forall x. CasaOptsMonoid -> Rep CasaOptsMonoid x
$cto :: forall x. Rep CasaOptsMonoid x -> CasaOptsMonoid
to :: forall x. Rep CasaOptsMonoid x -> CasaOptsMonoid
Generic, Int -> CasaOptsMonoid -> ShowS
[CasaOptsMonoid] -> ShowS
CasaOptsMonoid -> String
(Int -> CasaOptsMonoid -> ShowS)
-> (CasaOptsMonoid -> String)
-> ([CasaOptsMonoid] -> ShowS)
-> Show CasaOptsMonoid
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CasaOptsMonoid -> ShowS
showsPrec :: Int -> CasaOptsMonoid -> ShowS
$cshow :: CasaOptsMonoid -> String
show :: CasaOptsMonoid -> String
$cshowList :: [CasaOptsMonoid] -> ShowS
showList :: [CasaOptsMonoid] -> ShowS
Show)

-- | Decode uninterpreted Casa configuration options from JSON/YAML.

instance FromJSON (WithJSONWarnings CasaOptsMonoid) where
  parseJSON :: Value -> Parser (WithJSONWarnings CasaOptsMonoid)
parseJSON = String
-> (Object -> WarningParser CasaOptsMonoid)
-> Value
-> Parser (WithJSONWarnings CasaOptsMonoid)
forall a.
String
-> (Object -> WarningParser a)
-> Value
-> Parser (WithJSONWarnings a)
withObjectWarnings String
"CasaOptsMonoid"
    ( \Object
o -> do
        FirstTrue
casaMonoidEnable <- Maybe Bool -> FirstTrue
FirstTrue (Maybe Bool -> FirstTrue)
-> WriterT WarningParserMonoid Parser (Maybe Bool)
-> WriterT WarningParserMonoid Parser FirstTrue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
casaEnableName
        First CasaRepoPrefix
casaMonoidRepoPrefix <- Maybe CasaRepoPrefix -> First CasaRepoPrefix
forall a. Maybe a -> First a
First (Maybe CasaRepoPrefix -> First CasaRepoPrefix)
-> WriterT WarningParserMonoid Parser (Maybe CasaRepoPrefix)
-> WriterT WarningParserMonoid Parser (First CasaRepoPrefix)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object
-> Text
-> WriterT WarningParserMonoid Parser (Maybe CasaRepoPrefix)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
casaRepoPrefixName
        First Int
casaMonoidMaxKeysPerRequest <-
          Maybe Int -> First Int
forall a. Maybe a -> First a
First (Maybe Int -> First Int)
-> WriterT WarningParserMonoid Parser (Maybe Int)
-> WriterT WarningParserMonoid Parser (First Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe Int)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
casaMaxKeysPerRequestName
        CasaOptsMonoid -> WarningParser CasaOptsMonoid
forall a. a -> WriterT WarningParserMonoid Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CasaOptsMonoid {First Int
First CasaRepoPrefix
FirstTrue
casaMonoidEnable :: FirstTrue
casaMonoidRepoPrefix :: First CasaRepoPrefix
casaMonoidMaxKeysPerRequest :: First Int
casaMonoidEnable :: FirstTrue
casaMonoidRepoPrefix :: First CasaRepoPrefix
casaMonoidMaxKeysPerRequest :: First Int
..}
    )

-- | Left-biased combine Casa configuration options

instance Semigroup CasaOptsMonoid where
  <> :: CasaOptsMonoid -> CasaOptsMonoid -> CasaOptsMonoid
(<>) = CasaOptsMonoid -> CasaOptsMonoid -> CasaOptsMonoid
forall a. (Generic a, Monoid' (Rep a)) => a -> a -> a
mappenddefault

-- | Left-biased combine Casa configurations options

instance Monoid CasaOptsMonoid where
  mempty :: CasaOptsMonoid
mempty = CasaOptsMonoid
forall a. (Generic a, Monoid' (Rep a)) => a
memptydefault
  mappend :: CasaOptsMonoid -> CasaOptsMonoid -> CasaOptsMonoid
mappend = CasaOptsMonoid -> CasaOptsMonoid -> CasaOptsMonoid
forall a. Semigroup a => a -> a -> a
(<>)

-- | Casa configuration enable setting name.

casaEnableName :: Text
casaEnableName :: Text
casaEnableName = Text
"enable"

-- | Casa configuration repository prefix setting name.

casaRepoPrefixName :: Text
casaRepoPrefixName :: Text
casaRepoPrefixName = Text
"repo-prefix"

-- | Casa configuration maximum keys per request setting name.

casaMaxKeysPerRequestName :: Text
casaMaxKeysPerRequestName :: Text
casaMaxKeysPerRequestName = Text
"max-keys-per-request"