{-# 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.Update
-- 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.Update 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 an @UpdateItem@ operation.
--
-- /See:/ 'newUpdate' smart constructor.
data Update = Update'
  { -- | One or more substitution tokens for attribute names in an expression.
    Update -> Maybe (HashMap Text Text)
expressionAttributeNames :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text),
    -- | One or more values that can be substituted in an expression.
    Update -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues :: Prelude.Maybe (Prelude.HashMap Prelude.Text AttributeValue),
    -- | Use @ReturnValuesOnConditionCheckFailure@ to get the item attributes if
    -- the @Update@ condition fails. For @ReturnValuesOnConditionCheckFailure@,
    -- the valid values are: NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW.
    Update -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure :: Prelude.Maybe ReturnValuesOnConditionCheckFailure,
    -- | A condition that must be satisfied in order for a conditional update to
    -- succeed.
    Update -> Maybe Text
conditionExpression :: Prelude.Maybe Prelude.Text,
    -- | The primary key of the item to be updated. Each element consists of an
    -- attribute name and a value for that attribute.
    Update -> HashMap Text AttributeValue
key :: Prelude.HashMap Prelude.Text AttributeValue,
    -- | An expression that defines one or more attributes to be updated, the
    -- action to be performed on them, and new value(s) for them.
    Update -> Text
updateExpression :: Prelude.Text,
    -- | Name of the table for the @UpdateItem@ request.
    Update -> Text
tableName :: Prelude.Text
  }
  deriving (Update -> Update -> Bool
(Update -> Update -> Bool)
-> (Update -> Update -> Bool) -> Eq Update
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Update -> Update -> Bool
$c/= :: Update -> Update -> Bool
== :: Update -> Update -> Bool
$c== :: Update -> Update -> Bool
Prelude.Eq, ReadPrec [Update]
ReadPrec Update
Int -> ReadS Update
ReadS [Update]
(Int -> ReadS Update)
-> ReadS [Update]
-> ReadPrec Update
-> ReadPrec [Update]
-> Read Update
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Update]
$creadListPrec :: ReadPrec [Update]
readPrec :: ReadPrec Update
$creadPrec :: ReadPrec Update
readList :: ReadS [Update]
$creadList :: ReadS [Update]
readsPrec :: Int -> ReadS Update
$creadsPrec :: Int -> ReadS Update
Prelude.Read, Int -> Update -> ShowS
[Update] -> ShowS
Update -> String
(Int -> Update -> ShowS)
-> (Update -> String) -> ([Update] -> ShowS) -> Show Update
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Update] -> ShowS
$cshowList :: [Update] -> ShowS
show :: Update -> String
$cshow :: Update -> String
showsPrec :: Int -> Update -> ShowS
$cshowsPrec :: Int -> Update -> ShowS
Prelude.Show, (forall x. Update -> Rep Update x)
-> (forall x. Rep Update x -> Update) -> Generic Update
forall x. Rep Update x -> Update
forall x. Update -> Rep Update x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Update x -> Update
$cfrom :: forall x. Update -> Rep Update x
Prelude.Generic)

-- |
-- Create a value of 'Update' 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', 'update_expressionAttributeNames' - One or more substitution tokens for attribute names in an expression.
--
-- 'expressionAttributeValues', 'update_expressionAttributeValues' - One or more values that can be substituted in an expression.
--
-- 'returnValuesOnConditionCheckFailure', 'update_returnValuesOnConditionCheckFailure' - Use @ReturnValuesOnConditionCheckFailure@ to get the item attributes if
-- the @Update@ condition fails. For @ReturnValuesOnConditionCheckFailure@,
-- the valid values are: NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW.
--
-- 'conditionExpression', 'update_conditionExpression' - A condition that must be satisfied in order for a conditional update to
-- succeed.
--
-- 'key', 'update_key' - The primary key of the item to be updated. Each element consists of an
-- attribute name and a value for that attribute.
--
-- 'updateExpression', 'update_updateExpression' - An expression that defines one or more attributes to be updated, the
-- action to be performed on them, and new value(s) for them.
--
-- 'tableName', 'update_tableName' - Name of the table for the @UpdateItem@ request.
newUpdate ::
  -- | 'updateExpression'
  Prelude.Text ->
  -- | 'tableName'
  Prelude.Text ->
  Update
