{-# 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.IoT.Types.AttributePayload
-- 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.IoT.Types.AttributePayload where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | The attribute payload.
--
-- /See:/ 'newAttributePayload' smart constructor.
data AttributePayload = AttributePayload'
  { -- | A JSON string containing up to three key-value pair in JSON format. For
    -- example:
    --
    -- @{\\\"attributes\\\":{\\\"string1\\\":\\\"string2\\\"}}@
    AttributePayload -> Maybe (HashMap Text Text)
attributes :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text),
    -- | Specifies whether the list of attributes provided in the
    -- @AttributePayload@ is merged with the attributes stored in the registry,
    -- instead of overwriting them.
    --
    -- To remove an attribute, call @UpdateThing@ with an empty attribute
    -- value.
    --
    -- The @merge@ attribute is only valid when calling @UpdateThing@ or
    -- @UpdateThingGroup@.
    AttributePayload -> Maybe Bool
merge :: Prelude.Maybe Prelude.Bool
  }
  deriving (AttributePayload -> AttributePayload -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AttributePayload -> AttributePayload -> Bool
$c/= :: AttributePayload -> AttributePayload -> Bool
== :: AttributePayload -> AttributePayload -> Bool
$c== :: AttributePayload -> AttributePayload -> Bool
Prelude.Eq, ReadPrec [AttributePayload]
ReadPrec AttributePayload
Int -> ReadS AttributePayload
ReadS [AttributePayload]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AttributePayload]
$creadListPrec :: ReadPrec [AttributePayload]
readPrec :: ReadPrec AttributePayload
$creadPrec :: ReadPrec AttributePayload
readList :: ReadS [AttributePayload]
$creadList :: ReadS [AttributePayload]
readsPrec :: Int -> ReadS AttributePayload
$creadsPrec :: Int -> ReadS AttributePayload
Prelude.Read, Int -> AttributePayload -> ShowS
[AttributePayload] -> ShowS
AttributePayload -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AttributePayload] -> ShowS
$cshowList :: [AttributePayload] -> ShowS
show :: AttributePayload -> String
$cshow :: AttributePayload -> String
showsPrec :: Int -> AttributePayload -> ShowS
$cshowsPrec :: Int -> AttributePayload -> ShowS
Prelude.Show, forall x. Rep AttributePayload x -> AttributePayload
forall x. AttributePayload -> Rep AttributePayload x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AttributePayload x -> AttributePayload
$cfrom :: forall x. AttributePayload -> Rep AttributePayload x
Prelude.Generic)

-- |
-- Create a value of 'AttributePayload' 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:
--
-- 'attributes', 'attributePayload_attributes' - A JSON string containing up to three key-value pair in JSON format. For
-- example:
--
-- @{\\\"attributes\\\":{\\\"string1\\\":\\\"string2\\\"}}@
--
-- 'merge', 'attributePayload_merge' - Specifies whether the list of attributes provided in the
-- @AttributePayload@ is merged with the attributes stored in the registry,
-- instead of overwriting them.
--
-- To remove an attribute, call @UpdateThing@ with an empty attribute
-- value.
--
-- The @merge@ attribute is only valid when calling @UpdateThing@ or
-- @UpdateThingGroup@.
newAttributePayload ::
  AttributePayload
newAttributePayload :: AttributePayload
newAttributePayload =
  AttributePayload'
    { $sel:attributes:AttributePayload' :: Maybe (HashMap Text Text)
attributes = forall a. Maybe a
Prelude.Nothing,
      $sel:merge:AttributePayload' :: Maybe Bool
merge = forall a. Maybe a
Prelude.Nothing
    }

-- | A JSON string containing up to three key-value pair in JSON format. For
-- example:
--
-- @{\\\"attributes\\\":{\\\"string1\\\":\\\"string2\\\"}}@
attributePayload_attributes :: Lens.Lens' AttributePayload (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
attributePayload_attributes :: Lens' AttributePayload (Maybe (HashMap Text Text))
attributePayload_attributes = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AttributePayload' {Maybe (HashMap Text Text)
attributes :: Maybe (HashMap Text Text)
$sel:attributes:AttributePayload' :: AttributePayload -> Maybe (HashMap Text Text)
attributes} -> Maybe (HashMap Text Text)
attributes) (\s :: AttributePayload
s@AttributePayload' {} Maybe (HashMap Text Text)
a -> AttributePayload
s {$sel:attributes:AttributePayload' :: Maybe (HashMap Text Text)
attributes = Maybe (HashMap Text Text)
a} :: AttributePayload) 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

-- | Specifies whether the list of attributes provided in the
-- @AttributePayload@ is merged with the attributes stored in the registry,
-- instead of overwriting them.
--
-- To remove an attribute, call @UpdateThing@ with an empty attribute
-- value.
--
-- The @merge@ attribute is only valid when calling @UpdateThing@ or
-- @UpdateThingGroup@.
attributePayload_merge :: Lens.Lens' AttributePayload (Prelude.Maybe Prelude.Bool)
attributePayload_merge :: Lens' AttributePayload (Maybe Bool)
attributePayload_merge = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\AttributePayload' {Maybe Bool
merge :: Maybe Bool
$sel:merge:AttributePayload' :: AttributePayload -> Maybe Bool
merge} -> Maybe Bool
merge) (\s :: AttributePayload
s@AttributePayload' {} Maybe Bool
a -> AttributePayload
s {$sel:merge:AttributePayload' :: Maybe Bool
merge = Maybe Bool
a} :: AttributePayload)

instance Data.FromJSON AttributePayload where
  parseJSON :: Value -> Parser AttributePayload
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"AttributePayload"
      ( \Object
x ->
          Maybe (HashMap Text Text) -> Maybe Bool -> AttributePayload
AttributePayload'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"attributes" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"merge")
      )

instance Prelude.Hashable AttributePayload where
  hashWithSalt :: Int -> AttributePayload -> Int
hashWithSalt Int
_salt AttributePayload' {Maybe Bool
Maybe (HashMap Text Text)
merge :: Maybe Bool
attributes :: Maybe (HashMap Text Text)
$sel:merge:AttributePayload' :: AttributePayload -> Maybe Bool
$sel:attributes:AttributePayload' :: AttributePayload -> Maybe (HashMap Text Text)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (HashMap Text Text)
attributes
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Bool
merge

instance Prelude.NFData AttributePayload where
  rnf :: AttributePayload -> ()
rnf AttributePayload' {Maybe Bool
Maybe (HashMap Text Text)
merge :: Maybe Bool
attributes :: Maybe (HashMap Text Text)
$sel:merge:AttributePayload' :: AttributePayload -> Maybe Bool
$sel:attributes:AttributePayload' :: AttributePayload -> Maybe (HashMap Text Text)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (HashMap Text Text)
attributes
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Bool
merge

instance Data.ToJSON AttributePayload where
  toJSON :: AttributePayload -> Value
toJSON AttributePayload' {Maybe Bool
Maybe (HashMap Text Text)
merge :: Maybe Bool
attributes :: Maybe (HashMap Text Text)
$sel:merge:AttributePayload' :: AttributePayload -> Maybe Bool
$sel:attributes:AttributePayload' :: AttributePayload -> Maybe (HashMap Text Text)
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"attributes" 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)
attributes,
            (Key
"merge" 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 Bool
merge
          ]
      )