-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial poc commit of DynamoDB Mapper (#1232)
- Loading branch information
Showing
30 changed files
with
1,301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import aws.sdk.kotlin.gradle.dsl.configurePublishing | ||
import aws.sdk.kotlin.gradle.kmp.* | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
||
description = "High-level libraries for the AWS SDK for Kotlin" | ||
|
||
// FIXME 🔽🔽🔽 This is all copied from :aws-runtime and should be commonized 🔽🔽🔽 | ||
|
||
plugins { | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.kotlinx.binary.compatibility.validator) | ||
alias(libs.plugins.aws.kotlin.repo.tools.kmp) apply false | ||
jacoco | ||
} | ||
|
||
val sdkVersion: String by project | ||
|
||
// capture locally - scope issue with custom KMP plugin | ||
val libraries = libs | ||
|
||
subprojects { | ||
if (!needsKmpConfigured) return@subprojects | ||
|
||
group = "aws.sdk.kotlin" | ||
version = sdkVersion | ||
|
||
apply { | ||
plugin("org.jetbrains.kotlin.multiplatform") | ||
plugin("org.jetbrains.dokka") | ||
plugin(libraries.plugins.aws.kotlin.repo.tools.kmp.get().pluginId) | ||
} | ||
|
||
configurePublishing("aws-sdk-kotlin") | ||
|
||
kotlin { | ||
explicitApi() | ||
|
||
sourceSets { | ||
// dependencies available for all subprojects | ||
|
||
named("commonTest") { | ||
dependencies { | ||
implementation(libraries.kotest.assertions.core) | ||
} | ||
} | ||
|
||
named("jvmTest") { | ||
dependencies { | ||
implementation(libraries.kotest.assertions.core.jvm) | ||
implementation(libraries.slf4j.simple) | ||
} | ||
} | ||
} | ||
} | ||
|
||
kotlin.sourceSets.all { | ||
// Allow subprojects to use internal APIs | ||
// See https://kotlinlang.org/docs/reference/opt-in-requirements.html#opting-in-to-using-api | ||
listOf("kotlin.RequiresOptIn").forEach { languageSettings.optIn(it) } | ||
} | ||
|
||
dependencies { | ||
dokkaPlugin(project(":dokka-aws")) | ||
} | ||
|
||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { | ||
compilerOptions { | ||
jvmTarget.set(JvmTarget.JVM_1_8) | ||
freeCompilerArgs.add("-Xexpect-actual-classes") | ||
} | ||
} | ||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile> { | ||
compilerOptions { | ||
freeCompilerArgs.add("-Xexpect-actual-classes") | ||
} | ||
} | ||
} | ||
|
||
apiValidation { | ||
val availableSubprojects = subprojects.map { it.name }.toSet() | ||
|
||
ignoredProjects += listOf( | ||
"dynamodb-mapper-annotation-processor", | ||
"ddb-mapper-annotation-processor-test", | ||
).filter { it in availableSubprojects } // Some projects may not be in the build depending on bootstrapping | ||
} |
15 changes: 15 additions & 0 deletions
15
hll/ddb-mapper/dynamodb-mapper-annotation-processor/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
kotlin { | ||
sourceSets { | ||
jvmMain { | ||
dependencies { | ||
implementation(project(":hll:ddb-mapper:dynamodb-mapper-annotations")) | ||
implementation(libs.ksp.api) | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...vm/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aws.sdk.kotlin.hll.dynamodbmapper.processor.MapperProcessorProvider |
193 changes: 193 additions & 0 deletions
193
...notation-processor/jvm/src/aws/sdk/kotlin/hll/dynamodbmapper/processor/MapperProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.hll.dynamodbmapper.processor | ||
|
||
import aws.sdk.kotlin.hll.dynamodbmapper.DynamodDbAttribute | ||
import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbItem | ||
import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbPartitionKey | ||
import com.google.devtools.ksp.KspExperimental | ||
import com.google.devtools.ksp.getAnnotationsByType | ||
import com.google.devtools.ksp.isAnnotationPresent | ||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
import com.google.devtools.ksp.validate | ||
|
||
private val annotationName = DynamoDbItem::class.qualifiedName!! | ||
|
||
public class MapperProcessor(private val env: SymbolProcessorEnvironment) : SymbolProcessor { | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
env.logger.info("Searching for symbols annotated with $annotationName") | ||
val annotated = resolver.getSymbolsWithAnnotation(annotationName) | ||
val invalid = annotated.filterNot { it.validate() }.toList() | ||
env.logger.info("Found invalid classes $invalid") | ||
|
||
annotated | ||
.toList() | ||
.also { env.logger.info("Found annotated classes: $it") } | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { it.validate() } | ||
.forEach { it.accept(ItemVisitor(), Unit) } | ||
|
||
return invalid | ||
} | ||
|
||
private inner class ItemVisitor : KSVisitorVoid() { | ||
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | ||
val basePackageName = classDeclaration.packageName.asString() | ||
val packageName = "$basePackageName.mapper.schemas" | ||
|
||
val className = classDeclaration.qualifiedName!!.getShortName() | ||
val builderName = "${className}Builder" | ||
val converterName = "${className}Converter" | ||
val schemaName = "${className}Schema" | ||
|
||
val props = classDeclaration.getAllProperties().mapNotNull(Property.Companion::from) | ||
val keyProp = checkNotNull(props.singleOrNull { it.isPk }) { | ||
"Expected exactly one @DynamoDbPartitionKey annotation on a property" | ||
} | ||
|
||
env.codeGenerator.createNewFile( | ||
Dependencies(true, classDeclaration.containingFile!!), | ||
packageName, | ||
schemaName, | ||
).use { file -> | ||
file.bufferedWriter().use { writer -> | ||
writer.append( | ||
""" | ||
|package $packageName | ||
| | ||
|import aws.sdk.kotlin.hll.dynamodbmapper.* | ||
|import aws.sdk.kotlin.hll.dynamodbmapper.items.* | ||
|import aws.sdk.kotlin.hll.dynamodbmapper.schemas.* | ||
|import aws.sdk.kotlin.hll.dynamodbmapper.values.* | ||
|import $basePackageName.$className | ||
| | ||
|public class $builderName { | ||
${generateProperties(props)} | ||
| ${generateBuildMethod(className, props)} | ||
|} | ||
| | ||
|public object $converterName : ItemConverter<$className> by SimpleItemConverter( | ||
| builderFactory = ::$builderName, | ||
| build = $builderName::build, | ||
| descriptors = listOf( | ||
${generateDescriptors(className, builderName, props)} | ||
| ), | ||
|) | ||
| | ||
|public object $schemaName : ItemSchema.PartitionKey<$className, ${keyProp.typeName.getShortName()}> { | ||
| override val converter: $converterName = $converterName | ||
| override val partitionKey: KeySpec.${keyProp.keySpecType} = ${generateKeySpec(keyProp)} | ||
|} | ||
| | ||
|public fun DynamoDbMapper.get${className}Table(name: String): Table.PartitionKey<$className, ${keyProp.typeName.getShortName()}> = getTable(name, $schemaName) | ||
| | ||
""".trimMargin(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun generateBuildMethod(className: String, props: Sequence<Property>) = | ||
buildString { | ||
appendLine("public fun build(): $className {") | ||
|
||
props.forEach { prop -> | ||
appendLine(""" val ${prop.name} = requireNotNull(${prop.name}) { "Missing value for $className.${prop.name}" }""") | ||
} | ||
|
||
appendLine() | ||
|
||
append(" return $className(") | ||
append(props.joinToString(", ") { it.name }) | ||
appendLine(")") | ||
|
||
appendLine(" }") | ||
}.trimEnd() | ||
|
||
private fun generateDescriptors( | ||
className: String, | ||
builderName: String, | ||
props: Sequence<Property>, | ||
) = buildString { | ||
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" | ||
else -> error("Unsupported attribute type $fqTypeName") | ||
} | ||
|
||
append("| AttributeDescriptor(") | ||
|
||
// key | ||
append("\"") | ||
append(prop.ddbName) | ||
append("\", ") | ||
|
||
// getter | ||
append(className) | ||
append("::") | ||
append(prop.name) | ||
append(", ") | ||
|
||
// setter | ||
append(builderName) | ||
append("::") | ||
append(prop.name) | ||
append("::set, ") | ||
|
||
// converter | ||
append(converterType) | ||
|
||
appendLine("),") | ||
} | ||
}.trimEnd() | ||
|
||
private fun generateKeySpec(keyProp: Property) = buildString { | ||
append("KeySpec.") | ||
append(keyProp.keySpecType) | ||
append("(\"") | ||
append(keyProp.name) | ||
append("\")") | ||
} | ||
|
||
private fun generateProperties(props: Sequence<Property>) = buildString { | ||
props.forEach { prop -> | ||
append("| public var ") | ||
append(prop.name) | ||
append(": ") | ||
append(prop.typeName.asString()) | ||
appendLine("? = null") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private data class Property(val name: String, val ddbName: String, val typeName: KSName, val isPk: Boolean) { | ||
companion object { | ||
@OptIn(KspExperimental::class) | ||
fun from(ksProperty: KSPropertyDeclaration) = ksProperty | ||
.getter | ||
?.returnType | ||
?.resolve() | ||
?.declaration | ||
?.qualifiedName | ||
?.let { typeName -> | ||
val isPk = ksProperty.isAnnotationPresent(DynamoDbPartitionKey::class) | ||
val name = ksProperty.simpleName.getShortName() | ||
val ddbName = ksProperty.getAnnotationsByType(DynamodDbAttribute::class).singleOrNull()?.name ?: name | ||
Property(name, ddbName, typeName, isPk) | ||
} | ||
} | ||
} | ||
|
||
private val Property.keySpecType: String | ||
get() = when (val fqTypeName = typeName.asString()) { | ||
"kotlin.Int" -> "N" | ||
"kotlin.String" -> "S" | ||
else -> error("Unsupported key type $fqTypeName, expected Int or String") | ||
} |
12 changes: 12 additions & 0 deletions
12
...-processor/jvm/src/aws/sdk/kotlin/hll/dynamodbmapper/processor/MapperProcessorProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.hll.dynamodbmapper.processor | ||
|
||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
|
||
public class MapperProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): MapperProcessor = MapperProcessor(environment) | ||
} |
13 changes: 13 additions & 0 deletions
13
hll/ddb-mapper/dynamodb-mapper-annotations/api/dynamodb-mapper-annotations.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public abstract interface annotation class aws/sdk/kotlin/hll/dynamodbmapper/DynamoDbItem : java/lang/annotation/Annotation { | ||
} | ||
|
||
public abstract interface annotation class aws/sdk/kotlin/hll/dynamodbmapper/DynamoDbPartitionKey : java/lang/annotation/Annotation { | ||
} | ||
|
||
public abstract interface annotation class aws/sdk/kotlin/hll/dynamodbmapper/DynamoDbSortKey : java/lang/annotation/Annotation { | ||
} | ||
|
||
public abstract interface annotation class aws/sdk/kotlin/hll/dynamodbmapper/DynamodDbAttribute : java/lang/annotation/Annotation { | ||
public abstract fun name ()Ljava/lang/String; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ |
Oops, something went wrong.