Skip to content

Commit c4e64a5

Browse files
committed
Support nullable types
1 parent 53e9db0 commit c4e64a5

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

hll/dynamodb-mapper/dynamodb-mapper-codegen/src/main/kotlin/aws/sdk/kotlin/hll/dynamodbmapper/codegen/model/MapperTypes.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public object MapperTypes {
7878
public object Values {
7979
public fun valueConverter(value: Type): TypeRef = TypeRef(MapperPkg.Hl.Values, "ValueConverter", genericArgs = listOf(value))
8080
public val ItemToValueConverter: TypeRef = TypeRef(MapperPkg.Hl.Values, "ItemToValueConverter")
81+
public val NullableConverter: TypeRef = TypeRef(MapperPkg.Hl.Values, "NullableConverter")
8182

8283
public object Collections {
8384
public val ListConverter: TypeRef = TypeRef(MapperPkg.Hl.CollectionValues, "ListConverter")

hll/dynamodb-mapper/dynamodb-mapper-schema-codegen/src/main/kotlin/aws/sdk/kotlin/hll/dynamodbmapper/codegen/annotations/rendering/SchemaRenderer.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ internal class SchemaRenderer(
195195

196196
type.isGenericFor(Types.Kotlin.Collections.Set) -> writeInline("#T", ksType.singleArgument().setValueConverter)
197197

198+
type.nullable -> {
199+
writeInline("#T(", MapperTypes.Values.NullableConverter)
200+
renderValueConverter(ksType.makeNotNullable())
201+
writeInline(")")
202+
}
203+
198204
else -> writeInline(
199205
"#T",
200206
when (type) {
@@ -218,7 +224,7 @@ internal class SchemaRenderer(
218224
Types.Kotlin.UShort -> MapperTypes.Values.Scalars.UShortConverter
219225
Types.Kotlin.ULong -> MapperTypes.Values.Scalars.ULongConverter
220226

221-
else -> error("Unsupported attribute type $this")
227+
else -> error("Unsupported attribute type $type")
222228
},
223229
)
224230
}

hll/dynamodb-mapper/dynamodb-mapper-schema-generator-plugin/src/test/kotlin/aws/sdk/kotlin/hll/dynamodbmapper/plugins/SchemaGeneratorPluginTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,33 @@ class SchemaGeneratorPluginTest {
448448
assertContains(setOf(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE), testResult.task(":test")?.outcome)
449449
}
450450

451+
@Test
452+
fun testNullableTypes() {
453+
buildFile.appendText(
454+
"""
455+
dependencies {
456+
implementation("aws.smithy.kotlin:runtime-core:$smithyKotlinVersion")
457+
testImplementation(kotlin("test"))
458+
}
459+
""".trimIndent(),
460+
)
461+
462+
createClassFile("standard-item-converters/src/NullableItem")
463+
464+
val buildResult = runner.build()
465+
assertContains(setOf(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE), buildResult.task(":build")?.outcome)
466+
val schemaFile = File(testProjectDir, "build/generated/ksp/main/kotlin/org/example/dynamodbmapper/generatedschemas/NullableItemSchema.kt")
467+
assertTrue(schemaFile.exists())
468+
469+
val testFile = File(testProjectDir, "src/test/kotlin/org/example/standard-item-converters/test/NullableItemTest.kt")
470+
testFile.ensureParentDirsCreated()
471+
testFile.createNewFile()
472+
testFile.writeText(getResource("/standard-item-converters/test/NullableItemTest.kt"))
473+
474+
val testResult = runner.withArguments("test").build()
475+
assertContains(setOf(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE), testResult.task(":test")?.outcome)
476+
}
477+
451478
@Test
452479
fun testLists() {
453480
buildFile.appendText(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.example
6+
7+
import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbItem
8+
import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbPartitionKey
9+
import aws.smithy.kotlin.runtime.time.Instant
10+
11+
@DynamoDbItem
12+
public data class NullableItem(
13+
@DynamoDbPartitionKey var id: Int,
14+
15+
/**
16+
* A selection of nullable types
17+
*/
18+
var string: String?,
19+
var byte: Byte?,
20+
var int: Int?,
21+
var instant: Instant?,
22+
) {
23+
override fun equals(other: Any?): Boolean {
24+
if (this === other) return true
25+
if (other !is NullableItem) return false
26+
27+
if (id != other.id) return false
28+
if (string != other.string) return false
29+
if (byte != other.byte) return false
30+
if (int != other.int) return false
31+
if (instant?.epochSeconds != other.instant?.epochSeconds) return false
32+
33+
return true
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.example
6+
7+
import aws.smithy.kotlin.runtime.ExperimentalApi
8+
import aws.smithy.kotlin.runtime.time.Instant
9+
import org.example.dynamodbmapper.generatedschemas.NullableItemConverter
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
@OptIn(ExperimentalApi::class)
14+
public class NullableItemTest {
15+
@Test
16+
fun converterTest() {
17+
val nullable = NullableItem(
18+
id = 1,
19+
string = null,
20+
byte = null,
21+
int = 5,
22+
instant = Instant.now(),
23+
)
24+
25+
val item = NullableItemConverter.convertTo(nullable)
26+
val converted = NullableItemConverter.convertFrom(item)
27+
assertEquals(nullable, converted)
28+
}
29+
}

0 commit comments

Comments
 (0)