{-# 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.DynamoDB.Types.GlobalSecondaryIndex
-- 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.DynamoDB.Types.GlobalSecondaryIndex where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.DynamoDB.Types.AttributeValue
import Amazonka.DynamoDB.Types.KeySchemaElement
import Amazonka.DynamoDB.Types.Projection
import Amazonka.DynamoDB.Types.ProvisionedThroughput
import Amazonka.DynamoDB.Types.WriteRequest
import qualified Amazonka.Prelude as Prelude

-- | Represents the properties of a global secondary index.
--
-- /See:/ 'newGlobalSecondaryIndex' smart constructor.
data GlobalSecondaryIndex = GlobalSecondaryIndex'
  { -- | Represents the provisioned throughput settings for the specified global
    -- secondary index.
    --
    -- For current minimum and maximum provisioned throughput values, see
    -- <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html Service, Account, and Table Quotas>
    -- in the /Amazon DynamoDB Developer Guide/.
    GlobalSecondaryIndex -> Maybe ProvisionedThroughput
provisionedThroughput :: Prelude.Maybe ProvisionedThroughput,
    -- | The name of the global secondary index. The name must be unique among
    -- all other indexes on this table.
    GlobalSecondaryIndex -> Text
indexName :: Prelude.Text,
    -- | The complete key schema for a global secondary index, which consists of
    -- one or more pairs of attribute names and key types:
    --
    -- -   @HASH@ - partition key
    --
    -- -   @RANGE@ - sort key
    --
    -- The partition key of an item is also known as its /hash attribute/. The
    -- term \"hash attribute\" derives from DynamoDB\'s usage of an internal
    -- hash function to evenly distribute data items across partitions, based
    -- on their partition key values.
    --
    -- The sort key of an item is also known as its /range attribute/. The term
    -- \"range attribute\" derives from the way DynamoDB stores items with the
    -- same partition key physically close together, in sorted order by the
    -- sort key value.
    GlobalSecondaryIndex -> NonEmpty KeySchemaElement
keySchema :: Prelude.NonEmpty KeySchemaElement,
    -- | Represents attributes that are copied (projected) from the table into
    -- the global secondary index. These are in addition to the primary key
    -- attributes and index key attributes, which are automatically projected.
    GlobalSecondaryIndex -> Projection
projection :: Projection
  }
  deriving (GlobalSecondaryIndex -> GlobalSecondaryIndex -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GlobalSecondaryIndex -> GlobalSecondaryIndex -> Bool
$c/= :: GlobalSecondaryIndex -> GlobalSecondaryIndex -> Bool
== :: GlobalSecondaryIndex -> GlobalSecondaryIndex -> Bool
$c== :: GlobalSecondaryIndex -> GlobalSecondaryIndex -> Bool
Prelude.Eq, ReadPrec [GlobalSecondaryIndex]
ReadPrec GlobalSecondaryIndex
Int -> ReadS GlobalSecondaryIndex
ReadS [GlobalSecondaryIndex]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GlobalSecondaryIndex]
$creadListPrec :: ReadPrec [GlobalSecondaryIndex]
readPrec :: ReadPrec GlobalSecondaryIndex
$creadPrec :: ReadPrec GlobalSecondaryIndex
readList :: ReadS [GlobalSecondaryIndex]
$creadList :: ReadS [GlobalSecondaryIndex]
readsPrec :: Int -> ReadS GlobalSecondaryIndex
$creadsPrec :: Int -> ReadS GlobalSecondaryIndex
Prelude.Read, Int -> GlobalSecondaryIndex -> ShowS
[GlobalSecondaryIndex] -> ShowS
GlobalSecondaryIndex -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GlobalSecondaryIndex] -> ShowS
$cshowList :: [GlobalSecondaryIndex] -> ShowS
show :: GlobalSecondaryIndex -> String
$cshow :: GlobalSecondaryIndex -> String
showsPrec :: Int -> GlobalSecondaryIndex -> ShowS
$cshowsPrec :: Int -> GlobalSecondaryIndex -> ShowS
Prelude.Show, forall x. Rep GlobalSecondaryIndex x -> GlobalSecondaryIndex
forall x. GlobalSecondaryIndex -> Rep GlobalSecondaryIndex x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GlobalSecondaryIndex x -> GlobalSecondaryIndex
$cfrom :: forall x. GlobalSecondaryIndex -> Rep GlobalSecondaryIndex x
Prelude.Generic)

