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

import qualified Amazonka.Core as Core
import Amazonka.DynamoDB.Types.KeyType
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Represents /a single element/ of a key schema. A key schema specifies
-- the attributes that make up the primary key of a table, or the key
-- attributes of an index.
--
-- A @KeySchemaElement@ represents exactly one attribute of the primary
-- key. For example, a simple primary key would be represented by one
-- @KeySchemaElement@ (for the partition key). A composite primary key
-- would require one @KeySchemaElement@ for the partition key, and another
-- @KeySchemaElement@ for the sort key.
--
-- A @KeySchemaElement@ must be a scalar, top-level attribute (not a nested
-- attribute). The data type must be one of String, Number, or Binary. The
-- attribute cannot be nested within a List or a Map.
--
-- /See:/ 'newKeySchemaElement' smart constructor.
data KeySchemaElement = KeySchemaElement'
  { -- | The name of a key attribute.
    KeySchemaElement -> Text
attributeName :: Prelude.Text,
    -- | The role that this key attribute will assume:
    --
    -- -   @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.
    KeySchemaElement -> KeyType
keyType :: KeyType
  }
  deriving (KeySchemaElement -> KeySchemaElement -> Bool
(KeySchemaElement -> KeySchemaElement -> Bool)
-> (KeySchemaElement -> KeySchemaElement -> Bool)
-> Eq KeySchemaElement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: KeySchemaElement -> KeySchemaElement -> Bool
$c/= :: KeySchemaElement -> KeySchemaElement -> Bool
== :: KeySchemaElement -> KeySchemaElement -> Bool
$c== :: KeySchemaElement -> KeySchemaElement -> Bool
Prelude.Eq, ReadPrec [KeySchemaElement]
ReadPrec KeySchemaElement
Int -> ReadS KeySchemaElement
ReadS [KeySchemaElement]
(Int -> ReadS KeySchemaElement)
-> ReadS [KeySchemaElement]
-> ReadPrec KeySchemaElement
-> ReadPrec [KeySchemaElement]
-> Read KeySchemaElement
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [KeySchemaElement]
$creadListPrec :: ReadPrec [KeySchemaElement]
readPrec :: ReadPrec KeySchemaElement
$creadPrec :: ReadPrec KeySchemaElement
readList :: ReadS [KeySchemaElement]
$creadList :: ReadS [KeySchemaElement]
readsPrec :: Int -> ReadS KeySchemaElement
$creadsPrec :: Int -> ReadS KeySchemaElement
Prelude.Read, Int -> KeySchemaElement -> ShowS
[KeySchemaElement] -> ShowS
KeySchemaElement -> String
(Int -> KeySchemaElement -> ShowS)
-> (KeySchemaElement -> String)
-> ([KeySchemaElement] -> ShowS)
-> Show KeySchemaElement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [KeySchemaElement] -> ShowS
$cshowList :: [KeySchemaElement] -> ShowS
show :: KeySchemaElement -> String
$cshow :: KeySchemaElement -> String
showsPrec :: Int -> KeySchemaElement -> ShowS
$cshowsPrec :: Int -> KeySchemaElement -> ShowS
Prelude.Show, (forall x. KeySchemaElement -> Rep KeySchemaElement x)
-> (forall x. Rep KeySchemaElement x -> KeySchemaElement)
-> Generic KeySchemaElement
forall x. Rep KeySchemaElement x -> KeySchemaElement
forall x. KeySchemaElement -> Rep KeySchemaElement x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep KeySchemaElement x -> KeySchemaElement
$cfrom :: forall x. KeySchemaElement -> Rep KeySchemaElement x
Prelude.Generic)

-- |
-- Create a value of 'KeySchemaElement' 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:
--
-- 'attributeName', 'keySchemaElement_attributeName' - The name of a key attribute.
--
-- 'keyType', 'keySchemaElement_keyType' - The role that this key attribute will assume:
--
-- -   @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.
newKeySchemaElement ::
  -- | 'attributeName'
  Prelude.Text ->
  -- | 'keyType'
  KeyType ->
  KeySchemaElement
newKeySchemaElement :: Text -> KeyType -> KeySchemaElement
newKeySchemaElement Text
pAttributeName_ KeyType
pKeyType_ =
  KeySchemaElement' :: Text -> KeyType -> KeySchemaElement
KeySchemaElement'
    { $sel:attributeName:KeySchemaElement' :: Text
attributeName = Text
pAttributeName_,
      $sel:keyType:KeySchemaElement' :: KeyType
keyType = KeyType
pKeyType_
    }

-- | The name of a key attribute.
keySchemaElement_attributeName :: Lens.Lens' KeySchemaElement Prelude.Text
keySchemaElement_attributeName :: (Text -> f Text) -> KeySchemaElement -> f KeySchemaElement
keySchemaElement_attributeName = (KeySchemaElement -> Text)
-> (KeySchemaElement -> Text -> KeySchemaElement)
-> Lens KeySchemaElement KeySchemaElement Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\KeySchemaElement' {Text
attributeName :: Text
$sel:attributeName:KeySchemaElement' :: KeySchemaElement -> Text
attributeName} -> Text
attributeName) (\s :: KeySchemaElement
s@KeySchemaElement' {} Text
a -> KeySchemaElement
s {$sel:attributeName:KeySchemaElement' :: Text
attributeName = Text
a} :: KeySchemaElement)

-- | The role that this key attribute will assume:
--
-- -   @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.
keySchemaElement_keyType :: Lens.Lens' KeySchemaElement KeyType
keySchemaElement_keyType :: (KeyType -> f KeyType) -> KeySchemaElement -> f KeySchemaElement
keySchemaElement_keyType = (KeySchemaElement -> KeyType)
-> (KeySchemaElement -> KeyType -> KeySchemaElement)
-> Lens KeySchemaElement KeySchemaElement KeyType KeyType
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\KeySchemaElement' {KeyType
keyType :: KeyType
$sel:keyType:KeySchemaElement' :: KeySchemaElement -> KeyType
keyType} -> KeyType
keyType) (\s :: KeySchemaElement
s@KeySchemaElement' {} KeyType
a -> KeySchemaElement
s {$sel:keyType:KeySchemaElement' :: KeyType
keyType = KeyType
a} :: KeySchemaElement)

instance Core.FromJSON KeySchemaElement where
  parseJSON :: Value -> Parser KeySchemaElement
parseJSON =
    String
-> (Object -> Parser KeySchemaElement)
-> Value
-> Parser KeySchemaElement
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"KeySchemaElement"
      ( \Object
x ->
          Text -> KeyType -> KeySchemaElement
KeySchemaElement'
            (Text -> KeyType -> KeySchemaElement)
-> Parser Text -> Parser (KeyType -> KeySchemaElement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"AttributeName")
            Parser (KeyType -> KeySchemaElement)
-> Parser KeyType -> Parser KeySchemaElement
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser KeyType
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"KeyType")
      )

instance Prelude.Hashable KeySchemaElement

instance Prelude.NFData KeySchemaElement

instance Core.ToJSON KeySchemaElement where
  toJSON :: KeySchemaElement -> Value
toJSON KeySchemaElement' {Text
KeyType
keyType :: KeyType
attributeName :: Text
$sel:keyType:KeySchemaElement' :: KeySchemaElement -> KeyType
$sel:attributeName:KeySchemaElement' :: KeySchemaElement -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"AttributeName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
attributeName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"KeyType" Text -> KeyType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= KeyType
keyType)
          ]
      )