From f41b900ba7eb185ad5dc898ed9805d16bcc47ce7 Mon Sep 17 00:00:00 2001 From: Aaron J Todd Date: Wed, 4 Oct 2023 12:24:07 -0400 Subject: [PATCH] refactor: rename transform package to serde to align with our terminology --- .../smithy/kotlin/codegen/KotlinSettings.kt | 6 +++ .../protocol/HttpBindingProtocolGenerator.kt | 8 +-- .../protocol/HttpProtocolClientGenerator.kt | 3 +- .../codegen/rendering/serde/SerdeExt.kt | 16 +++--- .../codegen/IdempotentTokenGeneratorTest.kt | 14 ++--- .../HttpBindingProtocolGeneratorTest.kt | 52 +++++++++---------- .../HttpProtocolClientGeneratorTest.kt | 2 +- 7 files changed, 53 insertions(+), 48 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index 59558ae83..6251352fa 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -50,6 +50,12 @@ data class KotlinSettings( * Derive a subpackage namespace from the root package name */ fun subpackage(subpackageName: String): String = "$name.$subpackageName" + + /** + * The package namespace for generated serialization/deserialization support + */ + val serde: String + get() = subpackage("serde") } /** diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt index 2ed4bb155..bd735a157 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt @@ -155,7 +155,7 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator { val serializerSymbol = buildSymbol { definitionFile = "${op.serializerName()}.kt" name = op.serializerName() - namespace = "${ctx.settings.pkg.name}.transform" + namespace = ctx.settings.pkg.serde reference(inputSymbol, SymbolReference.ContextOption.DECLARE) } @@ -487,7 +487,7 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator { val definitionFileName = op.deserializerName().replaceFirstChar(Char::uppercaseChar) definitionFile = "$definitionFileName.kt" name = op.deserializerName() - namespace = ctx.settings.pkg.subpackage("transform") + namespace = ctx.settings.pkg.serde reference(outputSymbol, SymbolReference.ContextOption.DECLARE) } @@ -542,7 +542,7 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator { val deserializerName = "${outputSymbol.name}Deserializer" definitionFile = "$deserializerName.kt" name = deserializerName - namespace = ctx.settings.pkg.subpackage("transform") + namespace = ctx.settings.pkg.serde reference(outputSymbol, SymbolReference.ContextOption.DECLARE) } @@ -996,7 +996,7 @@ fun OperationShape.errorHandlerName(): String = "throw${capitalizedDefaultName() */ fun OperationShape.errorHandler(settings: KotlinSettings, block: SymbolRenderer): Symbol = buildSymbol { name = errorHandlerName() - namespace = "${settings.pkg.name}.transform" + namespace = settings.pkg.serde // place error handler in same file as operation deserializer definitionFile = "${deserializerName()}.kt" renderBy = block diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpProtocolClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpProtocolClientGenerator.kt index 0a8f1f690..fdc98c6b5 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpProtocolClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpProtocolClientGenerator.kt @@ -127,9 +127,8 @@ abstract class HttpProtocolClientGenerator( } protected open fun importSymbols(writer: KotlinWriter) { - writer.addImport("${ctx.settings.pkg.name}.model", "*") if (TopDownIndex(ctx.model).getContainedOperations(ctx.service).isNotEmpty()) { - writer.addImport("${ctx.settings.pkg.name}.transform", "*") + writer.addImport(ctx.settings.pkg.serde, "*") } val defaultClientSymbols = setOf( diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerdeExt.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerdeExt.kt index af69da8bb..56ab30cd5 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerdeExt.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerdeExt.kt @@ -42,8 +42,8 @@ fun OperationShape.bodySerializer( block: SymbolRenderer, ): Symbol = buildSymbol { name = bodySerializerName() - namespace = "${settings.pkg.name}.transform" - // place body serializer in same file as operation serializer implementaiton + namespace = settings.pkg.serde + // place body serializer in same file as operation serializer implementation definitionFile = "${serializerName()}.kt" renderBy = block } @@ -68,7 +68,7 @@ fun OperationShape.bodyDeserializer( block: SymbolRenderer, ): Symbol = buildSymbol { name = bodyDeserializerName() - namespace = "${settings.pkg.name}.transform" + namespace = settings.pkg.serde // place body serializer in same file as operation serializer implementation definitionFile = "${deserializerName()}.kt" renderBy = block @@ -95,7 +95,7 @@ fun Shape.documentSerializer( return buildSymbol { name = "$base$suffix" - namespace = settings.pkg.subpackage("transform") + namespace = settings.pkg.serde definitionFile = "${symbol.name}DocumentSerializer.kt" reference(symbol, SymbolReference.ContextOption.DECLARE) renderBy = block @@ -123,7 +123,7 @@ fun Shape.documentDeserializer( return buildSymbol { name = "$base$suffix" - namespace = settings.pkg.subpackage("transform") + namespace = settings.pkg.serde definitionFile = "${symbol.name}DocumentDeserializer.kt" reference(symbol, SymbolReference.ContextOption.DECLARE) renderBy = block @@ -141,7 +141,7 @@ fun Symbol.errorDeserializerName(): String = "deserialize" + StringUtils.capital */ fun Symbol.errorDeserializer(settings: KotlinSettings, block: SymbolRenderer): Symbol = buildSymbol { name = errorDeserializerName() - namespace = "${settings.pkg.name}.transform" + namespace = settings.pkg.serde val symbol = this@errorDeserializer // place it in the same file as the exception deserializer, e.g. for HTTP protocols this will be in // same file as HttpDeserialize @@ -163,7 +163,7 @@ fun Shape.payloadDeserializer( val suffix = mangledSuffix(members) return buildSymbol { name = "$base$suffix" - namespace = settings.pkg.subpackage("transform") + namespace = settings.pkg.serde definitionFile = "${symbol.name}PayloadDeserializer.kt" reference(symbol, SymbolReference.ContextOption.DECLARE) renderBy = block @@ -183,7 +183,7 @@ fun Shape.payloadSerializer( val suffix = mangledSuffix(members) return buildSymbol { name = "$base$suffix" - namespace = "${settings.pkg.name}.transform" + namespace = settings.pkg.serde definitionFile = "${symbol.name}PayloadSerializer.kt" reference(symbol, SymbolReference.ContextOption.DECLARE) renderBy = block diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/IdempotentTokenGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/IdempotentTokenGeneratorTest.kt index d92c3ac32..765ee5a1b 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/IdempotentTokenGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/IdempotentTokenGeneratorTest.kt @@ -13,22 +13,22 @@ import kotlin.test.Test class IdempotentTokenGeneratorTest { private val defaultModel: Model = javaClass.getResource("idempotent-token-test-model.smithy").toSmithyModel() - private fun getTransformFileContents(filename: String): String { + private fun getSerdeFileContents(filename: String): String { val (ctx, manifest, generator) = defaultModel.newTestContext() generator.generateProtocolClient(ctx) ctx.delegator.flushWriters() - return manifest.getTransformFileContents(filename) + return manifest.getSerdeFileContents(filename) } // Assume a specific file path to retrieve a file from the manifest - private fun MockManifest.getTransformFileContents(filename: String, packageNamespace: String = TestModelDefault.NAMESPACE): String { + private fun MockManifest.getSerdeFileContents(filename: String, packageNamespace: String = TestModelDefault.NAMESPACE): String { val packageNamespaceExpr = packageNamespace.replace('.', '/') - return expectFileString("src/main/kotlin/$packageNamespaceExpr/transform/$filename") + return expectFileString("src/main/kotlin/$packageNamespaceExpr/serde/$filename") } @Test fun `it serializes operation payload inputs with idempotency token trait`() { - val contents = getTransformFileContents("AllocateWidgetOperationSerializer.kt") + val contents = getSerdeFileContents("AllocateWidgetOperationSerializer.kt") contents.assertBalancedBracesAndParens() val expectedContents = """ internal class AllocateWidgetOperationSerializer: HttpSerialize { @@ -54,7 +54,7 @@ class IdempotentTokenGeneratorTest { @Test fun `it serializes operation query inputs with idempotency token trait`() { - val contents = getTransformFileContents("AllocateWidgetQueryOperationSerializer.kt") + val contents = getSerdeFileContents("AllocateWidgetQueryOperationSerializer.kt") contents.assertBalancedBracesAndParens() val expectedContents = """ internal class AllocateWidgetQueryOperationSerializer: HttpSerialize { @@ -78,7 +78,7 @@ internal class AllocateWidgetQueryOperationSerializer: HttpSerialize { diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGeneratorTest.kt index 9e0b37b3d..1e7321838 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGeneratorTest.kt @@ -17,27 +17,27 @@ import kotlin.test.assertTrue // NOTE: protocol conformance is mostly handled by the protocol tests suite class HttpBindingProtocolGeneratorTest { private val defaultModel = loadModelFromResource("http-binding-protocol-generator-test.smithy") - private fun getTransformFileContents(filename: String, testModel: Model = defaultModel): String { + private fun getSerdeFileContents(filename: String, testModel: Model = defaultModel): String { val (ctx, manifest, generator) = testModel.newTestContext() generator.generateProtocolClient(ctx) ctx.delegator.flushWriters() - return getTransformFileContents(manifest, filename) + return getSerdeFileContents(manifest, filename) } - private fun getTransformFileContents(manifest: MockManifest, filename: String): String = manifest - .expectFileString("src/main/kotlin/com/test/transform/$filename") + private fun getSerdeFileContents(manifest: MockManifest, filename: String): String = manifest + .expectFileString("src/main/kotlin/com/test/serde/$filename") @Test fun itCreatesSerializeTransformsInCorrectPackage() { val (ctx, manifest, generator) = defaultModel.newTestContext() generator.generateProtocolClient(ctx) ctx.delegator.flushWriters() - assertTrue(manifest.hasFile("src/main/kotlin/com/test/transform/SmokeTestOperationSerializer.kt")) + assertTrue(manifest.hasFile("src/main/kotlin/com/test/serde/SmokeTestOperationSerializer.kt")) } @Test fun itCreatesSmokeTestRequestSerializer() { - val contents = getTransformFileContents("SmokeTestOperationSerializer.kt") + val contents = getSerdeFileContents("SmokeTestOperationSerializer.kt") contents.assertBalancedBracesAndParens() val label1 = "\${input.label1}" // workaround for raw strings not being able to contain escapes val expectedContents = """ @@ -79,7 +79,7 @@ internal class SmokeTestOperationSerializer: HttpSerialize { @Test fun itSerializesExplicitStringPayloads() { - val contents = getTransformFileContents("ExplicitStringOperationSerializer.kt") + val contents = getSerdeFileContents("ExplicitStringOperationSerializer.kt") contents.assertBalancedBracesAndParens() val expectedContents = """ internal class ExplicitStringOperationSerializer: HttpSerialize { @@ -106,7 +106,7 @@ internal class ExplicitStringOperationSerializer: HttpSerialize { @@ -133,7 +133,7 @@ internal class ExplicitBlobOperationSerializer: HttpSerialize { @@ -160,7 +160,7 @@ internal class ExplicitBlobStreamOperationSerializer: HttpSerialize { @@ -188,7 +188,7 @@ internal class ExplicitStructOperationSerializer: HttpSerialize { @@ -216,7 +216,7 @@ internal class ExplicitDocumentOperationSerializer: HttpSerialize { @@ -246,7 +246,7 @@ internal class EnumInputOperationSerializer: HttpSerialize { @Test fun itSerializesOperationInputsWithTimestamps() { - val contents = getTransformFileContents("TimestampInputOperationSerializer.kt") + val contents = getSerdeFileContents("TimestampInputOperationSerializer.kt") contents.assertBalancedBracesAndParens() val tsLabel = "\${input.tsLabel?.format(TimestampFormat.ISO_8601)}" // workaround for raw strings not being able to contain escapes val expectedContents = """ @@ -291,7 +291,7 @@ internal class TimestampInputOperationSerializer: HttpSerialize { @@ -323,7 +323,7 @@ internal class BlobInputOperationSerializer: HttpSerialize { @Test fun itHandlesQueryStringLiterals() { - val contents = getTransformFileContents("ConstantQueryStringOperationSerializer.kt") + val contents = getSerdeFileContents("ConstantQueryStringOperationSerializer.kt") contents.assertBalancedBracesAndParens() val label1 = "\${input.hello}" // workaround for raw strings not being able to contain escapes val expectedContents = """ @@ -353,7 +353,7 @@ internal class ConstantQueryStringOperationSerializer: HttpSerialize { @@ -383,7 +383,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize