{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.DynamoDB.Types.ConditionCheck
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.DynamoDB.Types.ConditionCheck where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.DynamoDB.Types.AttributeValue
import Amazonka.DynamoDB.Types.ReturnValuesOnConditionCheckFailure
import Amazonka.DynamoDB.Types.WriteRequest
import qualified Amazonka.Prelude as Prelude

-- | Represents a request to perform a check that an item exists or to check
-- the condition of specific attributes of the item.
--
-- /See:/ 'newConditionCheck' smart constructor.
data ConditionCheck = ConditionCheck'
  { -- | One or more substitution tokens for attribute names in an expression.
    ConditionCheck -> Maybe (HashMap Text Text)
expressionAttributeNames :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text),
    -- | One or more values that can be substituted in an expression.
    ConditionCheck -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues :: Prelude.Maybe (Prelude.HashMap Prelude.Text AttributeValue),
    -- | Use @ReturnValuesOnConditionCheckFailure@ to get the item attributes if
    -- the @ConditionCheck@ condition fails. For
    -- @ReturnValuesOnConditionCheckFailure@, the valid values are: NONE and
    -- ALL_OLD.
    ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure :: Prelude.Maybe ReturnValuesOnConditionCheckFailure,
    -- | The primary key of the item to be checked. Each element consists of an
    -- attribute name and a value for that attribute.
    ConditionCheck -> HashMap Text AttributeValue
key :: Prelude.HashMap Prelude.Text AttributeValue,
    -- | Name of the table for the check item request.
    ConditionCheck -> Text
tableName :: Prelude.Text,
    -- | A condition that must be satisfied in order for a conditional update to
    -- succeed.
    ConditionCheck -> Text
conditionExpression :: Prelude.Text
  }
  deriving (ConditionCheck -> ConditionCheck -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConditionCheck -> ConditionCheck -> Bool
$c/= :: ConditionCheck -> ConditionCheck -> Bool
== :: ConditionCheck -> ConditionCheck -> Bool
$c== :: ConditionCheck -> ConditionCheck -> Bool
Prelude.Eq, ReadPrec [ConditionCheck]
ReadPrec ConditionCheck
Int -> ReadS ConditionCheck
ReadS [ConditionCheck]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ConditionCheck]
$creadListPrec :: ReadPrec [ConditionCheck]
readPrec :: ReadPrec ConditionCheck
$creadPrec :: ReadPrec ConditionCheck
readList :: ReadS [ConditionCheck]
$creadList :: ReadS [ConditionCheck]
readsPrec :: Int -> ReadS ConditionCheck
$creadsPrec :: Int -> ReadS ConditionCheck
Prelude.Read, Int -> ConditionCheck -> ShowS
[ConditionCheck] -> ShowS
ConditionCheck -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConditionCheck] -> ShowS
$cshowList :: [ConditionCheck] -> ShowS
show :: ConditionCheck -> String
$cshow :: ConditionCheck -> String
showsPrec :: Int -> ConditionCheck -> ShowS
$cshowsPrec :: Int -> ConditionCheck -> ShowS
Prelude.Show, forall x. Rep ConditionCheck x -> ConditionCheck
forall x. ConditionCheck -> Rep ConditionCheck x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ConditionCheck x -> ConditionCheck
$cfrom :: forall x. ConditionCheck -> Rep ConditionCheck x
Prelude.Generic)

-- |
-- Create a value of 'ConditionCheck' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'expressionAttributeNames', 'conditionCheck_expressionAttributeNames' - One or more substitution tokens for attribute names in an expression.
--
-- 'expressionAttributeValues', 'conditionCheck_expressionAttributeValues' - One or more values that can be substituted in an expression.
--
-- 'returnValuesOnConditionCheckFailure', 'conditionCheck_returnValuesOnConditionCheckFailure' - Use @ReturnValuesOnConditionCheckFailure@ to get the item attributes if
-- the @ConditionCheck@ condition fails. For
-- @ReturnValuesOnConditionCheckFailure@, the valid values are: NONE and
-- ALL_OLD.
--
-- 'key', 'conditionCheck_key' - The primary key of the item to be checked. Each element consists of an
-- attribute name and a value for that attribute.
--
-- 'tableName', 'conditionCheck_tableName' - Name of the table for the check item request.
--
-- 'conditionExpression', 'conditionCheck_conditionExpression' - A condition that must be satisfied in order for a conditional update to
-- succeed.
newConditionCheck ::
  -- | 'tableName'
  Prelude.Text ->
  -- | 'conditionExpression'
  Prelude.Text ->
  ConditionCheck
