{-# 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.IoTEvents.Types.DynamoDBv2Action
-- 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.IoTEvents.Types.DynamoDBv2Action where

import qualified Amazonka.Core as Core
import Amazonka.IoTEvents.Types.Payload
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Defines an action to write to the Amazon DynamoDB table that you
-- created. The default action payload contains all the information about
-- the detector model instance and the event that triggered the action. You
-- can customize the
-- <https://docs.aws.amazon.com/iotevents/latest/apireference/API_Payload.html payload>.
-- A separate column of the DynamoDB table receives one attribute-value
-- pair in the payload that you specify.
--
-- You must use expressions for all parameters in @DynamoDBv2Action@. The
-- expressions accept literals, operators, functions, references, and
-- substitution templates.
--
-- __Examples__
--
-- -   For literal values, the expressions must contain single quotes. For
--     example, the value for the @tableName@ parameter can be
--     @\'GreenhouseTemperatureTable\'@.
--
-- -   For references, you must specify either variables or input values.
--     For example, the value for the @tableName@ parameter can be
--     @$variable.ddbtableName@.
--
-- -   For a substitution template, you must use @${}@, and the template
--     must be in single quotes. A substitution template can also contain a
--     combination of literals, operators, functions, references, and
--     substitution templates.
--
--     In the following example, the value for the @contentExpression@
--     parameter in @Payload@ uses a substitution template.
--
--     @\'{\\\"sensorID\\\": \\\"${$input.GreenhouseInput.sensor_id}\\\", \\\"temperature\\\": \\\"${$input.GreenhouseInput.temperature * 9 \/ 5 + 32}\\\"}\'@
--
-- -   For a string concatenation, you must use @+@. A string concatenation
--     can also contain a combination of literals, operators, functions,
--     references, and substitution templates.
--
--     In the following example, the value for the @tableName@ parameter
--     uses a string concatenation.
--
--     @\'GreenhouseTemperatureTable \' + $input.GreenhouseInput.date@
--
-- For more information, see
-- <https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-expressions.html Expressions>
-- in the /AWS IoT Events Developer Guide/.
--
-- The value for the @type@ parameter in @Payload@ must be @JSON@.
--
-- /See:/ 'newDynamoDBv2Action' smart constructor.
data DynamoDBv2Action = DynamoDBv2Action'
  { DynamoDBv2Action -> Maybe Payload
payload :: Prelude.Maybe Payload,
    -- | The name of the DynamoDB table.
    DynamoDBv2Action -> Text
tableName :: Prelude.Text
  }
  deriving (DynamoDBv2Action -> DynamoDBv2Action -> Bool
(DynamoDBv2Action -> DynamoDBv2Action -> Bool)
-> (DynamoDBv2Action -> DynamoDBv2Action -> Bool)
-> Eq DynamoDBv2Action
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DynamoDBv2Action -> DynamoDBv2Action -> Bool
$c/= :: DynamoDBv2Action -> DynamoDBv2Action -> Bool
== :: DynamoDBv2Action -> DynamoDBv2Action -> Bool
$c== :: DynamoDBv2Action -> DynamoDBv2Action -> Bool
Prelude.Eq, ReadPrec [DynamoDBv2Action]
ReadPrec DynamoDBv2Action
Int -> ReadS DynamoDBv2Action
ReadS [DynamoDBv2Action]
(Int -> ReadS DynamoDBv2Action)
-> ReadS [DynamoDBv2Action]
-> ReadPrec DynamoDBv2Action
-> ReadPrec [DynamoDBv2Action]
-> Read DynamoDBv2Action
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DynamoDBv2Action]
$creadListPrec :: ReadPrec [DynamoDBv2Action]
readPrec :: ReadPrec DynamoDBv2Action
$creadPrec :: ReadPrec DynamoDBv2Action
readList :: ReadS [DynamoDBv2Action]
$creadList :: ReadS [DynamoDBv2Action]
readsPrec :: Int -> ReadS DynamoDBv2Action
$creadsPrec :: Int -> ReadS DynamoDBv2Action
Prelude.Read, Int -> DynamoDBv2Action -> ShowS
[DynamoDBv2Action] -> ShowS
DynamoDBv2Action -> String
(Int -> DynamoDBv2Action -> ShowS)
-> (DynamoDBv2Action -> String)
-> ([DynamoDBv2Action] -> ShowS)
-> Show DynamoDBv2Action
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DynamoDBv2Action] -> ShowS
$cshowList :: [DynamoDBv2Action] -> ShowS
show :: DynamoDBv2Action -> String
$cshow :: DynamoDBv2Action -> String
showsPrec :: Int -> DynamoDBv2Action -> ShowS
$cshowsPrec :: Int -> DynamoDBv2Action -> ShowS
Prelude.Show, (forall x. DynamoDBv2Action -> Rep DynamoDBv2Action x)
-> (forall x. Rep DynamoDBv2Action x -> DynamoDBv2Action)
-> Generic DynamoDBv2Action
forall x. Rep DynamoDBv2Action x -> DynamoDBv2Action
forall x. DynamoDBv2Action -> Rep DynamoDBv2Action x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DynamoDBv2Action x -> DynamoDBv2Action
$cfrom :: forall x. DynamoDBv2Action -> Rep DynamoDBv2Action x
Prelude.Generic)