-- |
-- Create a value of 'GlobalSecondaryIndex' 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:
--
-- 'provisionedThroughput', 'globalSecondaryIndex_provisionedThroughput' - Represents the provisioned throughput settings for the specified global
-- secondary index.
--
-- For current minimum and maximum provisioned throughput values, see
-- <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html Service, Account, and Table Quotas>
-- in the /Amazon DynamoDB Developer Guide/.
--
-- 'indexName', 'globalSecondaryIndex_indexName' - The name of the global secondary index. The name must be unique among
-- all other indexes on this table.
--
-- 'keySchema', 'globalSecondaryIndex_keySchema' - The complete key schema for a global secondary index, which consists of
-- one or more pairs of attribute names and key types:
--
-- -   @HASH@ - partition key
--
-- -   @RANGE@ - sort key
--
-- The partition key of an item is also known as its /hash attribute/. The
-- term \"hash attribute\" derives from DynamoDB\'s usage of an internal
-- hash function to evenly distribute data items across partitions, based
-- on their partition key values.
--
-- The sort key of an item is also known as its /range attribute/. The term
-- \"range attribute\" derives from the way DynamoDB stores items with the
-- same partition key physically close together, in sorted order by the
-- sort key value.
--
-- 'projection', 'globalSecondaryIndex_projection' - Represents attributes that are copied (projected) from the table into
-- the global secondary index. These are in addition to the primary key
-- attributes and index key attributes, which are automatically projected.
newGlobalSecondaryIndex ::
  -- | 'indexName'
  Prelude.Text ->
  -- | 'keySchema'
  Prelude.NonEmpty KeySchemaElement ->
  -- | 'projection'
  Projection ->
  GlobalSecondaryIndex
newGlobalSecondaryIndex :: Text
-> NonEmpty KeySchemaElement -> Projection -> GlobalSecondaryIndex
newGlobalSecondaryIndex
  Text
pIndexName_
  NonEmpty KeySchemaElement
pKeySchema_
  Projection
pProjection_ =
    GlobalSecondaryIndex'
      { $sel:provisionedThroughput:GlobalSecondaryIndex' :: Maybe ProvisionedThroughput
provisionedThroughput =
          forall a. Maybe a
Prelude.Nothing,
        $sel:indexName:GlobalSecondaryIndex' :: Text
indexName = Text
pIndexName_,
        $sel:keySchema:GlobalSecondaryIndex' :: NonEmpty KeySchemaElement
keySchema = forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced forall t b. AReview t b -> b -> t
Lens.# NonEmpty KeySchemaElement
pKeySchema_,
        $sel:projection:GlobalSecondaryIndex' :: Projection
projection = Projection
pProjection_
      }

-- | Represents the provisioned throughput settings for the specified global
-- secondary index.
--
-- For current minimum and maximum provisioned throughput values, see
-- <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html Service, Account, and Table Quotas>
-- in the /Amazon DynamoDB Developer Guide/.
globalSecondaryIndex_provisionedThroughput :: Lens.Lens' GlobalSecondaryIndex (Prelude.Maybe ProvisionedThroughput)
globalSecondaryIndex_provisionedThroughput :: Lens' GlobalSecondaryIndex (Maybe ProvisionedThroughput)
globalSecondaryIndex_provisionedThroughput = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GlobalSecondaryIndex' {Maybe ProvisionedThroughput
provisionedThroughput :: Maybe ProvisionedThroughput
$sel:provisionedThroughput:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Maybe ProvisionedThroughput
provisionedThroughput} -> Maybe ProvisionedThroughput
provisionedThroughput) (\s :: GlobalSecondaryIndex
s@GlobalSecondaryIndex' {} Maybe ProvisionedThroughput
a -> GlobalSecondaryIndex
s {$sel:provisionedThroughput:GlobalSecondaryIndex' :: Maybe ProvisionedThroughput
provisionedThroughput = Maybe ProvisionedThroughput
a} :: GlobalSecondaryIndex)

-- | The name of the global secondary index. The name must be unique among
-- all other indexes on this table.
globalSecondaryIndex_indexName :: Lens.Lens' GlobalSecondaryIndex Prelude.Text
globalSecondaryIndex_indexName :: Lens' GlobalSecondaryIndex Text
globalSecondaryIndex_indexName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GlobalSecondaryIndex' {Text
indexName :: Text
$sel:indexName:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Text
indexName} -> Text
indexName) (\s :: GlobalSecondaryIndex
s@GlobalSecondaryIndex' {} Text
a -> GlobalSecondaryIndex
s {$sel:indexName:GlobalSecondaryIndex' :: Text
indexName = Text
a} :: GlobalSecondaryIndex)