newUpdate :: Text -> Text -> Update
newUpdate Text
pUpdateExpression_ Text
pTableName_ =
  Update' :: Maybe (HashMap Text Text)
-> Maybe (HashMap Text AttributeValue)
-> Maybe ReturnValuesOnConditionCheckFailure
-> Maybe Text
-> HashMap Text AttributeValue
-> Text
-> Text
-> Update
Update'
    { $sel:expressionAttributeNames:Update' :: Maybe (HashMap Text Text)
expressionAttributeNames = Maybe (HashMap Text Text)
forall a. Maybe a
Prelude.Nothing,
      $sel:expressionAttributeValues:Update' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = Maybe (HashMap Text AttributeValue)
forall a. Maybe a
Prelude.Nothing,
      $sel:returnValuesOnConditionCheckFailure:Update' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure =
        Maybe ReturnValuesOnConditionCheckFailure
forall a. Maybe a
Prelude.Nothing,
      $sel:conditionExpression:Update' :: Maybe Text
conditionExpression = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:key:Update' :: HashMap Text AttributeValue
key = HashMap Text AttributeValue
forall a. Monoid a => a
Prelude.mempty,
      $sel:updateExpression:Update' :: Text
updateExpression = Text
pUpdateExpression_,
      $sel:tableName:Update' :: Text
tableName = Text
pTableName_
    }

-- | One or more substitution tokens for attribute names in an expression.
update_expressionAttributeNames :: Lens.Lens' Update (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
update_expressionAttributeNames :: (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> Update -> f Update
update_expressionAttributeNames = (Update -> Maybe (HashMap Text Text))
-> (Update -> Maybe (HashMap Text Text) -> Update)
-> Lens
     Update
     Update
     (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 (\Update' {Maybe (HashMap Text Text)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:expressionAttributeNames:Update' :: Update -> Maybe (HashMap Text Text)
expressionAttributeNames} -> Maybe (HashMap Text Text)
expressionAttributeNames) (\s :: Update
s@Update' {} Maybe (HashMap Text Text)
a -> Update
s {$sel:expressionAttributeNames:Update' :: Maybe (HashMap Text Text)
expressionAttributeNames = Maybe (HashMap Text Text)
a} :: Update) ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
 -> Update -> f Update)
-> ((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)))
-> Update
-> f Update
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.
update_expressionAttributeValues :: Lens.Lens' Update (Prelude.Maybe (Prelude.HashMap Prelude.Text AttributeValue))
update_expressionAttributeValues :: (Maybe (HashMap Text AttributeValue)
 -> f (Maybe (HashMap Text AttributeValue)))
-> Update -> f Update
update_expressionAttributeValues = (Update -> Maybe (HashMap Text AttributeValue))
-> (Update -> Maybe (HashMap Text AttributeValue) -> Update)
-> Lens
     Update
     Update
     (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 (\Update' {Maybe (HashMap Text AttributeValue)
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeValues:Update' :: Update -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues} -> Maybe (HashMap Text AttributeValue)
expressionAttributeValues) (\s :: Update
s@Update' {} Maybe (HashMap Text AttributeValue)
a -> Update
s {$sel:expressionAttributeValues:Update' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = Maybe (HashMap Text AttributeValue)
a} :: Update) ((Maybe (HashMap Text AttributeValue)
  -> f (Maybe (HashMap Text AttributeValue)))
 -> Update -> f Update)
-> ((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)))
-> Update
-> f Update
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 @Update@ condition fails. For @ReturnValuesOnConditionCheckFailure@,
-- the valid values are: NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW.
update_returnValuesOnConditionCheckFailure :: Lens.Lens' Update (Prelude.Maybe ReturnValuesOnConditionCheckFailure)
update_returnValuesOnConditionCheckFailure :: (Maybe ReturnValuesOnConditionCheckFailure
 -> f (Maybe ReturnValuesOnConditionCheckFailure))
-> Update -> f Update
update_returnValuesOnConditionCheckFailure = (Update -> Maybe ReturnValuesOnConditionCheckFailure)
-> (Update -> Maybe ReturnValuesOnConditionCheckFailure -> Update)
-> Lens
     Update
     Update
     (Maybe ReturnValuesOnConditionCheckFailure)
     (Maybe ReturnValuesOnConditionCheckFailure)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Update' {Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
$sel:returnValuesOnConditionCheckFailure:Update' :: Update -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure} -> Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure) (\s :: Update
s@Update' {} Maybe ReturnValuesOnConditionCheckFailure
a -> Update
s {$sel:returnValuesOnConditionCheckFailure:Update' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure = Maybe ReturnValuesOnConditionCheckFailure
a} :: Update)

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

-- | The primary key of the item to be updated. Each element consists of an
-- attribute name and a value for that attribute.
update_key :: Lens.Lens' Update (Prelude.HashMap Prelude.Text AttributeValue)
update_key :: (HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> Update -> f Update
update_key = (Update -> HashMap Text AttributeValue)
-> (Update -> HashMap Text AttributeValue -> Update)
-> Lens
     Update
     Update
     (HashMap Text AttributeValue)
     (HashMap Text AttributeValue)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Update' {HashMap Text AttributeValue
key :: HashMap Text AttributeValue
$sel:key:Update' :: Update -> HashMap Text AttributeValue
key} -> HashMap Text AttributeValue
key) (\s :: Update
s@Update' {} HashMap Text AttributeValue
a -> Update
s {$sel:key:Update' :: HashMap Text AttributeValue
key = HashMap Text AttributeValue
a} :: Update) ((HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
 -> Update -> f Update)
-> ((HashMap Text AttributeValue
     -> f (HashMap Text AttributeValue))
    -> HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> (HashMap Text AttributeValue -> f (HashMap Text AttributeValue))
-> Update
-> f Update
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

-- | An expression that defines one or more attributes to be updated, the
-- action to be performed on them, and new value(s) for them.
update_updateExpression :: Lens.Lens' Update Prelude.Text
update_updateExpression :: (Text -> f Text) -> Update -> f Update
update_updateExpression = (Update -> Text)
-> (Update -> Text -> Update) -> Lens Update Update Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Update' {Text
updateExpression :: Text
$sel:updateExpression:Update' :: Update -> Text
updateExpression} -> Text
updateExpression) (\s :: Update
s@Update' {} Text
a -> Update
s {$sel:updateExpression:Update' :: Text
updateExpression = Text
a} :: Update)

-- | Name of the table for the @UpdateItem@ request.
update_tableName :: Lens.Lens' Update Prelude.Text
update_tableName :: (Text -> f Text) -> Update -> f Update
update_tableName = (Update -> Text)
-> (Update -> Text -> Update) -> Lens Update Update Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Update' {Text
tableName :: Text
$sel:tableName:Update' :: Update -> Text
tableName} -> Text
tableName) (\s :: Update
s@Update' {} Text
a -> Update
s {$sel:tableName:Update' :: Text
tableName = Text
a} :: Update)

instance Prelude.Hashable Update

instance Prelude.NFData Update

instance Core.ToJSON Update where
  toJSON :: Update -> Value
toJSON Update' {Maybe Text
Maybe (HashMap Text Text)
Maybe (HashMap Text AttributeValue)
Maybe ReturnValuesOnConditionCheckFailure
Text
HashMap Text AttributeValue
tableName :: Text
updateExpression :: Text
key :: HashMap Text AttributeValue
conditionExpression :: Maybe Text
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
$sel:tableName:Update' :: Update -> Text
$sel:updateExpression:Update' :: Update -> Text
$sel:key:Update' :: Update -> HashMap Text AttributeValue
$sel:conditionExpression:Update' :: Update -> Maybe Text
$sel:returnValuesOnConditionCheckFailure:Update' :: Update -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:Update' :: Update -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:Update' :: Update -> 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,
            (Text
"ConditionExpression" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
conditionExpression,
            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
"UpdateExpression" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
updateExpression),
            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)
          ]
      )