Skip to content

Commit

Permalink
refactor: rename transform package to serde to align with our termino…
Browse files Browse the repository at this point in the history
…logy
  • Loading branch information
aajtodd committed Oct 4, 2023
1 parent af125b7 commit f41b900
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AllocateWidgetRequest> {
Expand All @@ -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<AllocateWidgetQueryRequest> {
Expand All @@ -78,7 +78,7 @@ internal class AllocateWidgetQueryOperationSerializer: HttpSerialize<AllocateWid

@Test
fun `it serializes operation header inputs with idempotency token trait`() {
val contents = getTransformFileContents("AllocateWidgetHeaderOperationSerializer.kt")
val contents = getSerdeFileContents("AllocateWidgetHeaderOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class AllocateWidgetHeaderOperationSerializer: HttpSerialize<AllocateWidgetHeaderRequest> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -79,7 +79,7 @@ internal class SmokeTestOperationSerializer: HttpSerialize<SmokeTestRequest> {

@Test
fun itSerializesExplicitStringPayloads() {
val contents = getTransformFileContents("ExplicitStringOperationSerializer.kt")
val contents = getSerdeFileContents("ExplicitStringOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class ExplicitStringOperationSerializer: HttpSerialize<ExplicitStringRequest> {
Expand All @@ -106,7 +106,7 @@ internal class ExplicitStringOperationSerializer: HttpSerialize<ExplicitStringRe

@Test
fun itSerializesExplicitBlobPayloads() {
val contents = getTransformFileContents("ExplicitBlobOperationSerializer.kt")
val contents = getSerdeFileContents("ExplicitBlobOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class ExplicitBlobOperationSerializer: HttpSerialize<ExplicitBlobRequest> {
Expand All @@ -133,7 +133,7 @@ internal class ExplicitBlobOperationSerializer: HttpSerialize<ExplicitBlobReques

@Test
fun itSerializesExplicitStreamingBlobPayloads() {
val contents = getTransformFileContents("ExplicitBlobStreamOperationSerializer.kt")
val contents = getSerdeFileContents("ExplicitBlobStreamOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class ExplicitBlobStreamOperationSerializer: HttpSerialize<ExplicitBlobStreamRequest> {
Expand All @@ -160,7 +160,7 @@ internal class ExplicitBlobStreamOperationSerializer: HttpSerialize<ExplicitBlob

@Test
fun itSerializesExplicitStructPayloads() {
val contents = getTransformFileContents("ExplicitStructOperationSerializer.kt")
val contents = getSerdeFileContents("ExplicitStructOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class ExplicitStructOperationSerializer: HttpSerialize<ExplicitStructRequest> {
Expand Down Expand Up @@ -188,7 +188,7 @@ internal class ExplicitStructOperationSerializer: HttpSerialize<ExplicitStructRe

@Test
fun itSerializesExplicitDocumentPayloads() {
val contents = getTransformFileContents("ExplicitDocumentOperationSerializer.kt")
val contents = getSerdeFileContents("ExplicitDocumentOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class ExplicitDocumentOperationSerializer: HttpSerialize<ExplicitDocumentRequest> {
Expand Down Expand Up @@ -216,7 +216,7 @@ internal class ExplicitDocumentOperationSerializer: HttpSerialize<ExplicitDocume

@Test
fun itSerializesOperationInputsWithEnums() {
val contents = getTransformFileContents("EnumInputOperationSerializer.kt")
val contents = getSerdeFileContents("EnumInputOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class EnumInputOperationSerializer: HttpSerialize<EnumInputRequest> {
Expand Down Expand Up @@ -246,7 +246,7 @@ internal class EnumInputOperationSerializer: HttpSerialize<EnumInputRequest> {

@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 = """
Expand Down Expand Up @@ -291,7 +291,7 @@ internal class TimestampInputOperationSerializer: HttpSerialize<TimestampInputRe
fun itCreatesBlobInputRequestSerializer() {
// base64 encoding is protocol dependent. The mock protocol generator is based on
// json protocol though which does encode to base64
val contents = getTransformFileContents("BlobInputOperationSerializer.kt")
val contents = getSerdeFileContents("BlobInputOperationSerializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class BlobInputOperationSerializer: HttpSerialize<BlobInputRequest> {
Expand Down Expand Up @@ -323,7 +323,7 @@ internal class BlobInputOperationSerializer: HttpSerialize<BlobInputRequest> {

@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 = """
Expand Down Expand Up @@ -353,7 +353,7 @@ internal class ConstantQueryStringOperationSerializer: HttpSerialize<ConstantQue

@Test
fun itCreatesSmokeTestResponseDeserializer() {
val contents = getTransformFileContents("SmokeTestOperationDeserializer.kt")
val contents = getSerdeFileContents("SmokeTestOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse> {
Expand Down Expand Up @@ -383,7 +383,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesPrefixHeaders() {
val contents = getTransformFileContents("PrefixHeadersOperationDeserializer.kt")
val contents = getSerdeFileContents("PrefixHeadersOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
val keysForMember1 = response.headers.names().filter { it.startsWith("X-Foo-") }
Expand All @@ -404,7 +404,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesPrimitiveHeaders() {
val contents = getTransformFileContents("PrimitiveShapesOperationOperationDeserializer.kt")
val contents = getSerdeFileContents("PrimitiveShapesOperationOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
builder.hBool = response.headers["X-d"]?.toBoolean() ?: false
Expand All @@ -418,7 +418,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitStringPayloads() {
val contents = getTransformFileContents("ExplicitStringOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitStringOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
val contents = response.body.readAll()?.decodeToString()
Expand All @@ -429,7 +429,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitEnumPayloads() {
val contents = getTransformFileContents("ExplicitEnumOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitEnumOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
val contents = response.body.readAll()?.decodeToString()
Expand All @@ -440,7 +440,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitBlobPayloads() {
val contents = getTransformFileContents("ExplicitBlobOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitBlobOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
builder.payload1 = response.body.readAll()
Expand All @@ -450,7 +450,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitStreamingBlobPayloads() {
val contents = getTransformFileContents("ExplicitBlobStreamOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitBlobStreamOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
builder.payload1 = response.body.toByteStream()
Expand All @@ -460,7 +460,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitStructPayloads() {
val contents = getTransformFileContents("ExplicitStructOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitStructOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
val payload = response.body.readAll()
Expand All @@ -473,7 +473,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse

@Test
fun itDeserializesExplicitDocumentPayloads() {
val contents = getTransformFileContents("ExplicitDocumentOperationDeserializer.kt")
val contents = getSerdeFileContents("ExplicitDocumentOperationDeserializer.kt")
contents.assertBalancedBracesAndParens()
val expectedContents = """
val payload = response.body.readAll()
Expand All @@ -487,7 +487,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse
@Test
fun itLeavesOffContentType() {
// GET/HEAD/TRACE/OPTIONS/CONNECT shouldn't specify content-type
val contents = getTransformFileContents("ConstantQueryStringOperationSerializer.kt")
val contents = getSerdeFileContents("ConstantQueryStringOperationSerializer.kt")
contents.shouldNotContain("Content-Type")
}

Expand All @@ -502,7 +502,7 @@ internal class SmokeTestOperationDeserializer: HttpDeserialize<SmokeTestResponse
""".prependNamespaceAndService(operations = listOf("Foo"))
.toSmithyModel()

val contents = getTransformFileContents("FooOperationSerializer.kt", model)
val contents = getSerdeFileContents("FooOperationSerializer.kt", model)

val latest = "\\\$LATEST"
val expected = """
Expand Down
Loading

0 comments on commit f41b900

Please sign in to comment.