{-# 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-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.Update 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 an @UpdateItem@ operation.
--
-- /See:/ 'newUpdate' smart constructor.
data Update = Update'
  { -- | A condition that must be satisfied in order for a conditional update to
    -- succeed.
    Update -> Maybe Text
conditionExpression :: Prelude.Maybe Prelude.Text,
    -- | 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,
    -- | 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
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]
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
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. 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:
--
-- 'conditionExpression', 'update_conditionExpression' - A condition that must be satisfied in order for a conditional update to
-- succeed.
--
-- '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.
--
-- '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'
    { $sel:conditionExpression:Update' :: Maybe Text
conditionExpression = forall a. Maybe a
Prelude.Nothing,
      $sel:expressionAttributeNames:Update' :: Maybe (HashMap Text Text)
expressionAttributeNames = forall a. Maybe a
Prelude.Nothing,
      $sel:expressionAttributeValues:Update' :: Maybe (HashMap Text AttributeValue)
expressionAttributeValues = forall a. Maybe a
Prelude.Nothing,
      $sel:returnValuesOnConditionCheckFailure:Update' :: Maybe ReturnValuesOnConditionCheckFailure
returnValuesOnConditionCheckFailure =
        forall a. Maybe a
Prelude.Nothing,
      $sel:key:Update' :: HashMap Text AttributeValue
key = forall a. Monoid a => a
Prelude.mempty,
      $sel:updateExpression:Update' :: Text
updateExpression = Text
pUpdateExpression_,
      $sel:tableName:Update' :: Text
tableName = Text
pTableName_
    }

-- | 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 :: Lens' Update (Maybe Text)
update_conditionExpression = 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)

-- | 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 :: Lens' Update (Maybe (HashMap Text Text))
update_expressionAttributeNames = 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) 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.
update_expressionAttributeValues :: Lens.Lens' Update (Prelude.Maybe (Prelude.HashMap Prelude.Text AttributeValue))
update_expressionAttributeValues :: Lens' Update (Maybe (HashMap Text AttributeValue))
update_expressionAttributeValues = 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) 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 @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 :: Lens' Update (Maybe ReturnValuesOnConditionCheckFailure)
update_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)

-- | 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 :: Lens' Update (HashMap Text AttributeValue)
update_key = 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) 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

-- | 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 :: Lens' Update Text
update_updateExpression = 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 :: Lens' Update Text
update_tableName = 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 where
  hashWithSalt :: Int -> Update -> Int
hashWithSalt Int
_salt 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
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
conditionExpression :: Maybe Text
$sel:tableName:Update' :: Update -> Text
$sel:updateExpression:Update' :: Update -> Text
$sel:key:Update' :: Update -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:Update' :: Update -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:Update' :: Update -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:Update' :: Update -> Maybe (HashMap Text Text)
$sel:conditionExpression:Update' :: Update -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
conditionExpression
      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
updateExpression
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
tableName

instance Prelude.NFData Update where
  rnf :: Update -> ()
rnf 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
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
conditionExpression :: Maybe Text
$sel:tableName:Update' :: Update -> Text
$sel:updateExpression:Update' :: Update -> Text
$sel:key:Update' :: Update -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:Update' :: Update -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:Update' :: Update -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:Update' :: Update -> Maybe (HashMap Text Text)
$sel:conditionExpression:Update' :: Update -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
conditionExpression
      seq :: forall a b. a -> b -> b
`Prelude.seq` 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
updateExpression
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
tableName

instance Data.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
returnValuesOnConditionCheckFailure :: Maybe ReturnValuesOnConditionCheckFailure
expressionAttributeValues :: Maybe (HashMap Text AttributeValue)
expressionAttributeNames :: Maybe (HashMap Text Text)
conditionExpression :: Maybe Text
$sel:tableName:Update' :: Update -> Text
$sel:updateExpression:Update' :: Update -> Text
$sel:key:Update' :: Update -> HashMap Text AttributeValue
$sel:returnValuesOnConditionCheckFailure:Update' :: Update -> Maybe ReturnValuesOnConditionCheckFailure
$sel:expressionAttributeValues:Update' :: Update -> Maybe (HashMap Text AttributeValue)
$sel:expressionAttributeNames:Update' :: Update -> Maybe (HashMap Text Text)
$sel:conditionExpression:Update' :: Update -> Maybe Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"ConditionExpression" 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 Text
conditionExpression,
            (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
"UpdateExpression" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
updateExpression),
            forall a. a -> Maybe a
Prelude.Just (Key
"TableName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
tableName)
          ]
      )