From 000bbb7a821a3cac485a54cae0ffdf3202a94923 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Mon, 29 Aug 2022 12:33:36 -0700 Subject: [PATCH 01/14] Very naive way of attempting to apply mixins. --- .../amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt index 22258b27d2..fd9890a187 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt @@ -10,6 +10,7 @@ import software.amazon.smithy.build.SmithyBuildPlugin import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape +import software.amazon.smithy.model.transform.ModelTransformer import software.amazon.smithy.rust.codegen.rustlang.Attribute.Companion.NonExhaustive import software.amazon.smithy.rust.codegen.rustlang.RustReservedWordSymbolProvider import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations @@ -34,6 +35,7 @@ class RustCodegenPlugin : SmithyBuildPlugin { override fun execute(context: PluginContext) { // Suppress extremely noisy logs about reserved words Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF + val codegenContext = context.toBuilder().model(ModelTransformer.create().flattenAndRemoveMixins(context.model)).build() // Discover `RustCodegenDecorators` on the classpath. `RustCodegenDecorator` returns different types of // customizations. A customization is a function of: // - location (e.g. the mutate section of an operation) @@ -41,7 +43,7 @@ class RustCodegenPlugin : SmithyBuildPlugin { // - writer: The active RustWriter at the given location val codegenDecorator = CombinedCodegenDecorator.fromClasspath( - context, + codegenContext, ClientCustomizations(), RequiredCustomizations(), FluentClientDecorator(), @@ -49,7 +51,7 @@ class RustCodegenPlugin : SmithyBuildPlugin { ) // CodegenVisitor is the main driver of code generation that traverses the model and generates code - CodegenVisitor(context, codegenDecorator).execute() + CodegenVisitor(codegenContext, codegenDecorator).execute() } companion object { From 617063aaa6cc77da8e12feb6b18f0cfd3dcf5ccc Mon Sep 17 00:00:00 2001 From: David Oguns Date: Mon, 29 Aug 2022 12:59:52 -0700 Subject: [PATCH 02/14] Format --- .../amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt index fd9890a187..97cb16d520 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt @@ -35,7 +35,9 @@ class RustCodegenPlugin : SmithyBuildPlugin { override fun execute(context: PluginContext) { // Suppress extremely noisy logs about reserved words Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF - val codegenContext = context.toBuilder().model(ModelTransformer.create().flattenAndRemoveMixins(context.model)).build() + val codegenContext = context.toBuilder() + .model(ModelTransformer.create().flattenAndRemoveMixins(context.model)) + .build() // Discover `RustCodegenDecorators` on the classpath. `RustCodegenDecorator` returns different types of // customizations. A customization is a function of: // - location (e.g. the mutate section of an operation) From 42fabb284d042a1c9e5b557f08d3e778db8d74ad Mon Sep 17 00:00:00 2001 From: David Oguns Date: Mon, 29 Aug 2022 15:18:21 -0700 Subject: [PATCH 03/14] Added logic to baseline transform instead. --- .../rust/codegen/server/smithy/ServerCodegenVisitor.kt | 2 ++ .../smithy/rust/codegen/smithy/RustCodegenPlugin.kt | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index c07e952373..cbf3eeeeb0 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -103,6 +103,8 @@ open class ServerCodegenVisitor( */ protected fun baselineTransform(model: Model) = model + // Flattens mixins out of the model and removes them from the model + .let { ModelTransformer.create().flattenAndRemoveMixins(it) } // Add errors attached at the service level to the models .let { ModelTransformer.create().copyServiceErrorsToOperations(it, settings.getService(it)) } // Add `Box` to recursive shapes as necessary diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt index 97cb16d520..22258b27d2 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt @@ -10,7 +10,6 @@ import software.amazon.smithy.build.SmithyBuildPlugin import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape -import software.amazon.smithy.model.transform.ModelTransformer import software.amazon.smithy.rust.codegen.rustlang.Attribute.Companion.NonExhaustive import software.amazon.smithy.rust.codegen.rustlang.RustReservedWordSymbolProvider import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations @@ -35,9 +34,6 @@ class RustCodegenPlugin : SmithyBuildPlugin { override fun execute(context: PluginContext) { // Suppress extremely noisy logs about reserved words Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF - val codegenContext = context.toBuilder() - .model(ModelTransformer.create().flattenAndRemoveMixins(context.model)) - .build() // Discover `RustCodegenDecorators` on the classpath. `RustCodegenDecorator` returns different types of // customizations. A customization is a function of: // - location (e.g. the mutate section of an operation) @@ -45,7 +41,7 @@ class RustCodegenPlugin : SmithyBuildPlugin { // - writer: The active RustWriter at the given location val codegenDecorator = CombinedCodegenDecorator.fromClasspath( - codegenContext, + context, ClientCustomizations(), RequiredCustomizations(), FluentClientDecorator(), @@ -53,7 +49,7 @@ class RustCodegenPlugin : SmithyBuildPlugin { ) // CodegenVisitor is the main driver of code generation that traverses the model and generates code - CodegenVisitor(codegenContext, codegenDecorator).execute() + CodegenVisitor(context, codegenDecorator).execute() } companion object { From d5a51040b240d05451f8404a832e23ee104e16f8 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Mon, 29 Aug 2022 15:53:09 -0700 Subject: [PATCH 04/14] Add baseline transform to client codegen as well. --- .../amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt index e5c133f32c..4eedb47805 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt @@ -88,6 +88,8 @@ class CodegenVisitor(context: PluginContext, private val codegenDecorator: RustC */ private fun baselineTransform(model: Model) = model + // Flattens mixins out of the model and removes them from the model + .let { ModelTransformer.create().flattenAndRemoveMixins(it) } // Add errors attached at the service level to the models .let { ModelTransformer.create().copyServiceErrorsToOperations(it, settings.getService(it)) } // Add `Box` to recursive shapes as necessary From 408734f57dea988d93411bb021a5af9b708f4b37 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Tue, 30 Aug 2022 10:02:25 -0700 Subject: [PATCH 05/14] WIP -- improve asSmithyModel for IDLv2. Need to add same unit test for server codegen. --- .../server/smithy/ServerCodegenVisitor.kt | 2 +- .../rust/codegen/smithy/CodegenVisitor.kt | 2 +- .../rust/codegen/testutil/TestHelpers.kt | 6 ++ .../codegen/smithy/BaselineTransformTest.kt | 75 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index cbf3eeeeb0..d75ec836ae 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -101,7 +101,7 @@ open class ServerCodegenVisitor( * Base model transformation applied to all services. * See below for details. */ - protected fun baselineTransform(model: Model) = + internal fun baselineTransform(model: Model) = model // Flattens mixins out of the model and removes them from the model .let { ModelTransformer.create().flattenAndRemoveMixins(it) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt index 4eedb47805..184b94581c 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt @@ -86,7 +86,7 @@ class CodegenVisitor(context: PluginContext, private val codegenDecorator: RustC * Base model transformation applied to all services * See below for details. */ - private fun baselineTransform(model: Model) = + internal fun baselineTransform(model: Model) = model // Flattens mixins out of the model and removes them from the model .let { ModelTransformer.create().flattenAndRemoveMixins(it) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt index 7883f79b67..f144e56134 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt @@ -94,6 +94,12 @@ fun String.asSmithyModel(sourceLocation: String? = null): Model { .unwrap() } +fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { + val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } + return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() + .unwrap() +} + /** * In tests, we frequently need to generate a struct, a builder, and an impl block to access said builder. */ diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt new file mode 100644 index 0000000000..3ca3831a42 --- /dev/null +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt @@ -0,0 +1,75 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.smithy + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import software.amazon.smithy.model.Model +import software.amazon.smithy.model.shapes.OperationShape +import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.model.shapes.StructureShape +import software.amazon.smithy.model.traits.MixinTrait +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.util.cloneOperation +import software.amazon.smithy.rust.codegen.util.orNull +import software.amazon.smithy.rust.codegen.util.rename +import software.amazon.smithy.rust.codegen.testutil.generatePluginContext +import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator +import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizations +import software.amazon.smithy.rust.codegen.smithy.customize.NoOpEventStreamSigningDecorator +import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations +import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator +import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor + +fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { + val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } + return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() + .unwrap() +} + +class BaselineTransformTest { + @Test + fun `verify mixins removed`() { + val model = """ + namespace com.example + + use aws.protocols#awsJson1_0 + + @awsJson1_0 + @aws.api#service(sdkId: "Test", endpointPrefix: "differentPrefix") + service Example { + operations: [ BasicOperation ] + } + + operation BasicOperation { + input: Shape + } + + @mixin + structure SimpleMixin { + name: String + } + + structure Shape with [ + SimpleMixin + ] { + greeting: String + } + """.asSmithyModel_Version2() + val (ctx, _) = generatePluginContext(model) + val codegenDecorator = + CombinedCodegenDecorator.fromClasspath( + ctx, + ClientCustomizations(), + RequiredCustomizations(), + FluentClientDecorator(), + NoOpEventStreamSigningDecorator(), + ) + val visitor = CodegenVisitor(ctx, codegenDecorator) + val baselineModel = visitor.baselineTransform(model) + baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true + } +} From 8882d0842bd6184f2a56b277aea03e419aaa4030 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Wed, 31 Aug 2022 13:45:27 -0700 Subject: [PATCH 06/14] Improved asSmithyModel function to accept Smithy version. Renamed test class and test case. --- .../smithy/rust/codegen/testutil/TestHelpers.kt | 10 ++-------- ...ineTransformTest.kt => CodegenVisitorTest.kt} | 16 ++++++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) rename codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/{BaselineTransformTest.kt => CodegenVisitorTest.kt} (84%) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt index f144e56134..55cc877c22 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt @@ -88,14 +88,8 @@ fun testCodegenContext( ) private const val SmithyVersion = "1.0" -fun String.asSmithyModel(sourceLocation: String? = null): Model { - val processed = letIf(!this.startsWith("\$version")) { "\$version: ${SmithyVersion.dq()}\n$it" } - return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() - .unwrap() -} - -fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { - val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } +fun String.asSmithyModel(sourceLocation: String? = null, smithyVersion: String = SmithyVersion): Model { + val processed = letIf(!this.startsWith("\$version")) { "\$version: ${smithyVersion.dq()}\n$it" } return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() .unwrap() } diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt similarity index 84% rename from codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt rename to codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index 3ca3831a42..c0c673e698 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/BaselineTransformTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -24,15 +24,15 @@ import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomiza import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor -fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { - val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } - return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() - .unwrap() -} +// fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { +// val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } +// return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() +// .unwrap() +// } -class BaselineTransformTest { +class CodegenVisitorTest { @Test - fun `verify mixins removed`() { + fun `baseline transform verify mixins removed`() { val model = """ namespace com.example @@ -58,7 +58,7 @@ class BaselineTransformTest { ] { greeting: String } - """.asSmithyModel_Version2() + """.asSmithyModel(smithyVersion = "2.0") val (ctx, _) = generatePluginContext(model) val codegenDecorator = CombinedCodegenDecorator.fromClasspath( From 9fadcb7e52eec5ed509a61b2af3f6a957e094286 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Wed, 31 Aug 2022 15:00:40 -0700 Subject: [PATCH 07/14] WIP -- test pass. ktlint issue needs fixed. --- .../server/smithy/ServerCodegenVisitorTest.kt | 53 +++++++++++++++++++ .../rust/codegen/smithy/CodegenVisitorTest.kt | 14 ----- 2 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt new file mode 100644 index 0000000000..96d2b0b780 --- /dev/null +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -0,0 +1,53 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.server.smithy + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.generatePluginContext +import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations +import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext +import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator + +class ServerCodegenVisitorTest { + @Test + fun `baseline transform verify mixins removed`() { + val model = """ + namespace com.example + + use aws.protocols#awsJson1_0 + + @awsJson1_0 + @aws.api#service(sdkId: "Test", endpointPrefix: "differentPrefix") + service Example { + operations: [ BasicOperation ] + } + + operation BasicOperation { + input: Shape + } + + @mixin + structure SimpleMixin { + name: String + } + + structure Shape with [ + SimpleMixin + ] { + greeting: String + } + """.asSmithyModel(smithyVersion = "2.0") + val (ctx, _) = generatePluginContext(model) + val codegenDecorator: CombinedCodegenDecorator = + CombinedCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) + val visitor = ServerCodegenVisitor(ctx, codegenDecorator) + val baselineModel = visitor.baselineTransform(model) + baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true + } +} diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index c0c673e698..02518a9aff 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -7,28 +7,14 @@ package software.amazon.smithy.rust.codegen.smithy import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.model.shapes.StructureShape -import software.amazon.smithy.model.traits.MixinTrait import software.amazon.smithy.rust.codegen.testutil.asSmithyModel -import software.amazon.smithy.rust.codegen.util.cloneOperation -import software.amazon.smithy.rust.codegen.util.orNull -import software.amazon.smithy.rust.codegen.util.rename import software.amazon.smithy.rust.codegen.testutil.generatePluginContext import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizations import software.amazon.smithy.rust.codegen.smithy.customize.NoOpEventStreamSigningDecorator import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator -import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor - -// fun String.asSmithyModel_Version2(sourceLocation: String? = null): Model { -// val processed = letIf(!this.startsWith("\$version")) { "\$version: \"2.0\"\n$it" } -// return Model.assembler().discoverModels().addUnparsedModel(sourceLocation ?: "test.smithy", processed).assemble() -// .unwrap() -// } class CodegenVisitorTest { @Test From c83b0e97d8c8265b961d4349233cff35e9962a8e Mon Sep 17 00:00:00 2001 From: David Oguns Date: Wed, 31 Aug 2022 15:05:02 -0700 Subject: [PATCH 08/14] Organized imports using InteliJ --- .../codegen/server/smithy/ServerCodegenVisitorTest.kt | 4 ++-- .../smithy/rust/codegen/smithy/CodegenVisitorTest.kt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt index 96d2b0b780..aaed2bbef2 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -8,11 +8,11 @@ package software.amazon.smithy.rust.codegen.server.smithy import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.testutil.asSmithyModel -import software.amazon.smithy.rust.codegen.testutil.generatePluginContext import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.generatePluginContext class ServerCodegenVisitorTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index 02518a9aff..0922595118 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -8,13 +8,13 @@ package software.amazon.smithy.rust.codegen.smithy import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.testutil.asSmithyModel -import software.amazon.smithy.rust.codegen.testutil.generatePluginContext +import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator -import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizations import software.amazon.smithy.rust.codegen.smithy.customize.NoOpEventStreamSigningDecorator -import software.amazon.smithy.rust.codegen.smithy.customizations.ClientCustomizations +import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizations import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.generatePluginContext class CodegenVisitorTest { @Test From b442b3649bc3c744c9da84914f7c700a9bd5dbe1 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Wed, 31 Aug 2022 15:10:47 -0700 Subject: [PATCH 09/14] Updated changelog next. --- CHANGELOG.next.toml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index ddb541e1d5..c8446ae7de 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -73,7 +73,6 @@ references = ["smithy-rs#1603", "aws-sdk-rust#586"] meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client" } author = "jdisanti" - [[aws-sdk-rust]] message = """ Implemented customizable operations per [RFC-0017](https://awslabs.github.io/smithy-rs/design/rfcs/rfc0017_customizable_client_operations.html). @@ -169,3 +168,9 @@ let response = client.some_operation() references = ["smithy-rs#1647", "smithy-rs#1112"] meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "client"} author = "Velfi" + +[[smithy-rs]] +message = "Smithy IDL v2 mixins are now supported" +references = ["smithy-rs#1680"] +meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all"} +author = "ogudavid" From 687dc93257d776ad950f2111e0920198b700bf94 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Wed, 31 Aug 2022 17:26:14 -0700 Subject: [PATCH 10/14] Add internal test access to baseline transform in ServerCodegenVisitor. --- .../rust/codegen/server/smithy/ServerCodegenVisitor.kt | 8 +++++++- .../codegen/server/smithy/ServerCodegenVisitorTest.kt | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index d75ec836ae..1b28ee8599 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -101,7 +101,7 @@ open class ServerCodegenVisitor( * Base model transformation applied to all services. * See below for details. */ - internal fun baselineTransform(model: Model) = + protected fun baselineTransform(model: Model) = model // Flattens mixins out of the model and removes them from the model .let { ModelTransformer.create().flattenAndRemoveMixins(it) } @@ -116,6 +116,12 @@ open class ServerCodegenVisitor( // Normalize event stream operations .let(EventStreamNormalizer::transform) + /** + * Exposure purely for unit test purposes. + */ + internal fun baselineTransformInternalTest(model: Model) = + baselineTransform(model) + /** * Execute code generation * diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt index aaed2bbef2..55c2c2bd1d 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -47,7 +47,7 @@ class ServerCodegenVisitorTest { val codegenDecorator: CombinedCodegenDecorator = CombinedCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) val visitor = ServerCodegenVisitor(ctx, codegenDecorator) - val baselineModel = visitor.baselineTransform(model) + val baselineModel = visitor.baselineTransformInternalTest(model) baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true } } From 2102a3b85f7b4f195280a014cb418a34e8c9faa0 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Fri, 2 Sep 2022 11:50:20 -0700 Subject: [PATCH 11/14] Fix invalid subproject generated by added tests messing up other tests. --- .../codegen/server/smithy/ServerCodegenVisitorTest.kt | 7 ++++++- .../smithy/rust/codegen/smithy/CodegenVisitorTest.kt | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt index 55c2c2bd1d..15aae1d124 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -13,6 +13,9 @@ import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.testutil.generatePluginContext +import java.nio.file.Files.createDirectory +import java.nio.file.Files.write +import java.nio.file.StandardOpenOption class ServerCodegenVisitorTest { @Test @@ -43,7 +46,9 @@ class ServerCodegenVisitorTest { greeting: String } """.asSmithyModel(smithyVersion = "2.0") - val (ctx, _) = generatePluginContext(model) + val (ctx, testDir) = generatePluginContext(model) + createDirectory(testDir.resolve("src")) + write(testDir.resolve("src/main.rs"), mutableListOf("fn main() {}"), StandardOpenOption.CREATE_NEW) val codegenDecorator: CombinedCodegenDecorator = CombinedCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) val visitor = ServerCodegenVisitor(ctx, codegenDecorator) diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index 0922595118..30d7b6807b 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -15,6 +15,9 @@ import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizatio import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator import software.amazon.smithy.rust.codegen.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.testutil.generatePluginContext +import java.nio.file.Files.createDirectory +import java.nio.file.Files.write +import java.nio.file.StandardOpenOption class CodegenVisitorTest { @Test @@ -45,7 +48,10 @@ class CodegenVisitorTest { greeting: String } """.asSmithyModel(smithyVersion = "2.0") - val (ctx, _) = generatePluginContext(model) + val (ctx, testDir) = generatePluginContext(model) + createDirectory(testDir.resolve("src")) + write(testDir.resolve("src/main.rs"), mutableListOf("fn main() {}"), StandardOpenOption.CREATE_NEW) + val codegenDecorator = CombinedCodegenDecorator.fromClasspath( ctx, @@ -54,7 +60,7 @@ class CodegenVisitorTest { FluentClientDecorator(), NoOpEventStreamSigningDecorator(), ) - val visitor = CodegenVisitor(ctx, codegenDecorator) + val visitor = CodegenVisitor(ctx, codegenDecorator).apply { execute() } val baselineModel = visitor.baselineTransform(model) baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true } From 8cc121394b84bd0c76b20f7616c544558f45c6fd Mon Sep 17 00:00:00 2001 From: David Oguns Date: Mon, 5 Sep 2022 11:53:12 -0700 Subject: [PATCH 12/14] PR comment improvement. --- .../rust/codegen/server/smithy/ServerCodegenVisitorTest.kt | 6 ++++-- .../smithy/rust/codegen/smithy/CodegenVisitorTest.kt | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt index 15aae1d124..4ed8ae265e 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -16,6 +16,8 @@ import software.amazon.smithy.rust.codegen.testutil.generatePluginContext import java.nio.file.Files.createDirectory import java.nio.file.Files.write import java.nio.file.StandardOpenOption +import kotlin.io.path.createDirectory +import kotlin.io.path.writeText class ServerCodegenVisitorTest { @Test @@ -47,8 +49,8 @@ class ServerCodegenVisitorTest { } """.asSmithyModel(smithyVersion = "2.0") val (ctx, testDir) = generatePluginContext(model) - createDirectory(testDir.resolve("src")) - write(testDir.resolve("src/main.rs"), mutableListOf("fn main() {}"), StandardOpenOption.CREATE_NEW) + testDir.resolve("src").createDirectory() + testDir.resolve("src/main.rs").writeText("fn main() {}") val codegenDecorator: CombinedCodegenDecorator = CombinedCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) val visitor = ServerCodegenVisitor(ctx, codegenDecorator) diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index 30d7b6807b..25d079199b 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -18,6 +18,8 @@ import software.amazon.smithy.rust.codegen.testutil.generatePluginContext import java.nio.file.Files.createDirectory import java.nio.file.Files.write import java.nio.file.StandardOpenOption +import kotlin.io.path.createDirectory +import kotlin.io.path.writeText class CodegenVisitorTest { @Test @@ -49,9 +51,8 @@ class CodegenVisitorTest { } """.asSmithyModel(smithyVersion = "2.0") val (ctx, testDir) = generatePluginContext(model) - createDirectory(testDir.resolve("src")) - write(testDir.resolve("src/main.rs"), mutableListOf("fn main() {}"), StandardOpenOption.CREATE_NEW) - + testDir.resolve("src").createDirectory() + testDir.resolve("src/main.rs").writeText("fn main() {}") val codegenDecorator = CombinedCodegenDecorator.fromClasspath( ctx, From 9a60dcc8aa1f7483738ed80219bf0fd716d5c9f5 Mon Sep 17 00:00:00 2001 From: David Oguns Date: Tue, 6 Sep 2022 12:22:10 -0700 Subject: [PATCH 13/14] Removing unneeded part of fix. --- .../amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index 25d079199b..a137b7ea3c 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -61,7 +61,7 @@ class CodegenVisitorTest { FluentClientDecorator(), NoOpEventStreamSigningDecorator(), ) - val visitor = CodegenVisitor(ctx, codegenDecorator).apply { execute() } + val visitor = CodegenVisitor(ctx, codegenDecorator) val baselineModel = visitor.baselineTransform(model) baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true } From 8961211de8ab92e23b3807b87ac35999d3e3e7fd Mon Sep 17 00:00:00 2001 From: David Oguns Date: Tue, 6 Sep 2022 13:28:51 -0700 Subject: [PATCH 14/14] ktlint --- .../rust/codegen/server/smithy/ServerCodegenVisitorTest.kt | 3 --- .../amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt | 3 --- 2 files changed, 6 deletions(-) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt index 4ed8ae265e..ccfc11ab38 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitorTest.kt @@ -13,9 +13,6 @@ import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.testutil.generatePluginContext -import java.nio.file.Files.createDirectory -import java.nio.file.Files.write -import java.nio.file.StandardOpenOption import kotlin.io.path.createDirectory import kotlin.io.path.writeText diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt index a137b7ea3c..85cf6ee8a0 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitorTest.kt @@ -15,9 +15,6 @@ import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizatio import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientDecorator import software.amazon.smithy.rust.codegen.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.testutil.generatePluginContext -import java.nio.file.Files.createDirectory -import java.nio.file.Files.write -import java.nio.file.StandardOpenOption import kotlin.io.path.createDirectory import kotlin.io.path.writeText