{-# 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.Payload
-- 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.Payload where

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

-- | Information needed to configure the payload.
--
-- By default, AWS IoT Events generates a standard payload in JSON for any
-- action. This action payload contains all attribute-value pairs that have
-- the information about the detector model instance and the event
-- triggered the action. To configure the action payload, you can use
-- @contentExpression@.
--
-- /See:/ 'newPayload' smart constructor.
data Payload = Payload'
  { -- | The content of the payload. You can use a string expression that
    -- includes quoted strings (@\'\<string>\'@), variables
    -- (@$variable.\<variable-name>@), input values
    -- (@$input.\<input-name>.\<path-to-datum>@), string concatenations, and
    -- quoted strings that contain @${}@ as the content. The recommended
    -- maximum size of a content expression is 1 KB.
    Payload -> Text
contentExpression :: Prelude.Text,
    -- | The value of the payload type can be either @STRING@ or @JSON@.
    Payload -> PayloadType
type' :: PayloadType
  }
  deriving (Payload -> Payload -> Bool
(Payload -> Payload -> Bool)
-> (Payload -> Payload -> Bool) -> Eq Payload
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Payload -> Payload -> Bool
$c/= :: Payload -> Payload -> Bool
== :: Payload -> Payload -> Bool
$c== :: Payload -> Payload -> Bool
Prelude.Eq, ReadPrec [Payload]
ReadPrec Payload
Int -> ReadS Payload
ReadS [Payload]
(Int -> ReadS Payload)
-> ReadS [Payload]
-> ReadPrec Payload
-> ReadPrec [Payload]
-> Read Payload
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Payload]
$creadListPrec :: ReadPrec [Payload]
readPrec :: ReadPrec Payload
$creadPrec :: ReadPrec Payload
readList :: ReadS [Payload]
$creadList :: ReadS [Payload]
readsPrec :: Int -> ReadS Payload
$creadsPrec :: Int -> ReadS Payload
Prelude.Read, Int -> Payload -> ShowS
[Payload] -> ShowS
Payload -> String
(Int -> Payload -> ShowS)
-> (Payload -> String) -> ([Payload] -> ShowS) -> Show Payload
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Payload] -> ShowS
$cshowList :: [Payload] -> ShowS
show :: Payload -> String
$cshow :: Payload -> String
showsPrec :: Int -> Payload -> ShowS
$cshowsPrec :: Int -> Payload -> ShowS
Prelude.Show, (forall x. Payload -> Rep Payload x)
-> (forall x. Rep Payload x -> Payload) -> Generic Payload
forall x. Rep Payload x -> Payload
forall x. Payload -> Rep Payload x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Payload x -> Payload
$cfrom :: forall x. Payload -> Rep Payload x
Prelude.Generic)

-- |
-- Create a value of 'Payload' 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:
--
-- 'contentExpression', 'payload_contentExpression' - The content of the payload. You can use a string expression that
-- includes quoted strings (@\'\<string>\'@), variables
-- (@$variable.\<variable-name>@), input values
-- (@$input.\<input-name>.\<path-to-datum>@), string concatenations, and
-- quoted strings that contain @${}@ as the content. The recommended
-- maximum size of a content expression is 1 KB.
--
-- 'type'', 'payload_type' - The value of the payload type can be either @STRING@ or @JSON@.
newPayload ::
  -- | 'contentExpression'
  Prelude.Text ->
  -- | 'type''
  PayloadType ->
  Payload
newPayload :: Text -> PayloadType -> Payload
newPayload Text
pContentExpression_ PayloadType
pType_ =
  Payload' :: Text -> PayloadType -> Payload
Payload'
    { $sel:contentExpression:Payload' :: Text
contentExpression = Text
pContentExpression_,
      $sel:type':Payload' :: PayloadType
type' = PayloadType
pType_
    }

-- | The content of the payload. You can use a string expression that
-- includes quoted strings (@\'\<string>\'@), variables
-- (@$variable.\<variable-name>@), input values
-- (@$input.\<input-name>.\<path-to-datum>@), string concatenations, and
-- quoted strings that contain @${}@ as the content. The recommended
-- maximum size of a content expression is 1 KB.
payload_contentExpression :: Lens.Lens' Payload Prelude.Text
payload_contentExpression :: (Text -> f Text) -> Payload -> f Payload
payload_contentExpression = (Payload -> Text)
-> (Payload -> Text -> Payload) -> Lens Payload Payload Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Payload' {Text
contentExpression :: Text
$sel:contentExpression:Payload' :: Payload -> Text
contentExpression} -> Text
contentExpression) (\s :: Payload
s@Payload' {} Text
a -> Payload
s {$sel:contentExpression:Payload' :: Text
contentExpression = Text
a} :: Payload)

-- | The value of the payload type can be either @STRING@ or @JSON@.
payload_type :: Lens.Lens' Payload PayloadType
payload_type :: (PayloadType -> f PayloadType) -> Payload -> f Payload
payload_type = (Payload -> PayloadType)
-> (Payload -> PayloadType -> Payload)
-> Lens Payload Payload PayloadType PayloadType
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Payload' {PayloadType
type' :: PayloadType
$sel:type':Payload' :: Payload -> PayloadType
type'} -> PayloadType
type') (\s :: Payload
s@Payload' {} PayloadType
a -> Payload
s {$sel:type':Payload' :: PayloadType
type' = PayloadType
a} :: Payload)

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

instance Prelude.Hashable Payload

instance Prelude.NFData Payload

instance Core.ToJSON Payload where
  toJSON :: Payload -> Value
toJSON Payload' {Text
PayloadType
type' :: PayloadType
contentExpression :: Text
$sel:type':Payload' :: Payload -> PayloadType
$sel:contentExpression:Payload' :: Payload -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"contentExpression" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
contentExpression),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"type" Text -> PayloadType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= PayloadType
type')
          ]
      )