newConditionCheck :: Text -> Text -> ConditionCheck
newConditionCheck Text
pTableName_ Text
pConditionExpression_ =
  ConditionCheck'
    { $sel:expressionAttributeNames:ConditionCheck' :: Maybe (HashMap Text Text)
expressionAttributeNames =
        forall a. Maybe a
Prelude.Nothing,
      $sel:expressionAttributeValues:ConditionCheck' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = forall a. Maybe a
Prelude.Nothing,
      $sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure =
        forall a. Maybe a
Prelude.Nothing,
      $sel:key:ConditionCheck' :: HashMap Text AttributeValue
key = forall a. Monoid a => a
Prelude.mempty,
      $sel:tableName:ConditionCheck' :: Text
tableName = Text
pTableName_,
      $sel:conditionExpression:ConditionCheck' :: Text
conditionExpression = Text
pConditionExpression_
    }

-- | One or more substitution tokens for attribute names in an expression.
conditionCheck_expressionAttributeNames :: Lens.Lens' ConditionCheck (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
conditionCheck_expressionAttributeNames :: Lens' ConditionCheck (Maybe (HashMap Text Text))
conditionCheck_expressionAttributeNames = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {Maybe (HashMap Text Text)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:expressionAttributeNames:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text Text)
expressionAttributeNames} -> Maybe (HashMap Text Text)
expressionAttributeNames) (\s :: ConditionCheck
s@ConditionCheck' {} Maybe (HashMap Text Text)
a -> ConditionCheck
s {$sel:expressionAttributeNames:ConditionCheck' :: Maybe (HashMap Text Text)
expressionAttributeNames = Maybe (HashMap Text Text)
a} :: ConditionCheck) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | One or more values that can be substituted in an expression.
conditionCheck_expressionAttributeValues :: Lens.Lens' ConditionCheck (Prelude.Maybe (Prelude.HashMap Prelude.Text AttributeValue))
conditionCheck_expressionAttributeValues :: Lens' ConditionCheck (Maybe (HashMap Text AttributeValue))
conditionCheck_expressionAttributeValues = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {Maybe (HashMap Text AttributeValue)
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeValues:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues} -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues) (\s :: ConditionCheck
s@ConditionCheck' {} Maybe (HashMap Text AttributeValue)
a -> ConditionCheck
s {$sel:expressionAttributeValues:ConditionCheck' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = Maybe (HashMap Text AttributeValue)
a} :: ConditionCheck) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Use @ReturnValuesOnConditionCheckFailure@ to get the item attributes if
-- the @ConditionCheck@ condition fails. For
-- @ReturnValuesOnConditionCheckFailure@, the valid values are: NONE and
-- ALL_OLD.
conditionCheck_returnValuesOnConditionCheckFailure :: Lens.Lens' ConditionCheck (Prelude.Maybe ReturnValuesOnConditionCheckFailure)
conditionCheck_returnValuesOnConditionCheckFailure :: Lens' ConditionCheck (Maybe ReturnValuesOnConditionCheckFailure)
conditionCheck_returnValuesOnConditionCheckFailure = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
$sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure} -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure) (\s :: ConditionCheck
s@ConditionCheck' {} Maybe ReturnValuesOnConditionCheckFailure
a -> ConditionCheck
s {$sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure = Maybe ReturnValuesOnConditionCheckFailure
a} :: ConditionCheck)