-- |
-- Create a value of 'DynamoDBv2Action' 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:
--
-- 'payload', 'dynamoDBv2Action_payload' - Undocumented member.
--
-- 'tableName', 'dynamoDBv2Action_tableName' - The name of the DynamoDB table.
newDynamoDBv2Action ::
  -- | 'tableName'
  Prelude.Text ->
  DynamoDBv2Action
newDynamoDBv2Action :: Text -> DynamoDBv2Action
newDynamoDBv2Action Text
pTableName_ =
  DynamoDBv2Action' :: Maybe Payload -> Text -> DynamoDBv2Action
DynamoDBv2Action'
    { $sel:payload:DynamoDBv2Action' :: Maybe Payload
payload = Maybe Payload
forall a. Maybe a
Prelude.Nothing,
      $sel:tableName:DynamoDBv2Action' :: Text
tableName = Text
pTableName_
    }

-- | Undocumented member.
dynamoDBv2Action_payload :: Lens.Lens' DynamoDBv2Action (Prelude.Maybe Payload)
dynamoDBv2Action_payload :: (Maybe Payload -> f (Maybe Payload))
-> DynamoDBv2Action -> f DynamoDBv2Action
dynamoDBv2Action_payload = (DynamoDBv2Action -> Maybe Payload)
-> (DynamoDBv2Action -> Maybe Payload -> DynamoDBv2Action)
-> Lens
     DynamoDBv2Action DynamoDBv2Action (Maybe Payload) (Maybe Payload)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DynamoDBv2Action' {Maybe Payload
payload :: Maybe Payload
$sel:payload:DynamoDBv2Action' :: DynamoDBv2Action -> Maybe Payload
payload} -> Maybe Payload
payload) (\s :: DynamoDBv2Action
s@DynamoDBv2Action' {} Maybe Payload
a -> DynamoDBv2Action
s {$sel:payload:DynamoDBv2Action' :: Maybe Payload
payload = Maybe Payload
a} :: DynamoDBv2Action)

-- | The name of the DynamoDB table.
dynamoDBv2Action_tableName :: Lens.Lens' DynamoDBv2Action Prelude.Text
dynamoDBv2Action_tableName :: (Text -> f Text) -> DynamoDBv2Action -> f DynamoDBv2Action
dynamoDBv2Action_tableName = (DynamoDBv2Action -> Text)
-> (DynamoDBv2Action -> Text -> DynamoDBv2Action)
-> Lens DynamoDBv2Action DynamoDBv2Action Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DynamoDBv2Action' {Text
tableName :: Text
$sel:tableName:DynamoDBv2Action' :: DynamoDBv2Action -> Text
tableName} -> Text
tableName) (\s :: DynamoDBv2Action
s@DynamoDBv2Action' {} Text
a -> DynamoDBv2Action
s {$sel:tableName:DynamoDBv2Action' :: Text
tableName = Text
a} :: DynamoDBv2Action)

instance Core.FromJSON DynamoDBv2Action where
  parseJSON :: Value -> Parser DynamoDBv2Action
parseJSON =
    String
-> (Object -> Parser DynamoDBv2Action)
-> Value
-> Parser DynamoDBv2Action
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"DynamoDBv2Action"
      ( \Object
x ->
          Maybe Payload -> Text -> DynamoDBv2Action
DynamoDBv2Action'
            (Maybe Payload -> Text -> DynamoDBv2Action)
-> Parser (Maybe Payload) -> Parser (Text -> DynamoDBv2Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Payload)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"payload")
            Parser (Text -> DynamoDBv2Action)
-> Parser Text -> Parser DynamoDBv2Action
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"tableName")
      )

instance Prelude.Hashable DynamoDBv2Action

instance Prelude.NFData DynamoDBv2Action

instance Core.ToJSON DynamoDBv2Action where
  toJSON :: DynamoDBv2Action -> Value
toJSON DynamoDBv2Action' {Maybe Payload
Text
tableName :: Text
payload :: Maybe Payload
$sel:tableName:DynamoDBv2Action' :: DynamoDBv2Action -> Text
$sel:payload:DynamoDBv2Action' :: DynamoDBv2Action -> Maybe Payload
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"payload" Text -> Payload -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Payload -> Pair) -> Maybe Payload -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Payload
payload,
            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)
          ]
      )