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

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
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
(AttributePayload -> AttributePayload -> Bool)
-> (AttributePayload -> AttributePayload -> Bool)
-> Eq AttributePayload
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]
(Int -> ReadS AttributePayload)
-> ReadS [AttributePayload]
-> ReadPrec AttributePayload
-> ReadPrec [AttributePayload]
-> Read 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
(Int -> AttributePayload -> ShowS)
-> (AttributePayload -> String)
-> ([AttributePayload] -> ShowS)
-> Show AttributePayload
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. AttributePayload -> Rep AttributePayload x)
-> (forall x. Rep AttributePayload x -> AttributePayload)
-> Generic AttributePayload
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' :: Maybe (HashMap Text Text) -> Maybe Bool -> AttributePayload
AttributePayload'
    { $sel:attributes:AttributePayload' :: Maybe (HashMap Text Text)
attributes = Maybe (HashMap Text Text)
forall a. Maybe a
Prelude.Nothing,
      $sel:merge:AttributePayload' :: Maybe Bool
merge = Maybe Bool
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 :: (Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
-> AttributePayload -> f AttributePayload
attributePayload_attributes = (AttributePayload -> Maybe (HashMap Text Text))
-> (AttributePayload
    -> Maybe (HashMap Text Text) -> AttributePayload)
-> Lens
     AttributePayload
     AttributePayload
     (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 (\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) ((Maybe (HashMap Text Text) -> f (Maybe (HashMap Text Text)))
 -> AttributePayload -> f AttributePayload)
-> ((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)))
-> AttributePayload
-> f AttributePayload
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

-- | 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 :: (Maybe Bool -> f (Maybe Bool))
-> AttributePayload -> f AttributePayload
attributePayload_merge = (AttributePayload -> Maybe Bool)
-> (AttributePayload -> Maybe Bool -> AttributePayload)
-> Lens AttributePayload AttributePayload (Maybe Bool) (Maybe Bool)
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 Core.FromJSON AttributePayload where
  parseJSON :: Value -> Parser AttributePayload
parseJSON =
    String
-> (Object -> Parser AttributePayload)
-> Value
-> Parser AttributePayload
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"AttributePayload"
      ( \Object
x ->
          Maybe (HashMap Text Text) -> Maybe Bool -> AttributePayload
AttributePayload'
            (Maybe (HashMap Text Text) -> Maybe Bool -> AttributePayload)
-> Parser (Maybe (HashMap Text Text))
-> Parser (Maybe Bool -> AttributePayload)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe (Maybe (HashMap Text Text)))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"attributes" Parser (Maybe (Maybe (HashMap Text Text)))
-> Maybe (HashMap Text Text) -> Parser (Maybe (HashMap Text Text))
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= Maybe (HashMap Text Text)
forall a. Monoid a => a
Prelude.mempty)
            Parser (Maybe Bool -> AttributePayload)
-> Parser (Maybe Bool) -> Parser AttributePayload
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"merge")
      )

instance Prelude.Hashable AttributePayload

instance Prelude.NFData AttributePayload

instance Core.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
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"attributes" 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)
attributes,
            (Text
"merge" Text -> Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Bool -> Pair) -> Maybe Bool -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Bool
merge
          ]
      )