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

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

-- | Specifies the @actions@ to be performed when the @condition@ evaluates
-- to TRUE.
--
-- /See:/ 'newEvent' smart constructor.
data Event = Event'
  { -- | The actions to be performed.
    Event -> Maybe [Action]
actions :: Prelude.Maybe [Action],
    -- | Optional. The Boolean expression that, when TRUE, causes the @actions@
    -- to be performed. If not present, the actions are performed (=TRUE). If
    -- the expression result is not a Boolean value, the actions are not
    -- performed (=FALSE).
    Event -> Maybe Text
condition :: Prelude.Maybe Prelude.Text,
    -- | The name of the event.
    Event -> Text
eventName :: Prelude.Text
  }
  deriving (Event -> Event -> Bool
(Event -> Event -> Bool) -> (Event -> Event -> Bool) -> Eq Event
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Event -> Event -> Bool
$c/= :: Event -> Event -> Bool
== :: Event -> Event -> Bool
$c== :: Event -> Event -> Bool
Prelude.Eq, ReadPrec [Event]
ReadPrec Event
Int -> ReadS Event
ReadS [Event]
(Int -> ReadS Event)
-> ReadS [Event]
-> ReadPrec Event
-> ReadPrec [Event]
-> Read Event
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Event]
$creadListPrec :: ReadPrec [Event]
readPrec :: ReadPrec Event
$creadPrec :: ReadPrec Event
readList :: ReadS [Event]
$creadList :: ReadS [Event]
readsPrec :: Int -> ReadS Event
$creadsPrec :: Int -> ReadS Event
Prelude.Read, Int -> Event -> ShowS
[Event] -> ShowS
Event -> String
(Int -> Event -> ShowS)
-> (Event -> String) -> ([Event] -> ShowS) -> Show Event
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Event] -> ShowS
$cshowList :: [Event] -> ShowS
show :: Event -> String
$cshow :: Event -> String
showsPrec :: Int -> Event -> ShowS
$cshowsPrec :: Int -> Event -> ShowS
Prelude.Show, (forall x. Event -> Rep Event x)
-> (forall x. Rep Event x -> Event) -> Generic Event
forall x. Rep Event x -> Event
forall x. Event -> Rep Event x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Event x -> Event
$cfrom :: forall x. Event -> Rep Event x
Prelude.Generic)

-- |
-- Create a value of 'Event' 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:
--
-- 'actions', 'event_actions' - The actions to be performed.
--
-- 'condition', 'event_condition' - Optional. The Boolean expression that, when TRUE, causes the @actions@
-- to be performed. If not present, the actions are performed (=TRUE). If
-- the expression result is not a Boolean value, the actions are not
-- performed (=FALSE).
--
-- 'eventName', 'event_eventName' - The name of the event.
newEvent ::
  -- | 'eventName'
  Prelude.Text ->
  Event
newEvent :: Text -> Event
newEvent Text
pEventName_ =
  Event' :: Maybe [Action] -> Maybe Text -> Text -> Event
Event'
    { $sel:actions:Event' :: Maybe [Action]
actions = Maybe [Action]
forall a. Maybe a
Prelude.Nothing,
      $sel:condition:Event' :: Maybe Text
condition = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:eventName:Event' :: Text
eventName = Text
pEventName_
    }

-- | The actions to be performed.
event_actions :: Lens.Lens' Event (Prelude.Maybe [Action])
event_actions :: (Maybe [Action] -> f (Maybe [Action])) -> Event -> f Event
event_actions = (Event -> Maybe [Action])
-> (Event -> Maybe [Action] -> Event)
-> Lens Event Event (Maybe [Action]) (Maybe [Action])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Event' {Maybe [Action]
actions :: Maybe [Action]
$sel:actions:Event' :: Event -> Maybe [Action]
actions} -> Maybe [Action]
actions) (\s :: Event
s@Event' {} Maybe [Action]
a -> Event
s {$sel:actions:Event' :: Maybe [Action]
actions = Maybe [Action]
a} :: Event) ((Maybe [Action] -> f (Maybe [Action])) -> Event -> f Event)
-> ((Maybe [Action] -> f (Maybe [Action]))
    -> Maybe [Action] -> f (Maybe [Action]))
-> (Maybe [Action] -> f (Maybe [Action]))
-> Event
-> f Event
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso [Action] [Action] [Action] [Action]
-> Iso
     (Maybe [Action]) (Maybe [Action]) (Maybe [Action]) (Maybe [Action])
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 [Action] [Action] [Action] [Action]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Optional. The Boolean expression that, when TRUE, causes the @actions@
-- to be performed. If not present, the actions are performed (=TRUE). If
-- the expression result is not a Boolean value, the actions are not
-- performed (=FALSE).
event_condition :: Lens.Lens' Event (Prelude.Maybe Prelude.Text)
event_condition :: (Maybe Text -> f (Maybe Text)) -> Event -> f Event
event_condition = (Event -> Maybe Text)
-> (Event -> Maybe Text -> Event)
-> Lens Event Event (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Event' {Maybe Text
condition :: Maybe Text
$sel:condition:Event' :: Event -> Maybe Text
condition} -> Maybe Text
condition) (\s :: Event
s@Event' {} Maybe Text
a -> Event
s {$sel:condition:Event' :: Maybe Text
condition = Maybe Text
a} :: Event)

-- | The name of the event.
event_eventName :: Lens.Lens' Event Prelude.Text
event_eventName :: (Text -> f Text) -> Event -> f Event
event_eventName = (Event -> Text)
-> (Event -> Text -> Event) -> Lens Event Event Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Event' {Text
eventName :: Text
$sel:eventName:Event' :: Event -> Text
eventName} -> Text
eventName) (\s :: Event
s@Event' {} Text
a -> Event
s {$sel:eventName:Event' :: Text
eventName = Text
a} :: Event)

instance Core.FromJSON Event where
  parseJSON :: Value -> Parser Event
parseJSON =
    String -> (Object -> Parser Event) -> Value -> Parser Event
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"Event"
      ( \Object
x ->
          Maybe [Action] -> Maybe Text -> Text -> Event
Event'
            (Maybe [Action] -> Maybe Text -> Text -> Event)
-> Parser (Maybe [Action]) -> Parser (Maybe Text -> Text -> Event)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe (Maybe [Action]))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"actions" Parser (Maybe (Maybe [Action]))
-> Maybe [Action] -> Parser (Maybe [Action])
forall a. Parser (Maybe a) -> a -> Parser a
Core..!= Maybe [Action]
forall a. Monoid a => a
Prelude.mempty)
            Parser (Maybe Text -> Text -> Event)
-> Parser (Maybe Text) -> Parser (Text -> Event)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"condition")
            Parser (Text -> Event) -> Parser Text -> Parser Event
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
"eventName")
      )

instance Prelude.Hashable Event

instance Prelude.NFData Event

instance Core.ToJSON Event where
  toJSON :: Event -> Value
toJSON Event' {Maybe [Action]
Maybe Text
Text
eventName :: Text
condition :: Maybe Text
actions :: Maybe [Action]
$sel:eventName:Event' :: Event -> Text
$sel:condition:Event' :: Event -> Maybe Text
$sel:actions:Event' :: Event -> Maybe [Action]
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"actions" Text -> [Action] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) ([Action] -> Pair) -> Maybe [Action] -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Action]
actions,
            (Text
"condition" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
condition,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"eventName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
eventName)
          ]
      )