{-# LANGUAGE DeriveDataTypeable        #-}
{-# LANGUAGE FlexibleContexts          #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE MultiParamTypeClasses     #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE RecordWildCards           #-}
{-# LANGUAGE ScopedTypeVariables       #-}
{-# LANGUAGE TypeFamilies              #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Aws.DynamoDb.Commands.BatchWriteItem
-- Copyright   :  Soostone Inc
-- License     :  BSD3
--
-- Maintainer  :  Justin Dawson <jtdawso@gmail.com>
-- Stability   :  experimental
--
-- @http:\/\/docs.aws.amazon.com\/amazondynamodb\/latest\/APIReference\/API_BatchWriteItem.html@
----------------------------------------------------------------------------

module Aws.DynamoDb.Commands.BatchWriteItem where

-------------------------------------------------------------------------------
import           Control.Applicative
import           Data.Aeson
import           Data.Default
import           Data.Foldable (asum)
import qualified Data.HashMap.Strict as HM
import qualified Data.Text           as T
import           Prelude
-------------------------------------------------------------------------------
import           Aws.Core
import           Aws.DynamoDb.Core
import           Aws.DynamoDb.Commands.PutItem
import           Aws.DynamoDb.Commands.DeleteItem
-------------------------------------------------------------------------------


data Request = PutRequest { Request -> Item
prItem :: Item }
             | DeleteRequest {Request -> PrimaryKey
drKey :: PrimaryKey}
     deriving (Request -> Request -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Request -> Request -> Bool
$c/= :: Request -> Request -> Bool
== :: Request -> Request -> Bool
$c== :: Request -> Request -> Bool
Eq,Int -> Request -> ShowS
[Request] -> ShowS
Request -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Request] -> ShowS
$cshowList :: [Request] -> ShowS
show :: Request -> String
$cshow :: Request -> String
showsPrec :: Int -> Request -> ShowS
$cshowsPrec :: Int -> Request -> ShowS
Show,ReadPrec [Request]
ReadPrec Request
Int -> ReadS Request
ReadS [Request]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Request]
$creadListPrec :: ReadPrec [Request]
readPrec :: ReadPrec Request
$creadPrec :: ReadPrec Request
readList :: ReadS [Request]
$creadList :: ReadS [Request]
readsPrec :: Int -> ReadS Request
$creadsPrec :: Int -> ReadS Request
Read,Eq Request
Request -> Request -> Bool
Request -> Request -> Ordering
Request -> Request -> Request
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Request -> Request -> Request
$cmin :: Request -> Request -> Request
max :: Request -> Request -> Request
$cmax :: Request -> Request -> Request
>= :: Request -> Request -> Bool
$c>= :: Request -> Request -> Bool
> :: Request -> Request -> Bool
$c> :: Request -> Request -> Bool
<= :: Request -> Request -> Bool
$c<= :: Request -> Request -> Bool
< :: Request -> Request -> Bool
$c< :: Request -> Request -> Bool
compare :: Request -> Request -> Ordering
$ccompare :: Request -> Request -> Ordering
Ord)

data BatchWriteItem = BatchWriteItem {
      BatchWriteItem -> [(Text, [Request])]
bwRequests :: [(T.Text,[Request])]
    -- ^ Put or Delete Requests for a specified table
    , BatchWriteItem -> ReturnConsumption
bwRetCons :: ReturnConsumption
    , BatchWriteItem -> ReturnItemCollectionMetrics
bwRetMet  :: ReturnItemCollectionMetrics
    } deriving (BatchWriteItem -> BatchWriteItem -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchWriteItem -> BatchWriteItem -> Bool
$c/= :: BatchWriteItem -> BatchWriteItem -> Bool
== :: BatchWriteItem -> BatchWriteItem -> Bool
$c== :: BatchWriteItem -> BatchWriteItem -> Bool
Eq,Int -> BatchWriteItem -> ShowS
[BatchWriteItem] -> ShowS
BatchWriteItem -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchWriteItem] -> ShowS
$cshowList :: [BatchWriteItem] -> ShowS
show :: BatchWriteItem -> String
$cshow :: BatchWriteItem -> String
showsPrec :: Int -> BatchWriteItem -> ShowS
$cshowsPrec :: Int -> BatchWriteItem -> ShowS
Show,ReadPrec [BatchWriteItem]
ReadPrec BatchWriteItem
Int -> ReadS BatchWriteItem
ReadS [BatchWriteItem]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchWriteItem]
$creadListPrec :: ReadPrec [BatchWriteItem]
readPrec :: ReadPrec BatchWriteItem
$creadPrec :: ReadPrec BatchWriteItem
readList :: ReadS [BatchWriteItem]
$creadList :: ReadS [BatchWriteItem]
readsPrec :: Int -> ReadS BatchWriteItem
$creadsPrec :: Int -> ReadS BatchWriteItem
Read,Eq BatchWriteItem
BatchWriteItem -> BatchWriteItem -> Bool
BatchWriteItem -> BatchWriteItem -> Ordering
BatchWriteItem -> BatchWriteItem -> BatchWriteItem
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BatchWriteItem -> BatchWriteItem -> BatchWriteItem
$cmin :: BatchWriteItem -> BatchWriteItem -> BatchWriteItem
max :: BatchWriteItem -> BatchWriteItem -> BatchWriteItem
$cmax :: BatchWriteItem -> BatchWriteItem -> BatchWriteItem
>= :: BatchWriteItem -> BatchWriteItem -> Bool
$c>= :: BatchWriteItem -> BatchWriteItem -> Bool
> :: BatchWriteItem -> BatchWriteItem -> Bool
$c> :: BatchWriteItem -> BatchWriteItem -> Bool
<= :: BatchWriteItem -> BatchWriteItem -> Bool
$c<= :: BatchWriteItem -> BatchWriteItem -> Bool
< :: BatchWriteItem -> BatchWriteItem -> Bool
$c< :: BatchWriteItem -> BatchWriteItem -> Bool
compare :: BatchWriteItem -> BatchWriteItem -> Ordering
$ccompare :: BatchWriteItem -> BatchWriteItem -> Ordering
Ord)


