Problem: I'm using an AWS DynamoDb Enhanced Java client V2 in Kotlin (the details of the client might not be important), where I want to make a BatchWriteItemEnhancedRequest containing a bunch of requests. The standard usage according to the docs goes:
val batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder()
.addWriteBatch(
WriteBatch.builder(MyClass::class.java)
.mappedTableResource(myMappedTable)
.addPutItem(putRequest1)
.addPutItem(putRequest2)
.addPutItem(putRequest3)
.build()
)
.build()
Obviously this can't handle an anonymous list of putRequest items, only some known discrete values like putRequest1 and putRequest2.
The builder for WriteBatch doesn't seem to allow adding lists of requests: doc
The only way to do this, as I can see, is to make a for-loop that goes:
var writeBatchBuilder = WriteBatch.builder(MyClass::class.java)
.mappedTableResource(myMappedTable)
for (request in putItemRequests){
writeBatchBuilder = writeBatchBuilder.addPutItem(request)
}
val writeBatch = writeBatchBuilder.build()
Which seems horrible to me. There's got to be a more idiomatic way to do this right?