Skip to content

Commit 15a9472

Browse files
authored
feat: basic annotation processing (#1399)
1 parent be764b7 commit 15a9472

File tree

38 files changed

+952
-143
lines changed

38 files changed

+952
-143
lines changed

hll/dynamodb-mapper/dynamodb-mapper-codegen/src/main/kotlin/aws/sdk/kotlin/hll/dynamodbmapper/codegen/annotations/AnnotationsProcessor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class AnnotationsProcessor(private val environment: SymbolProcessorEnviro
2323
private var invoked = false
2424
private val logger = environment.logger
2525
private val codeGenerator = environment.codeGenerator
26-
private val codeGeneratorFactory = CodeGeneratorFactory(codeGenerator, logger)
2726

2827
override fun process(resolver: Resolver): List<KSAnnotated> {
2928
if (invoked) {
@@ -42,6 +41,9 @@ public class AnnotationsProcessor(private val environment: SymbolProcessorEnviro
4241
.filterIsInstance<KSClassDeclaration>()
4342
.filter { it.validate() }
4443

44+
val dependencies = Dependencies(aggregating = true, *(annotatedClasses.mapNotNull { it.containingFile }.toTypedArray()))
45+
val codeGeneratorFactory = CodeGeneratorFactory(environment.codeGenerator, logger, dependencies)
46+
4547
HighLevelRenderer(annotatedClasses, logger, codeGeneratorFactory, getCodegenAttributes()).render()
4648

4749
return invalid

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ package aws.sdk.kotlin.hll.dynamodbmapper.codegen.annotations.rendering
66

77
import aws.sdk.kotlin.hll.codegen.core.CodeGeneratorFactory
88
import aws.sdk.kotlin.hll.codegen.rendering.RenderContext
9+
import aws.sdk.kotlin.hll.codegen.util.plus
910
import aws.sdk.kotlin.hll.dynamodbmapper.codegen.annotations.AnnotationsProcessorOptions
1011
import aws.sdk.kotlin.hll.dynamodbmapper.codegen.annotations.DestinationPackage
11-
import aws.smithy.kotlin.runtime.collections.Attributes
12-
import aws.smithy.kotlin.runtime.collections.emptyAttributes
13-
import aws.smithy.kotlin.runtime.collections.get
12+
import aws.smithy.kotlin.runtime.collections.*
1413
import com.google.devtools.ksp.processing.KSPLogger
1514
import com.google.devtools.ksp.symbol.KSClassDeclaration
1615

@@ -25,24 +24,45 @@ internal class HighLevelRenderer(
2524
private val codegenAttributes: Attributes = emptyAttributes(),
2625
) {
2726
internal fun render() {
28-
annotatedClasses.forEach {
29-
logger.info("Processing annotation on ${it.simpleName}")
27+
annotatedClasses.forEach { annotated ->
28+
logger.info("Processing annotation on ${annotated.simpleName}")
3029

3130
val codegenPkg = when (val dstPkg = codegenAttributes[AnnotationsProcessorOptions.DestinationPackageAttribute]) {
32-
is DestinationPackage.Relative -> "${it.packageName.asString()}.${dstPkg.pkg}"
31+
is DestinationPackage.Relative -> "${annotated.packageName.asString()}.${dstPkg.pkg}"
3332
is DestinationPackage.Absolute -> dstPkg.pkg
3433
}
3534

35+
val attributes = codegenAttributes + (SchemaAttributes.ShouldRenderValueConverterAttribute to annotated.shouldRenderValueConverter)
36+
3637
val renderCtx = RenderContext(
3738
logger,
3839
codegenFactory,
3940
codegenPkg,
4041
"dynamodb-mapper-annotation-processor",
41-
codegenAttributes,
42+
attributes,
4243
)
4344

44-
val annotation = SchemaRenderer(it, renderCtx)
45+
val annotation = SchemaRenderer(annotated, renderCtx)
4546
annotation.render()
4647
}
4748
}
49+
50+
// Value converters must be generated for any DynamoDbItem which is referenced by another DynamoDbItem
51+
private val KSClassDeclaration.shouldRenderValueConverter: Boolean
52+
get() = annotatedClasses.any { otherClass ->
53+
val name = requireNotNull(qualifiedName).asString()
54+
55+
otherClass.getAllProperties().any { prop ->
56+
val propType = prop.type.resolve()
57+
val propName = requireNotNull(propType.declaration.qualifiedName).asString()
58+
59+
// If the property OR any of its arguments reference the annotated type
60+
propName == name ||
61+
propType.arguments.any { arg ->
62+
val argType = arg.type?.resolve()
63+
val argName = requireNotNull(argType?.declaration?.qualifiedName).asString()
64+
argName == name
65+
}
66+
}
67+
}
4868
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.hll.dynamodbmapper.codegen.annotations.rendering
6+
7+
import aws.smithy.kotlin.runtime.collections.AttributeKey
8+
9+
/**
10+
* Internal schema code generation attributes
11+
*/
12+
internal object SchemaAttributes {
13+
/**
14+
* Whether a value converter should be generated for the class being processed
15+
*/
16+
internal val ShouldRenderValueConverterAttribute: AttributeKey<Boolean> = AttributeKey("ShouldRenderValueConverter")
17+
}

0 commit comments

Comments
 (0)