-------------------------------------------------------------------------------

toBatchWrite :: [PutItem]
           -> [DeleteItem]
           -> BatchWriteItem
toBatchWrite :: [PutItem] -> [DeleteItem] -> BatchWriteItem
toBatchWrite [PutItem]
ps [DeleteItem]
ds =[(Text, [Request])]
-> ReturnConsumption
-> ReturnItemCollectionMetrics
-> BatchWriteItem
BatchWriteItem [(Text, [Request])]
maps forall a. Default a => a
def forall a. Default a => a
def  
      where
        maps :: [(T.Text,[Request])]
        maps :: [(Text, [Request])]
maps = let pMap :: HashMap Text [Request]
pMap = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\HashMap Text [Request]
acc PutItem
p -> let key :: Text
key = PutItem -> Text
piTable PutItem
p
                                             in forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert Text
key (Item -> Request
PutRequest (PutItem -> Item
piItem PutItem
p) forall a. a -> [a] -> [a]
: (forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HM.lookupDefault [] Text
key HashMap Text [Request]
acc)) HashMap Text [Request]
acc) forall k v. HashMap k v
HM.empty [PutItem]
ps 
                   totalMap :: HashMap Text [Request]
totalMap = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\HashMap Text [Request]
acc DeleteItem
d -> let key :: Text
key = DeleteItem -> Text
diTable DeleteItem
d
                                                 in  forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert Text
key (PrimaryKey -> Request
DeleteRequest (DeleteItem -> PrimaryKey
diKey DeleteItem
d) forall a. a -> [a] -> [a]
: (forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HM.lookupDefault [] Text
key HashMap Text [Request]
acc)) HashMap Text [Request]
acc) HashMap Text [Request]
pMap [DeleteItem]
ds
                 in  forall k v. HashMap k v -> [(k, v)]
HM.toList HashMap Text [Request]
totalMap
-- | Construct a BatchWriteItem
batchWriteItem :: [(T.Text,[Request])]
               -> BatchWriteItem
batchWriteItem :: [(Text, [Request])] -> BatchWriteItem
batchWriteItem [(Text, [Request])]
reqs = [(Text, [Request])]
-> ReturnConsumption
-> ReturnItemCollectionMetrics
-> BatchWriteItem
BatchWriteItem [(Text, [Request])]
reqs forall a. Default a => a
def forall a. Default a => a
def


instance ToJSON Request where
   toJSON :: Request -> Value
