@@ -9,8 +9,13 @@ import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema
99import aws.sdk.kotlin.hll.dynamodbmapper.model.Index
1010import aws.sdk.kotlin.hll.dynamodbmapper.model.Table
1111import aws.sdk.kotlin.hll.dynamodbmapper.model.TableSpec
12- import aws.sdk.kotlin.hll.dynamodbmapper.operations.TableOperations
13- import aws.sdk.kotlin.hll.dynamodbmapper.operations.TableOperationsImpl
12+ import aws.sdk.kotlin.hll.dynamodbmapper.model.itemOf
13+ import aws.sdk.kotlin.hll.dynamodbmapper.operations.*
14+ import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.Interceptor
15+ import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.LReqContext
16+ import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
17+ import aws.sdk.kotlin.services.dynamodb.model.GetItemRequest as LowLevelGetItemRequest
18+ import aws.sdk.kotlin.services.dynamodb.model.GetItemResponse as LowLevelGetItemResponse
1419
1520internal fun <T , PK > tableImpl (
1621 mapper : DynamoDbMapper ,
@@ -35,7 +40,17 @@ internal fun <T, PK> tableImpl(
3540 schema : ItemSchema .CompositeKey <T , PK , SK >,
3641 ): Index .CompositeKey <T , PK , SK > = indexImpl(mapper, tableName, name, schema)
3742
38- override suspend fun getItem (partitionKey : PK ) = TODO (" not yet implemented" )
43+ override suspend fun getItem (partitionKey : PK ): T ? {
44+ val keyItem = itemOf(schema.partitionKey.toField(partitionKey))
45+ val interceptor = KeyInsertionInterceptor <T >(keyItem)
46+ val op = getItemOperation(specImpl).let {
47+ it.copy(
48+ interceptors = it.interceptors.prepend(interceptor),
49+ )
50+ }
51+ val hRes = op.execute(GetItemRequest { })
52+ return hRes.item
53+ }
3954 }
4055}
4156
@@ -61,6 +76,36 @@ internal fun <T, PK, SK> tableImpl(
6176 schema : ItemSchema .CompositeKey <T , PK , SK >,
6277 ): Index .CompositeKey <T , PK , SK > = indexImpl(mapper, tableName, name, schema)
6378
64- override suspend fun getItem (partitionKey : PK , sortKey : SK ) = TODO (" Not yet implemented" )
79+ override suspend fun getItem (partitionKey : PK , sortKey : SK ): T ? {
80+ val keyItem = itemOf(
81+ schema.partitionKey.toField(partitionKey),
82+ schema.sortKey.toField(sortKey),
83+ )
84+ val interceptor = KeyInsertionInterceptor <T >(keyItem)
85+ val op = getItemOperation(specImpl).let {
86+ it.copy(
87+ interceptors = it.interceptors.prepend(interceptor),
88+ )
89+ }
90+ val hRes = op.execute(GetItemRequest { })
91+ return hRes.item
92+ }
6593 }
6694}
95+
96+ private typealias GetItemInterceptor <T > =
97+ Interceptor <T , GetItemRequest <T >, LowLevelGetItemRequest , LowLevelGetItemResponse , GetItemResponse <T >>
98+
99+ private class KeyInsertionInterceptor <T >(private val newKey : Map <String , AttributeValue >) : GetItemInterceptor<T> {
100+ override fun modifyBeforeInvocation (ctx : LReqContext <T , GetItemRequest <T >, LowLevelGetItemRequest >) =
101+ ctx.lowLevelRequest.copy {
102+ if (key == null ) {
103+ key = newKey
104+ }
105+ }
106+ }
107+
108+ private fun <T > List<T>.prepend (element : T ): List <T > = buildList(size + 1 ) {
109+ add(element)
110+ addAll(this )
111+ }
0 commit comments