-- | The primary key of the item to be checked. Each element consists of an
-- attribute name and a value for that attribute.
conditionCheck_key :: Lens.Lens' ConditionCheck (Prelude.HashMap Prelude.Text AttributeValue)
conditionCheck_key :: Lens' ConditionCheck (HashMap Text AttributeValue)
conditionCheck_key = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {HashMap Text AttributeValue
key :: HashMap Text AttributeValue
$sel:key:ConditionCheck' :: ConditionCheck -> HashMap Text AttributeValue
key} -> HashMap Text AttributeValue
key) (\s :: ConditionCheck
s@ConditionCheck' {} HashMap Text AttributeValue
a -> ConditionCheck
s {$sel:key:ConditionCheck' :: HashMap Text AttributeValue
key = HashMap Text AttributeValue
a} :: ConditionCheck) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Name of the table for the check item request.
conditionCheck_tableName :: Lens.Lens' ConditionCheck Prelude.Text
conditionCheck_tableName :: Lens' ConditionCheck Text
conditionCheck_tableName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {Text
tableName :: Text
$sel:tableName:ConditionCheck' :: ConditionCheck -> Text
tableName} -> Text
tableName) (\s :: ConditionCheck
s@ConditionCheck' {} Text
a -> ConditionCheck
s {$sel:tableName:ConditionCheck' :: Text
tableName = Text
a} :: ConditionCheck)

-- | A condition that must be satisfied in order for a conditional update to
-- succeed.
conditionCheck_conditionExpression :: Lens.Lens' ConditionCheck Prelude.Text
conditionCheck_conditionExpression :: Lens' ConditionCheck Text
conditionCheck_conditionExpression = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ConditionCheck' {Text
conditionExpression :: Text
$sel:conditionExpression:ConditionCheck' :: ConditionCheck -> Text
conditionExpression} -> Text
conditionExpression) (\s :: ConditionCheck
s@ConditionCheck' {} Text
a -> ConditionCheck
s {$sel:conditionExpression:ConditionCheck' :: Text
conditionExpression = Text
a} :: ConditionCheck)

instance Prelude.Hashable ConditionCheck where
  hashWithSalt :: Int -> ConditionCheck -> Int
hashWithSalt Int
_salt ConditionCheck' {Maybe (HashMap Text Text)
Maybe (HashMap Text AttributeValue)
Maybe ReturnValuesOnConditionCheckFailure
Text
HashMap Text AttributeValue
conditionExpression :: Text
tableName :: Text
key :: HashMap Text AttributeValue
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:conditionExpression:ConditionCheck' :: ConditionCheck -> Text
$sel:tableName:ConditionCheck' :: ConditionCheck -> Text
$sel:key:ConditionCheck' :: ConditionCheck -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text Text)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (HashMap Text Text)
expressionAttributeNames
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (HashMap Text AttributeValue)
expressionAttributeValues
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` HashMap Text AttributeValue
key
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
tableName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
conditionExpression

instance Prelude.NFData ConditionCheck where
  rnf :: ConditionCheck -> ()
rnf ConditionCheck' {Maybe (HashMap Text Text)
Maybe (HashMap Text AttributeValue)
Maybe ReturnValuesOnConditionCheckFailure
Text
HashMap Text AttributeValue
conditionExpression :: Text
tableName :: Text
key :: HashMap Text AttributeValue
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:conditionExpression:ConditionCheck' :: ConditionCheck -> Text
$sel:tableName:ConditionCheck' :: ConditionCheck -> Text
$sel:key:ConditionCheck' :: ConditionCheck -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text Text)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (HashMap Text Text)
expressionAttributeNames
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe (HashMap Text AttributeValue)
expressionAttributeValues
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf HashMap Text AttributeValue
key
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
tableName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
conditionExpression

instance Data.ToJSON ConditionCheck where
  toJSON :: ConditionCheck -> Value
toJSON ConditionCheck' {Maybe (HashMap Text Text)
Maybe (HashMap Text AttributeValue)
Maybe ReturnValuesOnConditionCheckFailure
Text
HashMap Text AttributeValue
conditionExpression :: Text
tableName :: Text
key :: HashMap Text AttributeValue
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:conditionExpression:ConditionCheck' :: ConditionCheck -> Text
$sel:tableName:ConditionCheck' :: ConditionCheck -> Text
$sel:key:ConditionCheck' :: ConditionCheck -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:ConditionCheck' :: ConditionCheck -> Maybe (HashMap Text Text)
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"ExpressionAttributeNames" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text Text)
expressionAttributeNames,
            (Key
"ExpressionAttributeValues" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text AttributeValue)
expressionAttributeValues,
            (Key
"ReturnValuesOnConditionCheckFailure" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure,
            forall a. a -> Maybe a
Prelude.Just (Key
"Key" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= HashMap Text AttributeValue
key),
            forall a. a -> Maybe a
Prelude.Just (Key
"TableName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
tableName),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"ConditionExpression" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
conditionExpression)
          ]
      )