{-# 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-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.DynamoDB.Types.ConditionCheck where

import qualified Amazonka.Core as Core
import Amazonka.DynamoDB.Types.AttributeValue
import Amazonka.DynamoDB.Types.ReturnValuesOnConditionCheckFailure
import qualified Amazonka.Lens as Lens
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
(ConditionCheck -> ConditionCheck -> Bool)
-> (ConditionCheck -> ConditionCheck -> Bool) -> Eq ConditionCheck
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]
(Int -> ReadS ConditionCheck)
-> ReadS [ConditionCheck]
-> ReadPrec ConditionCheck
-> ReadPrec [ConditionCheck]
-> Read 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
(Int -> ConditionCheck -> ShowS)
-> (ConditionCheck -> String)
-> ([ConditionCheck] -> ShowS)
-> Show ConditionCheck
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. ConditionCheck -> Rep ConditionCheck x)
-> (forall x. Rep ConditionCheck x -> ConditionCheck)
-> Generic ConditionCheck
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' :: Maybe (HashMap Text Text)
-> Maybe (HashMap Text AttributeValue)
-> Maybe ReturnValuesOnConditionCheckFailure
-> HashMap Text AttributeValue
-> Text
-> Text
-> ConditionCheck
ConditionCheck'
    { $sel:expressionAttributeNames:ConditionCheck' :: Maybe (HashMap Text Text)
expressionAttributeNames =
        Maybe (HashMap Text Text)
forall a. Maybe a
Prelude.Nothing,
      $sel:expressionAttributeValues:ConditionCheck' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = Maybe (HashMap Text AttributeValue)
forall a. Maybe a
Prelude.Nothing,
      $sel:returnValuesOnConditionCheckFailure:ConditionCheck' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure =
        Maybe ReturnValuesOnConditionCheckFailure
forall a. Maybe a
Prelude.Nothing,
      $sel:key:ConditionCheck' :: HashMap Text AttributeValue
key = HashMap Text AttributeValue
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 :: (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> ConditionCheck -> f ConditionCheck
conditionCheck_expressionAttributeNames = (ConditionCheck -> Maybe (HashMap Text Text))
-> (ConditionCheck -> Maybe (HashMap Text Text) -> ConditionCheck)
-> Lens
     ConditionCheck
     ConditionCheck
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
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) ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
 -> ConditionCheck -> f ConditionCheck)
-> ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
    -> Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> ConditionCheck
-> f ConditionCheck
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
-> Iso
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
     (Maybe (HashMap Text Text))
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 AnIso
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
  (HashMap Text Text)
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 :: (Maybe (HashMap Text AttributeValue)
 -> f (Maybe (HashMap Text AttributeValue)))
-> ConditionCheck -> f ConditionCheck
conditionCheck_expressionAttributeValues = (ConditionCheck -> Maybe (HashMap Text AttributeValue))
-> (ConditionCheck
    -> Maybe (HashMap Text AttributeValue) -> ConditionCheck)
-> Lens
     ConditionCheck
     ConditionCheck
     (Maybe (HashMap Text AttributeValue))
     (Maybe (HashMap Text AttributeValue))
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) ((Maybe (HashMap Text AttributeValue)
  -> f (Maybe (HashMap Text AttributeValue)))
 -> ConditionCheck -> f ConditionCheck)
-> ((Maybe (HashMap Text AttributeValue)
     -> f (Maybe (HashMap Text AttributeValue)))
    -> Maybe (HashMap Text AttributeValue)
    -> f (Maybe (HashMap Text AttributeValue)))
-> (Maybe (HashMap Text AttributeValue)
    -> f (Maybe (HashMap Text AttributeValue)))
-> ConditionCheck
-> f ConditionCheck
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
-> Iso
     (Maybe (HashMap Text AttributeValue))
     (Maybe (HashMap Text AttributeValue))
     (Maybe (HashMap Text AttributeValue))
     (Maybe (HashMap Text AttributeValue))
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 AnIso
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
  (HashMap Text AttributeValue)
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 :: (Maybe ReturnValuesOnConditionCheckFailure
 -> f (Maybe ReturnValuesOnConditionCheckFailure))
-> ConditionCheck -> f ConditionCheck
conditionCheck_returnValuesOnConditionCheckFailure = (ConditionCheck -> Maybe ReturnValuesOnConditionCheckFailure)
-> (ConditionCheck
    -> Maybe ReturnValuesOnConditionCheckFailure -> ConditionCheck)
-> Lens
     ConditionCheck
     ConditionCheck
     (Maybe ReturnValuesOnConditionCheckFailure)
     (Maybe 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 :: (HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> ConditionCheck -> f ConditionCheck
conditionCheck_key = (ConditionCheck -> HashMap Text AttributeValue)
-> (ConditionCheck
    -> HashMap Text AttributeValue -> ConditionCheck)
-> Lens
     ConditionCheck
     ConditionCheck
     (HashMap Text AttributeValue)
     (HashMap Text AttributeValue)
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) ((HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
 -> ConditionCheck -> f ConditionCheck)
-> ((HashMap Text AttributeValue
     -> f (HashMap Text AttributeValue))
    -> HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> (HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> ConditionCheck
-> f ConditionCheck
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> HashMap Text AttributeValue -> f (HashMap Text AttributeValue)
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 :: (Text -> f Text) -> ConditionCheck -> f ConditionCheck
conditionCheck_tableName = (ConditionCheck -> Text)
-> (ConditionCheck -> Text -> ConditionCheck)
-> Lens ConditionCheck ConditionCheck Text Text
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 :: (Text -> f Text) -> ConditionCheck -> f ConditionCheck
conditionCheck_conditionExpression = (ConditionCheck -> Text)
-> (ConditionCheck -> Text -> ConditionCheck)
-> Lens ConditionCheck ConditionCheck Text Text
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

instance Prelude.NFData ConditionCheck

instance Core.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
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"ExpressionAttributeNames" Text -> HashMap Text Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (HashMap Text Text -> Pair)
-> Maybe (HashMap Text Text) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text Text)
expressionAttributeNames,
            (Text
"ExpressionAttributeValues" Text -> HashMap Text AttributeValue -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (HashMap Text AttributeValue -> Pair)
-> Maybe (HashMap Text AttributeValue) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (HashMap Text AttributeValue)
expressionAttributeValues,
            (Text
"ReturnValuesOnConditionCheckFailure" Text -> ReturnValuesOnConditionCheckFailure -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (ReturnValuesOnConditionCheckFailure -> Pair)
-> Maybe ReturnValuesOnConditionCheckFailure -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Key" Text -> HashMap Text AttributeValue -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= HashMap Text AttributeValue
key),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"TableName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
tableName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"ConditionExpression" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
conditionExpression)
          ]
      )