{-# 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.GlobalAccelerator.Types.PortRange
-- 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.GlobalAccelerator.Types.PortRange where

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

-- | A complex type for a range of ports for a listener.
--
-- /See:/ 'newPortRange' smart constructor.
data PortRange = PortRange'
  { -- | The first port in the range of ports, inclusive.
    PortRange -> Maybe Natural
fromPort :: Prelude.Maybe Prelude.Natural,
    -- | The last port in the range of ports, inclusive.
    PortRange -> Maybe Natural
toPort :: Prelude.Maybe Prelude.Natural
  }
  deriving (PortRange -> PortRange -> Bool
(PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> Bool) -> Eq PortRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PortRange -> PortRange -> Bool
$c/= :: PortRange -> PortRange -> Bool
== :: PortRange -> PortRange -> Bool
$c== :: PortRange -> PortRange -> Bool
Prelude.Eq, ReadPrec [PortRange]
ReadPrec PortRange
Int -> ReadS PortRange
ReadS [PortRange]
(Int -> ReadS PortRange)
-> ReadS [PortRange]
-> ReadPrec PortRange
-> ReadPrec [PortRange]
-> Read PortRange
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PortRange]
$creadListPrec :: ReadPrec [PortRange]
readPrec :: ReadPrec PortRange
$creadPrec :: ReadPrec PortRange
readList :: ReadS [PortRange]
$creadList :: ReadS [PortRange]
readsPrec :: Int -> ReadS PortRange
$creadsPrec :: Int -> ReadS PortRange
Prelude.Read, Int -> PortRange -> ShowS
[PortRange] -> ShowS
PortRange -> String
(Int -> PortRange -> ShowS)
-> (PortRange -> String)
-> ([PortRange] -> ShowS)
-> Show PortRange
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PortRange] -> ShowS
$cshowList :: [PortRange] -> ShowS
show :: PortRange -> String
$cshow :: PortRange -> String
showsPrec :: Int -> PortRange -> ShowS
$cshowsPrec :: Int -> PortRange -> ShowS
Prelude.Show, (forall x. PortRange -> Rep PortRange x)
-> (forall x. Rep PortRange x -> PortRange) -> Generic PortRange
forall x. Rep PortRange x -> PortRange
forall x. PortRange -> Rep PortRange x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PortRange x -> PortRange
$cfrom :: forall x. PortRange -> Rep PortRange x
Prelude.Generic)

-- |
-- Create a value of 'PortRange' 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:
--
-- 'fromPort', 'portRange_fromPort' - The first port in the range of ports, inclusive.
--
-- 'toPort', 'portRange_toPort' - The last port in the range of ports, inclusive.
newPortRange ::
  PortRange
newPortRange :: PortRange
newPortRange =
  PortRange' :: Maybe Natural -> Maybe Natural -> PortRange
PortRange'
    { $sel:fromPort:PortRange' :: Maybe Natural
fromPort = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:toPort:PortRange' :: Maybe Natural
toPort = Maybe Natural
forall a. Maybe a
Prelude.Nothing
    }

-- | The first port in the range of ports, inclusive.
portRange_fromPort :: Lens.Lens' PortRange (Prelude.Maybe Prelude.Natural)
portRange_fromPort :: (Maybe Natural -> f (Maybe Natural)) -> PortRange -> f PortRange
portRange_fromPort = (PortRange -> Maybe Natural)
-> (PortRange -> Maybe Natural -> PortRange)
-> Lens PortRange PortRange (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PortRange' {Maybe Natural
fromPort :: Maybe Natural
$sel:fromPort:PortRange' :: PortRange -> Maybe Natural
fromPort} -> Maybe Natural
fromPort) (\s :: PortRange
s@PortRange' {} Maybe Natural
a -> PortRange
s {$sel:fromPort:PortRange' :: Maybe Natural
fromPort = Maybe Natural
a} :: PortRange)

-- | The last port in the range of ports, inclusive.
portRange_toPort :: Lens.Lens' PortRange (Prelude.Maybe Prelude.Natural)
portRange_toPort :: (Maybe Natural -> f (Maybe Natural)) -> PortRange -> f PortRange
portRange_toPort = (PortRange -> Maybe Natural)
-> (PortRange -> Maybe Natural -> PortRange)
-> Lens PortRange PortRange (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PortRange' {Maybe Natural
toPort :: Maybe Natural
$sel:toPort:PortRange' :: PortRange -> Maybe Natural
toPort} -> Maybe Natural
toPort) (\s :: PortRange
s@PortRange' {} Maybe Natural
a -> PortRange
s {$sel:toPort:PortRange' :: Maybe Natural
toPort = Maybe Natural
a} :: PortRange)

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

instance Prelude.Hashable PortRange

instance Prelude.NFData PortRange

instance Core.ToJSON PortRange where
  toJSON :: PortRange -> Value
toJSON PortRange' {Maybe Natural
toPort :: Maybe Natural
fromPort :: Maybe Natural
$sel:toPort:PortRange' :: PortRange -> Maybe Natural
$sel:fromPort:PortRange' :: PortRange -> Maybe Natural
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"FromPort" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
fromPort,
            (Text
"ToPort" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
toPort
          ]
      )