Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add attribute converters for "standard" values #1381

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ slf4j-version = "2.0.9"
[libraries]
aws-kotlin-repo-tools-build-support = { module="aws.sdk.kotlin.gradle:build-support", version.ref = "aws-kotlin-repo-tools-version" }

kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin-version"}
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin-version"}
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin-version" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin-version" }
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin-version" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin-version" }
kotlin-test-junit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5", version.ref = "kotlin-version" }
dokka-core = { module = "org.jetbrains.dokka:dokka-core", version.ref = "dokka-version" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class MapperProcessor(private val env: SymbolProcessorEnvironment) : Symb
|import aws.sdk.kotlin.hll.dynamodbmapper.items.*
|import aws.sdk.kotlin.hll.dynamodbmapper.model.*
|import aws.sdk.kotlin.hll.dynamodbmapper.values.*
|import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.*
|import $basePackageName.$className
|
|public class $builderName {
Expand Down Expand Up @@ -115,9 +116,9 @@ public class MapperProcessor(private val env: SymbolProcessorEnvironment) : Symb
props.forEach { prop ->
val converterType = when (val fqTypeName = prop.typeName.asString()) {
"aws.smithy.kotlin.runtime.time.Instant" -> "InstantConverter.Default"
"kotlin.Boolean" -> "BooleanConverter"
"kotlin.Int" -> "IntConverter"
"kotlin.String" -> "StringConverter"
"kotlin.Boolean" -> "BooleanConverter.Default"
"kotlin.Int" -> "IntConverter.Default"
"kotlin.String" -> "StringConverter.Default"
else -> error("Unsupported attribute type $fqTypeName")
}

Expand Down
453 changes: 412 additions & 41 deletions hll/dynamodb-mapper/dynamodb-mapper/api/dynamodb-mapper.api

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions hll/dynamodb-mapper/dynamodb-mapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kotlin {

commonTest {
dependencies {
implementation(libs.kotlin.reflect)
implementation(libs.kotlinx.coroutines.test)
implementation(libs.kotest.assertions.core)
implementation(libs.kotest.runner.junit5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import aws.smithy.kotlin.runtime.InternalApi
import aws.smithy.kotlin.runtime.content.Document
import aws.smithy.kotlin.runtime.util.toNumber

// FIXME Combine with DocumentValueConverter or refactor to commonize as much code as possible
public object DocumentConverter : ItemConverter<Document> {
override fun fromItem(item: Item): Document = item
.mapValues { (_, attr) -> fromAv(attr) }
.mapValues { (_, attr) -> fromAttributeValue(attr) }
.let(Document::Map)

override fun toItem(obj: Document, onlyAttributes: Set<String>?): Item {
Expand All @@ -25,26 +26,26 @@ public object DocumentConverter : ItemConverter<Document> {
obj.filterKeys { it in onlyAttributes }
}

return map.mapValues { (_, value) -> toAv(value) }.toItem()
return map.mapValues { (_, value) -> toAttributeValue(value) }.toItem()
}
}

@OptIn(InternalApi::class)
private fun fromAv(attr: AttributeValue): Document? = when (attr) {
private fun fromAttributeValue(attr: AttributeValue): Document? = when (attr) {
is AttributeValue.Null -> null
is AttributeValue.N -> Document.Number(attr.value.toNumber()!!) // FIXME need better toNumber logic
is AttributeValue.S -> Document.String(attr.value)
is AttributeValue.Bool -> Document.Boolean(attr.value)
is AttributeValue.L -> Document.List(attr.value.map(::fromAv))
is AttributeValue.M -> Document.Map(attr.value.mapValues { (_, nestedValue) -> fromAv(nestedValue) })
is AttributeValue.L -> Document.List(attr.value.map(::fromAttributeValue))
is AttributeValue.M -> Document.Map(attr.value.mapValues { (_, nestedValue) -> fromAttributeValue(nestedValue) })
else -> error("Documents do not support ${attr::class.qualifiedName}")
}

private fun toAv(value: Document?): AttributeValue = when (value) {
private fun toAttributeValue(value: Document?): AttributeValue = when (value) {
null -> AttributeValue.Null(true)
is Document.Number -> AttributeValue.N(value.value.toString())
is Document.String -> AttributeValue.S(value.value)
is Document.Boolean -> AttributeValue.Bool(value.value)
is Document.List -> AttributeValue.L(value.value.map(::toAv))
is Document.Map -> AttributeValue.M(value.mapValues { (_, nestedValue) -> toAv(nestedValue) })
is Document.List -> AttributeValue.L(value.value.map(::toAttributeValue))
is Document.Map -> AttributeValue.M(value.mapValues { (_, nestedValue) -> toAttributeValue(nestedValue) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package aws.sdk.kotlin.hll.dynamodbmapper.items
import aws.sdk.kotlin.hll.dynamodbmapper.model.Item

/**
* Defines the logic for converting between objects and items
* Defines the logic for converting between objects and DynamoDB items
* @param T The type of objects which will be converted
*/
public interface ItemConverter<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public class SimpleItemConverter<T, B>(
*
* ```kotlin
* val descriptor = descriptors[name] // AttributeDescriptor<*, T, B>
* val value = descriptor.converter.fromAv(av) // Any?
* val value = descriptor.converter.fromAttributeValue(av) // Any?
* descriptor.setter(builder, value) // Type mismatch for value. Required: Nothing, Found: Any?
* ```
*/
fun <A> AttributeDescriptor<A, T, B>.fromAv(av: AttributeValue) =
builder.setter(converter.fromAv(av))
fun <A> AttributeDescriptor<A, T, B>.fromAttributeValue(attr: AttributeValue) =
builder.setter(converter.fromAttributeValue(attr))

item.forEach { (name, av) ->
item.forEach { (name, attr) ->
// TODO make behavior for unknown attributes configurable (ignore, exception, other?)
descriptors[name]?.fromAv(av)
descriptors[name]?.fromAttributeValue(attr)
}

return builder.build()
Expand All @@ -66,11 +66,11 @@ public class SimpleItemConverter<T, B>(
* ```kotlin
* val descriptor = descriptors[name] // AttributeDescriptor<*, T, B>
* val value = descriptor.getter(obj) // Any?
* descriptor.converter.toAv(value) // Type mismatch for value. Required: Nothing, Found: Any?
* descriptor.converter.toAttributeValue(value) // Type mismatch for value. Required: Nothing, Found: Any?
* ```
*/
fun <A> AttributeDescriptor<A, T, B>.toAv() =
converter.toAv(getter(obj))
fun <A> AttributeDescriptor<A, T, B>.toAttributeValue() =
converter.toAttributeValue(getter(obj))

val descriptors = if (onlyAttributes == null) {
this.descriptors.values
Expand All @@ -79,7 +79,7 @@ public class SimpleItemConverter<T, B>(
}

return buildItem {
descriptors.forEach { desc -> put(desc.name, desc.toAv()) }
descriptors.forEach { desc -> put(desc.name, desc.toAttributeValue()) }
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.values

import aws.sdk.kotlin.services.dynamodb.model.AttributeValue

/**
* Converts between potentially `null` values and
* [DynamoDB `NULL` values](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Null).
* If the value is non-null, the given [delegate] converter will be used.
* @param A The non-nullable type
* @param delegate A [ValueConverter] which will be used for non-null values
*/
public class NullableConverter<A : Any>(private val delegate: ValueConverter<A>) : ValueConverter<A?> {
override fun fromAttributeValue(attr: AttributeValue): A? = when (attr) {
is AttributeValue.Null -> null
else -> delegate.fromAttributeValue(attr)
}

override fun toAttributeValue(value: A?): AttributeValue = when (value) {
null -> AttributeValue.Null(true)
else -> delegate.toAttributeValue(value)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ package aws.sdk.kotlin.hll.dynamodbmapper.values

import aws.sdk.kotlin.services.dynamodb.model.AttributeValue

// TODO document, add unit tests
public interface ValueConverter<A> {
public fun fromAv(attr: AttributeValue): A
public fun toAv(value: A): AttributeValue
/**
* Defines the logic for converting individual values between a high-level type [V] (e.g., [String], [Boolean], [Map])
* and
* [DynamoDB data types](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes)
* @param V The type of high-level values which will be converted to low-level DynamoDB attribute values
*/
public interface ValueConverter<V> {
/**
* Convert the given [attr] from an [AttributeValue] to a value of type [V]
* @param attr The DynamoDB attribute value to convert
* @return The value converted from [attr]
*/
public fun fromAttributeValue(attr: AttributeValue): V

/**
* Convert the given [value] of type [V] to an [AttributeValue]
* @param value The value to convert
* @return The [AttributeValue] converted from [value]
*/
public fun toAttributeValue(value: V): AttributeValue
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.values.collections

import aws.sdk.kotlin.hll.dynamodbmapper.values.ValueConverter
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue

/**
* Converts between a [Set] of [ByteArray] elements and
* [DynamoDB `BS` values](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.SetTypes)
*/
public class ByteArraySetConverter : ValueConverter<Set<ByteArray>> {
public companion object {
/**
* The default instance of [ByteArraySetConverter]
*/
public val Default: ByteArraySetConverter = ByteArraySetConverter()
}

override fun fromAttributeValue(attr: AttributeValue): Set<ByteArray> = attr.asBs().toSet()
override fun toAttributeValue(value: Set<ByteArray>): AttributeValue = AttributeValue.Bs(value.toList())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.values.collections

import aws.sdk.kotlin.hll.dynamodbmapper.values.ValueConverter
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue

/**
* Converts between [List] and
* [DynamoDB `L` values](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Document.List)
* @param elementConverter A [ValueConverter] for the elements of lists
*/
public class ListConverter<T>(private val elementConverter: ValueConverter<T>) : ValueConverter<List<T>> {
override fun fromAttributeValue(attr: AttributeValue): List<T> =
attr.asL().map(elementConverter::fromAttributeValue)

override fun toAttributeValue(value: List<T>): AttributeValue =
AttributeValue.L(value.map(elementConverter::toAttributeValue))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.values.collections

import aws.sdk.kotlin.hll.dynamodbmapper.values.ValueConverter
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue

/**
* Converts between [Map] and
* [DynamoDB `M` values](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Document.Map)
* Note that this converter requires maps' keys to be strings to in order to match DynamoDB's `M` type. Maps with
* non-string keys must use a custom converter.
* @param elementConverter A [ValueConverter] used to convert the values inside maps
*/
public class MapConverter<T>(private val elementConverter: ValueConverter<T>) : ValueConverter<Map<String, T>> {
override fun fromAttributeValue(attr: AttributeValue): Map<String, T> =
attr.asM().mapValues { (_, v) -> elementConverter.fromAttributeValue(v) }

override fun toAttributeValue(value: Map<String, T>): AttributeValue =
AttributeValue.M(value.mapValues { (_, v) -> elementConverter.toAttributeValue(v) })
}
Loading
Loading