toJSON PutRequest{Item
prItem :: Item
prItem :: Request -> Item
..} =
       [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
         [ Key
"PutRequest" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ([Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ [Key
"Item" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Item
prItem])
         ]
   toJSON DeleteRequest{PrimaryKey
drKey :: PrimaryKey
drKey :: Request -> PrimaryKey
..} =
       [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
         [ Key
"DeleteRequest" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=  ([Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ [Key
"Key" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PrimaryKey
drKey])
         ]

instance ToJSON BatchWriteItem where
    toJSON :: BatchWriteItem -> Value
toJSON BatchWriteItem{[(Text, [Request])]
ReturnItemCollectionMetrics
ReturnConsumption
bwRetMet :: ReturnItemCollectionMetrics
bwRetCons :: ReturnConsumption
bwRequests :: [(Text, [Request])]
bwRetMet :: BatchWriteItem -> ReturnItemCollectionMetrics
bwRetCons :: BatchWriteItem -> ReturnConsumption
bwRequests :: BatchWriteItem -> [(Text, [Request])]
..} =
        [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
          [ Key
"RequestItems" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList [(Text, [Request])]
bwRequests
          , Key
"ReturnConsumedCapacity" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ReturnConsumption
bwRetCons
          , Key
"ReturnItemCollectionMetrics" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ReturnItemCollectionMetrics
bwRetMet
          ]

instance FromJSON Request where
    parseJSON :: Value -> Parser Request
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"PutRequest or DeleteRequest" forall a b. (a -> b) -> a -> b
$ \Object
o ->
     
     forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [
           do
             Object
pr <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"PutRequest"
             Item
i  <- Object
pr forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"Item"
             forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Item -> Request
PutRequest Item
i ,
           do
             Object
dr <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"DeleteRequest"
             PrimaryKey
pk <- Object
dr forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"Key"
             forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ PrimaryKey -> Request
DeleteRequest PrimaryKey
pk
          ]
    
data BatchWriteItemResponse = BatchWriteItemResponse {
      BatchWriteItemResponse -> [(Text, [Request])]
bwUnprocessed    :: [(T.Text,[Request])]
    -- ^ Unprocessed Requests on failure
    , BatchWriteItemResponse -> Maybe ConsumedCapacity
bwConsumed :: Maybe ConsumedCapacity
    -- ^ Amount of capacity consumed
    , BatchWriteItemResponse -> Maybe ItemCollectionMetrics
bwColMet   :: Maybe ItemCollectionMetrics
    -- ^ Collection metrics for tables affected by BatchWriteItem.
    } deriving (BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c/= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
== :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c== :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
Eq,Int -> BatchWriteItemResponse -> ShowS
[BatchWriteItemResponse] -> ShowS
BatchWriteItemResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchWriteItemResponse] -> ShowS
$cshowList :: [BatchWriteItemResponse] -> ShowS
show :: BatchWriteItemResponse -> String
$cshow :: BatchWriteItemResponse -> String
showsPrec :: Int -> BatchWriteItemResponse -> ShowS
$cshowsPrec :: Int -> BatchWriteItemResponse -> ShowS
Show,ReadPrec [BatchWriteItemResponse]
ReadPrec BatchWriteItemResponse
Int -> ReadS BatchWriteItemResponse
ReadS [BatchWriteItemResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchWriteItemResponse]
$creadListPrec :: ReadPrec [BatchWriteItemResponse]
readPrec :: ReadPrec BatchWriteItemResponse
$creadPrec :: ReadPrec BatchWriteItemResponse
readList :: ReadS [BatchWriteItemResponse]
$creadList :: ReadS [BatchWriteItemResponse]
readsPrec :: Int -> ReadS BatchWriteItemResponse
$creadsPrec :: Int -> ReadS BatchWriteItemResponse
Read,Eq BatchWriteItemResponse
BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
BatchWriteItemResponse -> BatchWriteItemResponse -> Ordering
BatchWriteItemResponse
-> BatchWriteItemResponse -> BatchWriteItemResponse
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BatchWriteItemResponse
-> BatchWriteItemResponse -> BatchWriteItemResponse
$cmin :: BatchWriteItemResponse
-> BatchWriteItemResponse -> BatchWriteItemResponse
max :: BatchWriteItemResponse
-> BatchWriteItemResponse -> BatchWriteItemResponse
$cmax :: BatchWriteItemResponse
-> BatchWriteItemResponse -> BatchWriteItemResponse
>= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c>= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
> :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c> :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
<= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c<= :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
< :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
$c< :: BatchWriteItemResponse -> BatchWriteItemResponse -> Bool
compare :: BatchWriteItemResponse -> BatchWriteItemResponse -> Ordering
$ccompare :: BatchWriteItemResponse -> BatchWriteItemResponse -> Ordering
Ord)



instance Transaction BatchWriteItem BatchWriteItemResponse


instance SignQuery BatchWriteItem where
    type ServiceConfiguration BatchWriteItem = DdbConfiguration
    signQuery :: forall queryType.
BatchWriteItem
-> ServiceConfiguration BatchWriteItem queryType
-> SignatureData
-> SignedQuery
signQuery BatchWriteItem
gi = forall a qt.
ToJSON a =>
ByteString
-> a -> DdbConfiguration qt -> SignatureData -> SignedQuery
ddbSignQuery ByteString
"BatchWriteItem" BatchWriteItem
gi


instance FromJSON BatchWriteItemResponse where
    parseJSON :: Value -> Parser BatchWriteItemResponse
parseJSON (Object Object
v) = [(Text, [Request])]
-> Maybe ConsumedCapacity
-> Maybe ItemCollectionMetrics
-> BatchWriteItemResponse
BatchWriteItemResponse
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k v. HashMap k v -> [(k, v)]
HM.toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"UnprocessedItems")
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ConsumedCapacity"
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ItemCollectionMetrics"
    parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"BatchWriteItemResponse must be an object."


instance ResponseConsumer r BatchWriteItemResponse where
    type ResponseMetadata BatchWriteItemResponse = DdbResponse
    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata BatchWriteItemResponse)
-> HTTPResponseConsumer BatchWriteItemResponse
responseConsumer Request
_ r
_ IORef (ResponseMetadata BatchWriteItemResponse)
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp = forall a. FromJSON a => IORef DdbResponse -> HTTPResponseConsumer a
ddbResponseConsumer IORef (ResponseMetadata BatchWriteItemResponse)
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp


instance AsMemoryResponse BatchWriteItemResponse where
    type MemoryResponse BatchWriteItemResponse = BatchWriteItemResponse
    loadToMemory :: BatchWriteItemResponse
-> ResourceT IO (MemoryResponse BatchWriteItemResponse)
loadToMemory = forall (m :: * -> *) a. Monad m => a -> m a
return