-- | The complete key schema for a global secondary index, which consists of
-- one or more pairs of attribute names and key types:
--
-- -   @HASH@ - partition key
--
-- -   @RANGE@ - sort key
--
-- The partition key of an item is also known as its /hash attribute/. The
-- term \"hash attribute\" derives from DynamoDB\'s usage of an internal
-- hash function to evenly distribute data items across partitions, based
-- on their partition key values.
--
-- The sort key of an item is also known as its /range attribute/. The term
-- \"range attribute\" derives from the way DynamoDB stores items with the
-- same partition key physically close together, in sorted order by the
-- sort key value.
globalSecondaryIndex_keySchema :: Lens.Lens' GlobalSecondaryIndex (Prelude.NonEmpty KeySchemaElement)
globalSecondaryIndex_keySchema :: Lens' GlobalSecondaryIndex (NonEmpty KeySchemaElement)
globalSecondaryIndex_keySchema = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GlobalSecondaryIndex' {NonEmpty KeySchemaElement
keySchema :: NonEmpty KeySchemaElement
$sel:keySchema:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> NonEmpty KeySchemaElement
keySchema} -> NonEmpty KeySchemaElement
keySchema) (\s :: GlobalSecondaryIndex
s@GlobalSecondaryIndex' {} NonEmpty KeySchemaElement
a -> GlobalSecondaryIndex
s {$sel:keySchema:GlobalSecondaryIndex' :: NonEmpty KeySchemaElement
keySchema = NonEmpty KeySchemaElement
a} :: GlobalSecondaryIndex) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Represents attributes that are copied (projected) from the table into
-- the global secondary index. These are in addition to the primary key
-- attributes and index key attributes, which are automatically projected.
globalSecondaryIndex_projection :: Lens.Lens' GlobalSecondaryIndex Projection
globalSecondaryIndex_projection :: Lens' GlobalSecondaryIndex Projection
globalSecondaryIndex_projection = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GlobalSecondaryIndex' {Projection
projection :: Projection
$sel:projection:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Projection
projection} -> Projection
projection) (\s :: GlobalSecondaryIndex
s@GlobalSecondaryIndex' {} Projection
a -> GlobalSecondaryIndex
s {$sel:projection:GlobalSecondaryIndex' :: Projection
projection = Projection
a} :: GlobalSecondaryIndex)

instance Data.FromJSON GlobalSecondaryIndex where
  parseJSON :: Value -> Parser GlobalSecondaryIndex
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"GlobalSecondaryIndex"
      ( \Object
x ->
          Maybe ProvisionedThroughput
-> Text
-> NonEmpty KeySchemaElement
-> Projection
-> GlobalSecondaryIndex
GlobalSecondaryIndex'
            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
"ProvisionedThroughput")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"IndexName")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"KeySchema")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"Projection")
      )

instance Prelude.Hashable GlobalSecondaryIndex where
  hashWithSalt :: Int -> GlobalSecondaryIndex -> Int
hashWithSalt Int
_salt GlobalSecondaryIndex' {Maybe ProvisionedThroughput
NonEmpty KeySchemaElement
Text
Projection
projection :: Projection
keySchema :: NonEmpty KeySchemaElement
indexName :: Text
provisionedThroughput :: Maybe ProvisionedThroughput
$sel:projection:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Projection
$sel:keySchema:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> NonEmpty KeySchemaElement
$sel:indexName:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Text
$sel:provisionedThroughput:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Maybe ProvisionedThroughput
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe ProvisionedThroughput
provisionedThroughput
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
indexName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` NonEmpty KeySchemaElement
keySchema
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Projection
projection

instance Prelude.NFData GlobalSecondaryIndex where
  rnf :: GlobalSecondaryIndex -> ()
rnf GlobalSecondaryIndex' {Maybe ProvisionedThroughput
NonEmpty KeySchemaElement
Text
Projection
projection :: Projection
keySchema :: NonEmpty KeySchemaElement
indexName :: Text
provisionedThroughput :: Maybe ProvisionedThroughput
$sel:projection:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Projection
$sel:keySchema:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> NonEmpty KeySchemaElement
$sel:indexName:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Text
$sel:provisionedThroughput:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Maybe ProvisionedThroughput
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe ProvisionedThroughput
provisionedThroughput
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
indexName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf NonEmpty KeySchemaElement
keySchema
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Projection
projection

instance Data.ToJSON GlobalSecondaryIndex where
  toJSON :: GlobalSecondaryIndex -> Value
toJSON GlobalSecondaryIndex' {Maybe ProvisionedThroughput
NonEmpty KeySchemaElement
Text
Projection
projection :: Projection
keySchema :: NonEmpty KeySchemaElement
indexName :: Text
provisionedThroughput :: Maybe ProvisionedThroughput
$sel:projection:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Projection
$sel:keySchema:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> NonEmpty KeySchemaElement
$sel:indexName:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Text
$sel:provisionedThroughput:GlobalSecondaryIndex' :: GlobalSecondaryIndex -> Maybe ProvisionedThroughput
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"ProvisionedThroughput" 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 ProvisionedThroughput
provisionedThroughput,
            forall a. a -> Maybe a
Prelude.Just (Key
"IndexName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
indexName),
            forall a. a -> Maybe a
Prelude.Just (Key
"KeySchema" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= NonEmpty KeySchemaElement
keySchema),
            forall a. a -> Maybe a
Prelude.Just (Key
"Projection" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Projection
projection)
          ]
      )