module Hasura.RQL.Types.ApiLimit
  ( ApiLimit (..),
    DepthLimit,
    Limit (..),
    MaxDepth (..),
    MaxNodes (..),
    MaxTime (..),
    MaxBatchSize (..),
    NodeLimit,
    RateLimit,
    RateLimitConfig (..),
    TimeLimit,
    BatchLimit,
    UniqueParamConfig (..),
    emptyApiLimit,
  )
where

import Autodocodec
  ( HasCodec (codec),
    bimapCodec,
    dimapCodec,
    disjointEitherCodec,
    literalTextValueCodec,
    optionalField,
    optionalField',
    optionalFieldWithDefault',
    requiredField',
    requiredFieldWith',
  )
import Autodocodec qualified as AC
import Autodocodec.Extended (integralWithLowerBoundCodec, realFracWithLowerBoundCodec, typeableName)
import Control.Lens
import Data.Aeson
import Data.Aeson.Casing qualified as Casing
import Data.Map (Map)
import Data.Text qualified as T
import Data.Text.Extended (ToTxt (..))
import Data.Typeable (Typeable)
import Hasura.Prelude
import Hasura.RQL.Types.Roles (RoleName)
import Hasura.RQL.Types.Session (isSessionVariable)

data ApiLimit = ApiLimit
  { ApiLimit -> Maybe RateLimit
_alRateLimit :: Maybe RateLimit,
    ApiLimit -> Maybe DepthLimit
_alDepthLimit :: Maybe DepthLimit,
    ApiLimit -> Maybe NodeLimit
_alNodeLimit :: Maybe NodeLimit,
    ApiLimit -> Maybe TimeLimit
_alTimeLimit :: Maybe TimeLimit,
    ApiLimit -> Maybe BatchLimit
_alBatchLimit :: Maybe BatchLimit,
    ApiLimit -> Bool
_alDisabled :: Bool
  }
  deriving (Int -> ApiLimit -> ShowS
[ApiLimit] -> ShowS
ApiLimit -> String
(Int -> ApiLimit -> ShowS)
-> (ApiLimit -> String) -> ([ApiLimit] -> ShowS) -> Show ApiLimit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ApiLimit -> ShowS
showsPrec :: Int -> ApiLimit -> ShowS
$cshow :: ApiLimit -> String
show :: ApiLimit -> String
$cshowList :: [ApiLimit] -> ShowS
showList :: [ApiLimit] -> ShowS
Show, ApiLimit -> ApiLimit -> Bool
(ApiLimit -> ApiLimit -> Bool)
-> (ApiLimit -> ApiLimit -> Bool) -> Eq ApiLimit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ApiLimit -> ApiLimit -> Bool
== :: ApiLimit -> ApiLimit -> Bool
$c/= :: ApiLimit -> ApiLimit -> Bool
/= :: ApiLimit -> ApiLimit -> Bool
Eq, (forall x. ApiLimit -> Rep ApiLimit x)
-> (forall x. Rep ApiLimit x -> ApiLimit) -> Generic ApiLimit
forall x. Rep ApiLimit x -> ApiLimit
forall x. ApiLimit -> Rep ApiLimit x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ApiLimit -> Rep ApiLimit x
from :: forall x. ApiLimit -> Rep ApiLimit x
$cto :: forall x. Rep ApiLimit x -> ApiLimit
to :: forall x. Rep ApiLimit x -> ApiLimit
Generic)

instance HasCodec ApiLimit where
  codec :: JSONCodec ApiLimit
codec =
    Text -> ObjectCodec ApiLimit ApiLimit -> JSONCodec ApiLimit
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
AC.object Text
"ApiLimit"
      (ObjectCodec ApiLimit ApiLimit -> JSONCodec ApiLimit)
-> ObjectCodec ApiLimit ApiLimit -> JSONCodec ApiLimit
forall a b. (a -> b) -> a -> b
$ Maybe RateLimit
-> Maybe DepthLimit
-> Maybe NodeLimit
-> Maybe TimeLimit
-> Maybe BatchLimit
-> Bool
-> ApiLimit
ApiLimit
      (Maybe RateLimit
 -> Maybe DepthLimit
 -> Maybe NodeLimit
 -> Maybe TimeLimit
 -> Maybe BatchLimit
 -> Bool
 -> ApiLimit)
-> Codec Object ApiLimit (Maybe RateLimit)
-> Codec
     Object
     ApiLimit
     (Maybe DepthLimit
      -> Maybe NodeLimit
      -> Maybe TimeLimit
      -> Maybe BatchLimit
      -> Bool
      -> ApiLimit)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ObjectCodec (Maybe RateLimit) (Maybe RateLimit)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"rate_limit"
      ObjectCodec (Maybe RateLimit) (Maybe RateLimit)
-> (ApiLimit -> Maybe RateLimit)
-> Codec Object ApiLimit (Maybe RateLimit)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Maybe RateLimit
_alRateLimit
        Codec
  Object
  ApiLimit
  (Maybe DepthLimit
   -> Maybe NodeLimit
   -> Maybe TimeLimit
   -> Maybe BatchLimit
   -> Bool
   -> ApiLimit)
-> Codec Object ApiLimit (Maybe DepthLimit)
-> Codec
     Object
     ApiLimit
     (Maybe NodeLimit
      -> Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
forall a b.
Codec Object ApiLimit (a -> b)
-> Codec Object ApiLimit a -> Codec Object ApiLimit b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (Maybe DepthLimit) (Maybe DepthLimit)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"depth_limit"
      ObjectCodec (Maybe DepthLimit) (Maybe DepthLimit)
-> (ApiLimit -> Maybe DepthLimit)
-> Codec Object ApiLimit (Maybe DepthLimit)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Maybe DepthLimit
_alDepthLimit
        Codec
  Object
  ApiLimit
  (Maybe NodeLimit
   -> Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
-> Codec Object ApiLimit (Maybe NodeLimit)
-> Codec
     Object
     ApiLimit
     (Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
forall a b.
Codec Object ApiLimit (a -> b)
-> Codec Object ApiLimit a -> Codec Object ApiLimit b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (Maybe NodeLimit) (Maybe NodeLimit)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"node_limit"
      ObjectCodec (Maybe NodeLimit) (Maybe NodeLimit)
-> (ApiLimit -> Maybe NodeLimit)
-> Codec Object ApiLimit (Maybe NodeLimit)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Maybe NodeLimit
_alNodeLimit
        Codec
  Object
  ApiLimit
  (Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
-> Codec Object ApiLimit (Maybe TimeLimit)
-> Codec Object ApiLimit (Maybe BatchLimit -> Bool -> ApiLimit)
forall a b.
Codec Object ApiLimit (a -> b)
-> Codec Object ApiLimit a -> Codec Object ApiLimit b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (Maybe TimeLimit) (Maybe TimeLimit)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"time_limit"
      ObjectCodec (Maybe TimeLimit) (Maybe TimeLimit)
-> (ApiLimit -> Maybe TimeLimit)
-> Codec Object ApiLimit (Maybe TimeLimit)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Maybe TimeLimit
_alTimeLimit
        Codec Object ApiLimit (Maybe BatchLimit -> Bool -> ApiLimit)
-> Codec Object ApiLimit (Maybe BatchLimit)
-> Codec Object ApiLimit (Bool -> ApiLimit)
forall a b.
Codec Object ApiLimit (a -> b)
-> Codec Object ApiLimit a -> Codec Object ApiLimit b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (Maybe BatchLimit) (Maybe BatchLimit)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"batch_limit"
      ObjectCodec (Maybe BatchLimit) (Maybe BatchLimit)
-> (ApiLimit -> Maybe BatchLimit)
-> Codec Object ApiLimit (Maybe BatchLimit)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Maybe BatchLimit
_alBatchLimit
        Codec Object ApiLimit (Bool -> ApiLimit)
-> Codec Object ApiLimit Bool -> ObjectCodec ApiLimit ApiLimit
forall a b.
Codec Object ApiLimit (a -> b)
-> Codec Object ApiLimit a -> Codec Object ApiLimit b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Bool -> ObjectCodec Bool Bool
forall output.
HasCodec output =>
Text -> output -> ObjectCodec output output
optionalFieldWithDefault' Text
"disabled" Bool
False
      ObjectCodec Bool Bool
-> (ApiLimit -> Bool) -> Codec Object ApiLimit Bool
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= ApiLimit -> Bool
_alDisabled

instance FromJSON ApiLimit where
  parseJSON :: Value -> Parser ApiLimit
parseJSON = String -> (Object -> Parser ApiLimit) -> Value -> Parser ApiLimit
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ApiLimit" ((Object -> Parser ApiLimit) -> Value -> Parser ApiLimit)
-> (Object -> Parser ApiLimit) -> Value -> Parser ApiLimit
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    Maybe RateLimit
-> Maybe DepthLimit
-> Maybe NodeLimit
-> Maybe TimeLimit
-> Maybe BatchLimit
-> Bool
-> ApiLimit
ApiLimit
      (Maybe RateLimit
 -> Maybe DepthLimit
 -> Maybe NodeLimit
 -> Maybe TimeLimit
 -> Maybe BatchLimit
 -> Bool
 -> ApiLimit)
-> Parser (Maybe RateLimit)
-> Parser
     (Maybe DepthLimit
      -> Maybe NodeLimit
      -> Maybe TimeLimit
      -> Maybe BatchLimit
      -> Bool
      -> ApiLimit)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o
      Object -> Key -> Parser (Maybe RateLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"rate_limit"
      Parser
  (Maybe DepthLimit
   -> Maybe NodeLimit
   -> Maybe TimeLimit
   -> Maybe BatchLimit
   -> Bool
   -> ApiLimit)
-> Parser (Maybe DepthLimit)
-> Parser
     (Maybe NodeLimit
      -> Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe DepthLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"depth_limit"
      Parser
  (Maybe NodeLimit
   -> Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
-> Parser (Maybe NodeLimit)
-> Parser (Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe NodeLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"node_limit"
      Parser (Maybe TimeLimit -> Maybe BatchLimit -> Bool -> ApiLimit)
-> Parser (Maybe TimeLimit)
-> Parser (Maybe BatchLimit -> Bool -> ApiLimit)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe TimeLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"time_limit"
      Parser (Maybe BatchLimit -> Bool -> ApiLimit)
-> Parser (Maybe BatchLimit) -> Parser (Bool -> ApiLimit)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe BatchLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"batch_limit"
      Parser (Bool -> ApiLimit) -> Parser Bool -> Parser ApiLimit
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"disabled"
      Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False

instance ToJSON ApiLimit where
  toJSON :: ApiLimit -> Value
toJSON =
    Options -> ApiLimit -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (ShowS -> Options
Casing.aesonPrefix ShowS
Casing.snakeCase) {omitNothingFields :: Bool
omitNothingFields = Bool
True}

emptyApiLimit :: ApiLimit
emptyApiLimit :: ApiLimit
emptyApiLimit = Maybe RateLimit
-> Maybe DepthLimit
-> Maybe NodeLimit
-> Maybe TimeLimit
-> Maybe BatchLimit
-> Bool
-> ApiLimit
ApiLimit Maybe RateLimit
forall a. Maybe a
Nothing Maybe DepthLimit
forall a. Maybe a
Nothing Maybe NodeLimit
forall a. Maybe a
Nothing Maybe TimeLimit
forall a. Maybe a
Nothing Maybe BatchLimit
forall a. Maybe a
Nothing Bool
False

data Limit a = Limit
  { forall a. Limit a -> a
_lGlobal :: a,
    forall a. Limit a -> Map RoleName a
_lPerRole :: Map RoleName a
  }
  deriving (Int -> Limit a -> ShowS
[Limit a] -> ShowS
Limit a -> String
(Int -> Limit a -> ShowS)
-> (Limit a -> String) -> ([Limit a] -> ShowS) -> Show (Limit a)
forall a. Show a => Int -> Limit a -> ShowS
forall a. Show a => [Limit a] -> ShowS
forall a. Show a => Limit a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Limit a -> ShowS
showsPrec :: Int -> Limit a -> ShowS
$cshow :: forall a. Show a => Limit a -> String
show :: Limit a -> String
$cshowList :: forall a. Show a => [Limit a] -> ShowS
showList :: [Limit a] -> ShowS
Show, Limit a -> Limit a -> Bool
(Limit a -> Limit a -> Bool)
-> (Limit a -> Limit a -> Bool) -> Eq (Limit a)
forall a. Eq a => Limit a -> Limit a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Limit a -> Limit a -> Bool
== :: Limit a -> Limit a -> Bool
$c/= :: forall a. Eq a => Limit a -> Limit a -> Bool
/= :: Limit a -> Limit a -> Bool
Eq, (forall x. Limit a -> Rep (Limit a) x)
-> (forall x. Rep (Limit a) x -> Limit a) -> Generic (Limit a)
forall x. Rep (Limit a) x -> Limit a
forall x. Limit a -> Rep (Limit a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Limit a) x -> Limit a
forall a x. Limit a -> Rep (Limit a) x
$cfrom :: forall a x. Limit a -> Rep (Limit a) x
from :: forall x. Limit a -> Rep (Limit a) x
$cto :: forall a x. Rep (Limit a) x -> Limit a
to :: forall x. Rep (Limit a) x -> Limit a
Generic)

instance (HasCodec a, Typeable a) => HasCodec (Limit a) where
  codec :: JSONCodec (Limit a)
codec =
    Text -> ObjectCodec (Limit a) (Limit a) -> JSONCodec (Limit a)
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
AC.object (Text
"Limit_" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> forall a. Typeable a => Text
forall {k} (a :: k). Typeable a => Text
typeableName @a)
      (ObjectCodec (Limit a) (Limit a) -> JSONCodec (Limit a))
-> ObjectCodec (Limit a) (Limit a) -> JSONCodec (Limit a)
forall a b. (a -> b) -> a -> b
$ a -> Map RoleName a -> Limit a
forall a. a -> Map RoleName a -> Limit a
Limit
      (a -> Map RoleName a -> Limit a)
-> Codec Object (Limit a) a
-> Codec Object (Limit a) (Map RoleName a -> Limit a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ObjectCodec a a
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"global"
      ObjectCodec a a -> (Limit a -> a) -> Codec Object (Limit a) a
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= Limit a -> a
forall a. Limit a -> a
_lGlobal
        Codec Object (Limit a) (Map RoleName a -> Limit a)
-> Codec Object (Limit a) (Map RoleName a)
-> ObjectCodec (Limit a) (Limit a)
forall a b.
Codec Object (Limit a) (a -> b)
-> Codec Object (Limit a) a -> Codec Object (Limit a) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> Map RoleName a -> ObjectCodec (Map RoleName a) (Map RoleName a)
forall output.
HasCodec output =>
Text -> output -> ObjectCodec output output
optionalFieldWithDefault' Text
"per_role" Map RoleName a
forall a. Monoid a => a
mempty
      ObjectCodec (Map RoleName a) (Map RoleName a)
-> (Limit a -> Map RoleName a)
-> Codec Object (Limit a) (Map RoleName a)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= Limit a -> Map RoleName a
forall a. Limit a -> Map RoleName a
_lPerRole

instance (FromJSON a) => FromJSON (Limit a) where
  parseJSON :: Value -> Parser (Limit a)
parseJSON = String -> (Object -> Parser (Limit a)) -> Value -> Parser (Limit a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Limit" ((Object -> Parser (Limit a)) -> Value -> Parser (Limit a))
-> (Object -> Parser (Limit a)) -> Value -> Parser (Limit a)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    a -> Map RoleName a -> Limit a
forall a. a -> Map RoleName a -> Limit a
Limit (a -> Map RoleName a -> Limit a)
-> Parser a -> Parser (Map RoleName a -> Limit a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser a
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"global" Parser (Map RoleName a -> Limit a)
-> Parser (Map RoleName a) -> Parser (Limit a)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe (Map RoleName a))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"per_role" Parser (Maybe (Map RoleName a))
-> Map RoleName a -> Parser (Map RoleName a)
forall a. Parser (Maybe a) -> a -> Parser a
.!= Map RoleName a
forall a. Monoid a => a
mempty

instance (ToJSON a) => ToJSON (Limit a) where
  toJSON :: Limit a -> Value
toJSON =
    Options -> Limit a -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (ShowS -> Options
Casing.aesonPrefix ShowS
Casing.snakeCase)

type RateLimit = Limit RateLimitConfig

type DepthLimit = Limit MaxDepth

type NodeLimit = Limit MaxNodes

type TimeLimit = Limit MaxTime

type BatchLimit = Limit MaxBatchSize

data RateLimitConfig = RateLimitConfig
  { RateLimitConfig -> Int
_rlcMaxReqsPerMin :: Int,
    RateLimitConfig -> Maybe UniqueParamConfig
_rlcUniqueParams :: Maybe UniqueParamConfig
  }
  deriving (Int -> RateLimitConfig -> ShowS
[RateLimitConfig] -> ShowS
RateLimitConfig -> String
(Int -> RateLimitConfig -> ShowS)
-> (RateLimitConfig -> String)
-> ([RateLimitConfig] -> ShowS)
-> Show RateLimitConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RateLimitConfig -> ShowS
showsPrec :: Int -> RateLimitConfig -> ShowS
$cshow :: RateLimitConfig -> String
show :: RateLimitConfig -> String
$cshowList :: [RateLimitConfig] -> ShowS
showList :: [RateLimitConfig] -> ShowS
Show, RateLimitConfig -> RateLimitConfig -> Bool
(RateLimitConfig -> RateLimitConfig -> Bool)
-> (RateLimitConfig -> RateLimitConfig -> Bool)
-> Eq RateLimitConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RateLimitConfig -> RateLimitConfig -> Bool
== :: RateLimitConfig -> RateLimitConfig -> Bool
$c/= :: RateLimitConfig -> RateLimitConfig -> Bool
/= :: RateLimitConfig -> RateLimitConfig -> Bool
Eq, (forall x. RateLimitConfig -> Rep RateLimitConfig x)
-> (forall x. Rep RateLimitConfig x -> RateLimitConfig)
-> Generic RateLimitConfig
forall x. Rep RateLimitConfig x -> RateLimitConfig
forall x. RateLimitConfig -> Rep RateLimitConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RateLimitConfig -> Rep RateLimitConfig x
from :: forall x. RateLimitConfig -> Rep RateLimitConfig x
$cto :: forall x. Rep RateLimitConfig x -> RateLimitConfig
to :: forall x. Rep RateLimitConfig x -> RateLimitConfig
Generic)

instance HasCodec RateLimitConfig where
  codec :: JSONCodec RateLimitConfig
codec =
    Text
-> ObjectCodec RateLimitConfig RateLimitConfig
-> JSONCodec RateLimitConfig
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
AC.object Text
"RateLimitConfig"
      (ObjectCodec RateLimitConfig RateLimitConfig
 -> JSONCodec RateLimitConfig)
-> ObjectCodec RateLimitConfig RateLimitConfig
-> JSONCodec RateLimitConfig
forall a b. (a -> b) -> a -> b
$ Int -> Maybe UniqueParamConfig -> RateLimitConfig
RateLimitConfig
      (Int -> Maybe UniqueParamConfig -> RateLimitConfig)
-> Codec Object RateLimitConfig Int
-> Codec
     Object RateLimitConfig (Maybe UniqueParamConfig -> RateLimitConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ValueCodec Int Int -> ObjectCodec Int Int
forall input output.
Text -> ValueCodec input output -> ObjectCodec input output
requiredFieldWith' Text
"max_reqs_per_min" (Int -> ValueCodec Int Int
forall i. (Integral i, Bounded i) => i -> JSONCodec i
integralWithLowerBoundCodec Int
0)
      ObjectCodec Int Int
-> (RateLimitConfig -> Int) -> Codec Object RateLimitConfig Int
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= RateLimitConfig -> Int
_rlcMaxReqsPerMin
        Codec
  Object RateLimitConfig (Maybe UniqueParamConfig -> RateLimitConfig)
-> Codec Object RateLimitConfig (Maybe UniqueParamConfig)
-> ObjectCodec RateLimitConfig RateLimitConfig
forall a b.
Codec Object RateLimitConfig (a -> b)
-> Codec Object RateLimitConfig a -> Codec Object RateLimitConfig b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> Text
-> ObjectCodec (Maybe UniqueParamConfig) (Maybe UniqueParamConfig)
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField Text
"unique_params" Text
"This would be either fixed value \"IP\" or a list of Session variables"
      ObjectCodec (Maybe UniqueParamConfig) (Maybe UniqueParamConfig)
-> (RateLimitConfig -> Maybe UniqueParamConfig)
-> Codec Object RateLimitConfig (Maybe UniqueParamConfig)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
AC..= RateLimitConfig -> Maybe UniqueParamConfig
_rlcUniqueParams

instance FromJSON RateLimitConfig where
  parseJSON :: Value -> Parser RateLimitConfig
parseJSON =
    Options -> Value -> Parser RateLimitConfig
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (ShowS -> Options
Casing.aesonPrefix ShowS
Casing.snakeCase)

instance ToJSON RateLimitConfig where
  toJSON :: RateLimitConfig -> Value
toJSON =
    Options -> RateLimitConfig -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (ShowS -> Options
Casing.aesonPrefix ShowS
Casing.snakeCase)

-- | The unique key using which an authenticated client can be identified
data UniqueParamConfig
  = -- | it can be a list of session variable (like session var in 'UserInfo')
    UPCSessionVar [Text]
  | -- | or it can be an IP address
    UPCIpAddress
  deriving (Int -> UniqueParamConfig -> ShowS
[UniqueParamConfig] -> ShowS
UniqueParamConfig -> String
(Int -> UniqueParamConfig -> ShowS)
-> (UniqueParamConfig -> String)
-> ([UniqueParamConfig] -> ShowS)
-> Show UniqueParamConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UniqueParamConfig -> ShowS
showsPrec :: Int -> UniqueParamConfig -> ShowS
$cshow :: UniqueParamConfig -> String
show :: UniqueParamConfig -> String
$cshowList :: [UniqueParamConfig] -> ShowS
showList :: [UniqueParamConfig] -> ShowS
Show, UniqueParamConfig -> UniqueParamConfig -> Bool
(UniqueParamConfig -> UniqueParamConfig -> Bool)
-> (UniqueParamConfig -> UniqueParamConfig -> Bool)
-> Eq UniqueParamConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UniqueParamConfig -> UniqueParamConfig -> Bool
== :: UniqueParamConfig -> UniqueParamConfig -> Bool
$c/= :: UniqueParamConfig -> UniqueParamConfig -> Bool
/= :: UniqueParamConfig -> UniqueParamConfig -> Bool
Eq, (forall x. UniqueParamConfig -> Rep UniqueParamConfig x)
-> (forall x. Rep UniqueParamConfig x -> UniqueParamConfig)
-> Generic UniqueParamConfig
forall x. Rep UniqueParamConfig x -> UniqueParamConfig
forall x. UniqueParamConfig -> Rep UniqueParamConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UniqueParamConfig -> Rep UniqueParamConfig x
from :: forall x. UniqueParamConfig -> Rep UniqueParamConfig x
$cto :: forall x. Rep UniqueParamConfig x -> UniqueParamConfig
to :: forall x. Rep UniqueParamConfig x -> UniqueParamConfig
Generic)

instance HasCodec UniqueParamConfig where
  codec :: JSONCodec UniqueParamConfig
codec = (Either () [Text] -> Either String UniqueParamConfig)
-> (UniqueParamConfig -> Either () [Text])
-> Codec Value (Either () [Text]) (Either () [Text])
-> JSONCodec UniqueParamConfig
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> Either String newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
bimapCodec Either () [Text] -> Either String UniqueParamConfig
forall {a} {a}.
IsString a =>
Either a [Text] -> Either a UniqueParamConfig
dec UniqueParamConfig -> Either () [Text]
enc (Codec Value (Either () [Text]) (Either () [Text])
 -> JSONCodec UniqueParamConfig)
-> Codec Value (Either () [Text]) (Either () [Text])
-> JSONCodec UniqueParamConfig
forall a b. (a -> b) -> a -> b
$ Codec Value () ()
-> Codec Value [Text] [Text]
-> Codec Value (Either () [Text]) (Either () [Text])
forall context input1 output1 input2 output2.
Codec context input1 output1
-> Codec context input2 output2
-> Codec context (Either input1 input2) (Either output1 output2)
disjointEitherCodec Codec Value () ()
ipAddress Codec Value [Text] [Text]
sessionVariables
    where
      ipAddress :: Codec Value () ()
ipAddress =
        (Either () () -> ())
-> (() -> Either () ())
-> Codec Value (Either () ()) (Either () ())
-> Codec Value () ()
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec Either () () -> ()
forall {c}. Either c c -> c
fromEither () -> Either () ()
forall a b. a -> Either a b
Left
          (Codec Value (Either () ()) (Either () ()) -> Codec Value () ())
-> Codec Value (Either () ()) (Either () ()) -> Codec Value () ()
forall a b. (a -> b) -> a -> b
$ Codec Value () ()
-> Codec Value () () -> Codec Value (Either () ()) (Either () ())
forall context input1 output1 input2 output2.
Codec context input1 output1
-> Codec context input2 output2
-> Codec context (Either input1 input2) (Either output1 output2)
disjointEitherCodec
            (() -> Text -> Codec Value () ()
forall value. value -> Text -> JSONCodec value
literalTextValueCodec () Text
"IP")
            (() -> Text -> Codec Value () ()
forall value. value -> Text -> JSONCodec value
literalTextValueCodec () Text
"ip")
      sessionVariables :: Codec Value [Text] [Text]
sessionVariables = Codec Value [Text] [Text]
forall value. HasCodec value => JSONCodec value
codec

      dec :: Either a [Text] -> Either a UniqueParamConfig
dec (Left a
_) = UniqueParamConfig -> Either a UniqueParamConfig
forall a b. b -> Either a b
Right UniqueParamConfig
UPCIpAddress
      dec (Right [Text]
xs) = [Text] -> UniqueParamConfig
UPCSessionVar ([Text] -> UniqueParamConfig)
-> Either a [Text] -> Either a UniqueParamConfig
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Either a Text) -> [Text] -> Either a [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Text -> Either a Text
forall {a}. IsString a => Text -> Either a Text
parseSessVar [Text]
xs

      parseSessVar :: Text -> Either a Text
parseSessVar Text
s
        | Text -> Bool
isSessionVariable Text
s Bool -> Bool -> Bool
&& Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"x-hasura-role" = Text -> Either a Text
forall a b. b -> Either a b
Right Text
s
        | Bool
otherwise = a -> Either a Text
forall a b. a -> Either a b
Left a
"Not a valid value. Should be either: 'IP' or a list of Hasura session variables"

      enc :: UniqueParamConfig -> Either () [Text]
enc UniqueParamConfig
UPCIpAddress = () -> Either () [Text]
forall a b. a -> Either a b
Left ()
      enc (UPCSessionVar [Text]
xs) = [Text] -> Either () [Text]
forall a b. b -> Either a b
Right [Text]
xs

      fromEither :: Either c c -> c
fromEither = (c -> c) -> (c -> c) -> Either c c -> c
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either c -> c
forall a. a -> a
id c -> c
forall a. a -> a
id

instance ToJSON UniqueParamConfig where
  toJSON :: UniqueParamConfig -> Value
toJSON = \case
    UPCSessionVar [Text]
xs -> [Text] -> Value
forall a. ToJSON a => a -> Value
toJSON [Text]
xs
    UniqueParamConfig
UPCIpAddress -> Value
"IP"

instance FromJSON UniqueParamConfig where
  parseJSON :: Value -> Parser UniqueParamConfig
parseJSON = \case
    String Text
v -> case Text -> Text
T.toLower Text
v of
      Text
"ip" -> UniqueParamConfig -> Parser UniqueParamConfig
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure UniqueParamConfig
UPCIpAddress
      Text
_ -> String -> Parser UniqueParamConfig
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg
    Array Array
xs -> (Value -> Parser Text) -> Array -> Parser (Vector Text)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Vector a -> f (Vector b)
traverse Value -> Parser Text
parseSessVar Array
xs Parser (Vector Text)
-> (Vector Text -> UniqueParamConfig) -> Parser UniqueParamConfig
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> [Text] -> UniqueParamConfig
UPCSessionVar ([Text] -> UniqueParamConfig)
-> (Vector Text -> [Text]) -> Vector Text -> UniqueParamConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Text -> [Text]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
    Value
_ -> String -> Parser UniqueParamConfig
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg
    where
      parseSessVar :: Value -> Parser Text
parseSessVar = \case
        String Text
s
          | Text -> Bool
isSessionVariable Text
s Bool -> Bool -> Bool
&& Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"x-hasura-role" -> Text -> Parser Text
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
s
          | Bool
otherwise -> String -> Parser Text
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg
        Value
_ -> String -> Parser Text
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMsg
      errMsg :: String
errMsg = String
"Not a valid value. Should be either: 'IP' or a list of Hasura session variables"

newtype MaxDepth = MaxDepth {MaxDepth -> Int
unMaxDepth :: Int}
  deriving stock (Int -> MaxDepth -> ShowS
[MaxDepth] -> ShowS
MaxDepth -> String
(Int -> MaxDepth -> ShowS)
-> (MaxDepth -> String) -> ([MaxDepth] -> ShowS) -> Show MaxDepth
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MaxDepth -> ShowS
showsPrec :: Int -> MaxDepth -> ShowS
$cshow :: MaxDepth -> String
show :: MaxDepth -> String
$cshowList :: [MaxDepth] -> ShowS
showList :: [MaxDepth] -> ShowS
Show, MaxDepth -> MaxDepth -> Bool
(MaxDepth -> MaxDepth -> Bool)
-> (MaxDepth -> MaxDepth -> Bool) -> Eq MaxDepth
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaxDepth -> MaxDepth -> Bool
== :: MaxDepth -> MaxDepth -> Bool
$c/= :: MaxDepth -> MaxDepth -> Bool
/= :: MaxDepth -> MaxDepth -> Bool
Eq, Eq MaxDepth
Eq MaxDepth
-> (MaxDepth -> MaxDepth -> Ordering)
-> (MaxDepth -> MaxDepth -> Bool)
-> (MaxDepth -> MaxDepth -> Bool)
-> (MaxDepth -> MaxDepth -> Bool)
-> (MaxDepth -> MaxDepth -> Bool)
-> (MaxDepth -> MaxDepth -> MaxDepth)
-> (MaxDepth -> MaxDepth -> MaxDepth)
-> Ord MaxDepth
MaxDepth -> MaxDepth -> Bool
MaxDepth -> MaxDepth -> Ordering
MaxDepth -> MaxDepth -> MaxDepth
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MaxDepth -> MaxDepth -> Ordering
compare :: MaxDepth -> MaxDepth -> Ordering
$c< :: MaxDepth -> MaxDepth -> Bool
< :: MaxDepth -> MaxDepth -> Bool
$c<= :: MaxDepth -> MaxDepth -> Bool
<= :: MaxDepth -> MaxDepth -> Bool
$c> :: MaxDepth -> MaxDepth -> Bool
> :: MaxDepth -> MaxDepth -> Bool
$c>= :: MaxDepth -> MaxDepth -> Bool
>= :: MaxDepth -> MaxDepth -> Bool
$cmax :: MaxDepth -> MaxDepth -> MaxDepth
max :: MaxDepth -> MaxDepth -> MaxDepth
$cmin :: MaxDepth -> MaxDepth -> MaxDepth
min :: MaxDepth -> MaxDepth -> MaxDepth
Ord, (forall x. MaxDepth -> Rep MaxDepth x)
-> (forall x. Rep MaxDepth x -> MaxDepth) -> Generic MaxDepth
forall x. Rep MaxDepth x -> MaxDepth
forall x. MaxDepth -> Rep MaxDepth x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MaxDepth -> Rep MaxDepth x
from :: forall x. MaxDepth -> Rep MaxDepth x
$cto :: forall x. Rep MaxDepth x -> MaxDepth
to :: forall x. Rep MaxDepth x -> MaxDepth
Generic)
  deriving newtype ([MaxDepth] -> Value
[MaxDepth] -> Encoding
MaxDepth -> Value
MaxDepth -> Encoding
(MaxDepth -> Value)
-> (MaxDepth -> Encoding)
-> ([MaxDepth] -> Value)
-> ([MaxDepth] -> Encoding)
-> ToJSON MaxDepth
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: MaxDepth -> Value
toJSON :: MaxDepth -> Value
$ctoEncoding :: MaxDepth -> Encoding
toEncoding :: MaxDepth -> Encoding
$ctoJSONList :: [MaxDepth] -> Value
toJSONList :: [MaxDepth] -> Value
$ctoEncodingList :: [MaxDepth] -> Encoding
toEncodingList :: [MaxDepth] -> Encoding
ToJSON, Value -> Parser [MaxDepth]
Value -> Parser MaxDepth
(Value -> Parser MaxDepth)
-> (Value -> Parser [MaxDepth]) -> FromJSON MaxDepth
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser MaxDepth
parseJSON :: Value -> Parser MaxDepth
$cparseJSONList :: Value -> Parser [MaxDepth]
parseJSONList :: Value -> Parser [MaxDepth]
FromJSON)

instance HasCodec MaxDepth where
  codec :: JSONCodec MaxDepth
codec =
    (Int -> MaxDepth)
-> (MaxDepth -> Int) -> ValueCodec Int Int -> JSONCodec MaxDepth
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec Int -> MaxDepth
MaxDepth MaxDepth -> Int
unMaxDepth
      (ValueCodec Int Int -> JSONCodec MaxDepth)
-> ValueCodec Int Int -> JSONCodec MaxDepth
forall a b. (a -> b) -> a -> b
$ Int -> ValueCodec Int Int
forall i. (Integral i, Bounded i) => i -> JSONCodec i
integralWithLowerBoundCodec Int
0

newtype MaxNodes = MaxNodes {MaxNodes -> Int
unMaxNodes :: Int}
  deriving stock (Int -> MaxNodes -> ShowS
[MaxNodes] -> ShowS
MaxNodes -> String
(Int -> MaxNodes -> ShowS)
-> (MaxNodes -> String) -> ([MaxNodes] -> ShowS) -> Show MaxNodes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MaxNodes -> ShowS
showsPrec :: Int -> MaxNodes -> ShowS
$cshow :: MaxNodes -> String
show :: MaxNodes -> String
$cshowList :: [MaxNodes] -> ShowS
showList :: [MaxNodes] -> ShowS
Show, MaxNodes -> MaxNodes -> Bool
(MaxNodes -> MaxNodes -> Bool)
-> (MaxNodes -> MaxNodes -> Bool) -> Eq MaxNodes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaxNodes -> MaxNodes -> Bool
== :: MaxNodes -> MaxNodes -> Bool
$c/= :: MaxNodes -> MaxNodes -> Bool
/= :: MaxNodes -> MaxNodes -> Bool
Eq, Eq MaxNodes
Eq MaxNodes
-> (MaxNodes -> MaxNodes -> Ordering)
-> (MaxNodes -> MaxNodes -> Bool)
-> (MaxNodes -> MaxNodes -> Bool)
-> (MaxNodes -> MaxNodes -> Bool)
-> (MaxNodes -> MaxNodes -> Bool)
-> (MaxNodes -> MaxNodes -> MaxNodes)
-> (MaxNodes -> MaxNodes -> MaxNodes)
-> Ord MaxNodes
MaxNodes -> MaxNodes -> Bool
MaxNodes -> MaxNodes -> Ordering
MaxNodes -> MaxNodes -> MaxNodes
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MaxNodes -> MaxNodes -> Ordering
compare :: MaxNodes -> MaxNodes -> Ordering
$c< :: MaxNodes -> MaxNodes -> Bool
< :: MaxNodes -> MaxNodes -> Bool
$c<= :: MaxNodes -> MaxNodes -> Bool
<= :: MaxNodes -> MaxNodes -> Bool
$c> :: MaxNodes -> MaxNodes -> Bool
> :: MaxNodes -> MaxNodes -> Bool
$c>= :: MaxNodes -> MaxNodes -> Bool
>= :: MaxNodes -> MaxNodes -> Bool
$cmax :: MaxNodes -> MaxNodes -> MaxNodes
max :: MaxNodes -> MaxNodes -> MaxNodes
$cmin :: MaxNodes -> MaxNodes -> MaxNodes
min :: MaxNodes -> MaxNodes -> MaxNodes
Ord, (forall x. MaxNodes -> Rep MaxNodes x)
-> (forall x. Rep MaxNodes x -> MaxNodes) -> Generic MaxNodes
forall x. Rep MaxNodes x -> MaxNodes
forall x. MaxNodes -> Rep MaxNodes x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MaxNodes -> Rep MaxNodes x
from :: forall x. MaxNodes -> Rep MaxNodes x
$cto :: forall x. Rep MaxNodes x -> MaxNodes
to :: forall x. Rep MaxNodes x -> MaxNodes
Generic)
  deriving newtype ([MaxNodes] -> Value
[MaxNodes] -> Encoding
MaxNodes -> Value
MaxNodes -> Encoding
(MaxNodes -> Value)
-> (MaxNodes -> Encoding)
-> ([MaxNodes] -> Value)
-> ([MaxNodes] -> Encoding)
-> ToJSON MaxNodes
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: MaxNodes -> Value
toJSON :: MaxNodes -> Value
$ctoEncoding :: MaxNodes -> Encoding
toEncoding :: MaxNodes -> Encoding
$ctoJSONList :: [MaxNodes] -> Value
toJSONList :: [MaxNodes] -> Value
$ctoEncodingList :: [MaxNodes] -> Encoding
toEncodingList :: [MaxNodes] -> Encoding
ToJSON, Value -> Parser [MaxNodes]
Value -> Parser MaxNodes
(Value -> Parser MaxNodes)
-> (Value -> Parser [MaxNodes]) -> FromJSON MaxNodes
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser MaxNodes
parseJSON :: Value -> Parser MaxNodes
$cparseJSONList :: Value -> Parser [MaxNodes]
parseJSONList :: Value -> Parser [MaxNodes]
FromJSON)

instance HasCodec MaxNodes where
  codec :: JSONCodec MaxNodes
codec =
    (Int -> MaxNodes)
-> (MaxNodes -> Int) -> ValueCodec Int Int -> JSONCodec MaxNodes
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec Int -> MaxNodes
MaxNodes MaxNodes -> Int
unMaxNodes
      (ValueCodec Int Int -> JSONCodec MaxNodes)
-> ValueCodec Int Int -> JSONCodec MaxNodes
forall a b. (a -> b) -> a -> b
$ Int -> ValueCodec Int Int
forall i. (Integral i, Bounded i) => i -> JSONCodec i
integralWithLowerBoundCodec Int
0

newtype MaxTime = MaxTime {MaxTime -> Seconds
unMaxTime :: Seconds}
  deriving stock (Int -> MaxTime -> ShowS
[MaxTime] -> ShowS
MaxTime -> String
(Int -> MaxTime -> ShowS)
-> (MaxTime -> String) -> ([MaxTime] -> ShowS) -> Show MaxTime
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MaxTime -> ShowS
showsPrec :: Int -> MaxTime -> ShowS
$cshow :: MaxTime -> String
show :: MaxTime -> String
$cshowList :: [MaxTime] -> ShowS
showList :: [MaxTime] -> ShowS
Show, MaxTime -> MaxTime -> Bool
(MaxTime -> MaxTime -> Bool)
-> (MaxTime -> MaxTime -> Bool) -> Eq MaxTime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaxTime -> MaxTime -> Bool
== :: MaxTime -> MaxTime -> Bool
$c/= :: MaxTime -> MaxTime -> Bool
/= :: MaxTime -> MaxTime -> Bool
Eq, Eq MaxTime
Eq MaxTime
-> (MaxTime -> MaxTime -> Ordering)
-> (MaxTime -> MaxTime -> Bool)
-> (MaxTime -> MaxTime -> Bool)
-> (MaxTime -> MaxTime -> Bool)
-> (MaxTime -> MaxTime -> Bool)
-> (MaxTime -> MaxTime -> MaxTime)
-> (MaxTime -> MaxTime -> MaxTime)
-> Ord MaxTime
MaxTime -> MaxTime -> Bool
MaxTime -> MaxTime -> Ordering
MaxTime -> MaxTime -> MaxTime
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MaxTime -> MaxTime -> Ordering
compare :: MaxTime -> MaxTime -> Ordering
$c< :: MaxTime -> MaxTime -> Bool
< :: MaxTime -> MaxTime -> Bool
$c<= :: MaxTime -> MaxTime -> Bool
<= :: MaxTime -> MaxTime -> Bool
$c> :: MaxTime -> MaxTime -> Bool
> :: MaxTime -> MaxTime -> Bool
$c>= :: MaxTime -> MaxTime -> Bool
>= :: MaxTime -> MaxTime -> Bool
$cmax :: MaxTime -> MaxTime -> MaxTime
max :: MaxTime -> MaxTime -> MaxTime
$cmin :: MaxTime -> MaxTime -> MaxTime
min :: MaxTime -> MaxTime -> MaxTime
Ord, (forall x. MaxTime -> Rep MaxTime x)
-> (forall x. Rep MaxTime x -> MaxTime) -> Generic MaxTime
forall x. Rep MaxTime x -> MaxTime
forall x. MaxTime -> Rep MaxTime x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MaxTime -> Rep MaxTime x
from :: forall x. MaxTime -> Rep MaxTime x
$cto :: forall x. Rep MaxTime x -> MaxTime
to :: forall x. Rep MaxTime x -> MaxTime
Generic)
  deriving newtype ([MaxTime] -> Value
[MaxTime] -> Encoding
MaxTime -> Value
MaxTime -> Encoding
(MaxTime -> Value)
-> (MaxTime -> Encoding)
-> ([MaxTime] -> Value)
-> ([MaxTime] -> Encoding)
-> ToJSON MaxTime
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: MaxTime -> Value
toJSON :: MaxTime -> Value
$ctoEncoding :: MaxTime -> Encoding
toEncoding :: MaxTime -> Encoding
$ctoJSONList :: [MaxTime] -> Value
toJSONList :: [MaxTime] -> Value
$ctoEncodingList :: [MaxTime] -> Encoding
toEncodingList :: [MaxTime] -> Encoding
ToJSON, Value -> Parser [MaxTime]
Value -> Parser MaxTime
(Value -> Parser MaxTime)
-> (Value -> Parser [MaxTime]) -> FromJSON MaxTime
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser MaxTime
parseJSON :: Value -> Parser MaxTime
$cparseJSONList :: Value -> Parser [MaxTime]
parseJSONList :: Value -> Parser [MaxTime]
FromJSON)

instance HasCodec MaxTime where
  codec :: JSONCodec MaxTime
codec =
    (Seconds -> MaxTime)
-> (MaxTime -> Seconds)
-> Codec Value Seconds Seconds
-> JSONCodec MaxTime
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec Seconds -> MaxTime
MaxTime MaxTime -> Seconds
unMaxTime
      (Codec Value Seconds Seconds -> JSONCodec MaxTime)
-> Codec Value Seconds Seconds -> JSONCodec MaxTime
forall a b. (a -> b) -> a -> b
$ Scientific -> Codec Value Seconds Seconds
forall r. (Real r, Fractional r) => Scientific -> JSONCodec r
realFracWithLowerBoundCodec Scientific
0

newtype MaxBatchSize = MaxBatchSize {MaxBatchSize -> Int
unMaxBatchSize :: Int}
  deriving stock (Int -> MaxBatchSize -> ShowS
[MaxBatchSize] -> ShowS
MaxBatchSize -> String
(Int -> MaxBatchSize -> ShowS)
-> (MaxBatchSize -> String)
-> ([MaxBatchSize] -> ShowS)
-> Show MaxBatchSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MaxBatchSize -> ShowS
showsPrec :: Int -> MaxBatchSize -> ShowS
$cshow :: MaxBatchSize -> String
show :: MaxBatchSize -> String
$cshowList :: [MaxBatchSize] -> ShowS
showList :: [MaxBatchSize] -> ShowS
Show, MaxBatchSize -> MaxBatchSize -> Bool
(MaxBatchSize -> MaxBatchSize -> Bool)
-> (MaxBatchSize -> MaxBatchSize -> Bool) -> Eq MaxBatchSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaxBatchSize -> MaxBatchSize -> Bool
== :: MaxBatchSize -> MaxBatchSize -> Bool
$c/= :: MaxBatchSize -> MaxBatchSize -> Bool
/= :: MaxBatchSize -> MaxBatchSize -> Bool
Eq, Eq MaxBatchSize
Eq MaxBatchSize
-> (MaxBatchSize -> MaxBatchSize -> Ordering)
-> (MaxBatchSize -> MaxBatchSize -> Bool)
-> (MaxBatchSize -> MaxBatchSize -> Bool)
-> (MaxBatchSize -> MaxBatchSize -> Bool)
-> (MaxBatchSize -> MaxBatchSize -> Bool)
-> (MaxBatchSize -> MaxBatchSize -> MaxBatchSize)
-> (MaxBatchSize -> MaxBatchSize -> MaxBatchSize)
-> Ord MaxBatchSize
MaxBatchSize -> MaxBatchSize -> Bool
MaxBatchSize -> MaxBatchSize -> Ordering
MaxBatchSize -> MaxBatchSize -> MaxBatchSize
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MaxBatchSize -> MaxBatchSize -> Ordering
compare :: MaxBatchSize -> MaxBatchSize -> Ordering
$c< :: MaxBatchSize -> MaxBatchSize -> Bool
< :: MaxBatchSize -> MaxBatchSize -> Bool
$c<= :: MaxBatchSize -> MaxBatchSize -> Bool
<= :: MaxBatchSize -> MaxBatchSize -> Bool
$c> :: MaxBatchSize -> MaxBatchSize -> Bool
> :: MaxBatchSize -> MaxBatchSize -> Bool
$c>= :: MaxBatchSize -> MaxBatchSize -> Bool
>= :: MaxBatchSize -> MaxBatchSize -> Bool
$cmax :: MaxBatchSize -> MaxBatchSize -> MaxBatchSize
max :: MaxBatchSize -> MaxBatchSize -> MaxBatchSize
$cmin :: MaxBatchSize -> MaxBatchSize -> MaxBatchSize
min :: MaxBatchSize -> MaxBatchSize -> MaxBatchSize
Ord, (forall x. MaxBatchSize -> Rep MaxBatchSize x)
-> (forall x. Rep MaxBatchSize x -> MaxBatchSize)
-> Generic MaxBatchSize
forall x. Rep MaxBatchSize x -> MaxBatchSize
forall x. MaxBatchSize -> Rep MaxBatchSize x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MaxBatchSize -> Rep MaxBatchSize x
from :: forall x. MaxBatchSize -> Rep MaxBatchSize x
$cto :: forall x. Rep MaxBatchSize x -> MaxBatchSize
to :: forall x. Rep MaxBatchSize x -> MaxBatchSize
Generic)
  deriving newtype ([MaxBatchSize] -> Value
[MaxBatchSize] -> Encoding
MaxBatchSize -> Value
MaxBatchSize -> Encoding
(MaxBatchSize -> Value)
-> (MaxBatchSize -> Encoding)
-> ([MaxBatchSize] -> Value)
-> ([MaxBatchSize] -> Encoding)
-> ToJSON MaxBatchSize
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: MaxBatchSize -> Value
toJSON :: MaxBatchSize -> Value
$ctoEncoding :: MaxBatchSize -> Encoding
toEncoding :: MaxBatchSize -> Encoding
$ctoJSONList :: [MaxBatchSize] -> Value
toJSONList :: [MaxBatchSize] -> Value
$ctoEncodingList :: [MaxBatchSize] -> Encoding
toEncodingList :: [MaxBatchSize] -> Encoding
ToJSON, Value -> Parser [MaxBatchSize]
Value -> Parser MaxBatchSize
(Value -> Parser MaxBatchSize)
-> (Value -> Parser [MaxBatchSize]) -> FromJSON MaxBatchSize
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser MaxBatchSize
parseJSON :: Value -> Parser MaxBatchSize
$cparseJSONList :: Value -> Parser [MaxBatchSize]
parseJSONList :: Value -> Parser [MaxBatchSize]
FromJSON)

instance HasCodec MaxBatchSize where
  codec :: JSONCodec MaxBatchSize
codec =
    (Int -> MaxBatchSize)
-> (MaxBatchSize -> Int)
-> ValueCodec Int Int
-> JSONCodec MaxBatchSize
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec Int -> MaxBatchSize
MaxBatchSize MaxBatchSize -> Int
unMaxBatchSize
      (ValueCodec Int Int -> JSONCodec MaxBatchSize)
-> ValueCodec Int Int -> JSONCodec MaxBatchSize
forall a b. (a -> b) -> a -> b
$ Int -> ValueCodec Int Int
forall i. (Integral i, Bounded i) => i -> JSONCodec i
integralWithLowerBoundCodec Int
0

-- | Defers to the (illegal) DiffTime Show instance.
--
-- >>> toTxt (MaxTime 2.5)
-- "2.5s"
instance ToTxt MaxTime where
  toTxt :: MaxTime -> Text
toTxt (MaxTime Seconds
t) = DiffTime -> Text
forall a. Show a => a -> Text
tshow (DiffTime -> Text) -> DiffTime -> Text
forall a b. (a -> b) -> a -> b
$ Seconds -> DiffTime
seconds Seconds
t