From d74997d42271a2c62d65260e9847f5a7a678ad73 Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Tue, 13 Dec 2022 16:34:36 -0800 Subject: [PATCH 1/3] Break up `RustCodegenDecorator` This change creates `ClientCodegenDecorator` and `ServerCodegenDecorator` in `codegen-client` and `codegen-server` respectively to replace `RustCodegenDecorator`. Client/server equivalents are created to replace `CombinedCodegenDecorator` as well. This eliminates the need for the `supportsCodegenContext` method since the decorator interface is no longer generic, so the `ServiceLoader` is now powerful enough to differentiate. The largest benefit, however, is that now clients and servers can have separate customizations. --- CHANGELOG.next.toml | 20 +- aws/sdk-adhoc-test/build.gradle.kts | 2 +- .../smithy/rustsdk/AwsCodegenDecorator.kt | 9 +- .../smithy/rustsdk/AwsEndpointDecorator.kt | 8 +- .../smithy/rustsdk/AwsEndpointsStdLib.kt | 10 +- .../rustsdk/AwsFluentClientDecorator.kt | 8 +- .../smithy/rustsdk/AwsPresigningDecorator.kt | 8 +- .../smithy/rustsdk/AwsReadmeDecorator.kt | 9 +- .../smithy/rustsdk/CrateLicenseDecorator.kt | 10 +- .../smithy/rustsdk/CredentialProviders.kt | 9 +- .../HttpConnectorConfigCustomization.kt | 8 +- .../rustsdk/HttpRequestChecksumDecorator.kt | 9 +- .../rustsdk/HttpResponseChecksumDecorator.kt | 9 +- .../rustsdk/IntegrationTestDependencies.kt | 9 +- .../amazon/smithy/rustsdk/RegionDecorator.kt | 14 +- .../rustsdk/RetryClassifierDecorator.kt | 9 +- .../smithy/rustsdk/SdkConfigDecorator.kt | 9 +- .../smithy/rustsdk/ServiceConfigDecorator.kt | 9 +- .../smithy/rustsdk/SigV4SigningDecorator.kt | 8 +- .../smithy/rustsdk/UserAgentDecorator.kt | 9 +- .../apigateway/ApiGatewayDecorator.kt | 8 +- .../customize/auth/DisabledAuthDecorator.kt | 10 +- .../rustsdk/customize/ec2/Ec2Decorator.kt | 10 +- .../customize/glacier/GlacierDecorator.kt | 8 +- .../customize/route53/Route53Decorator.kt | 9 +- .../rustsdk/customize/s3/S3Decorator.kt | 7 +- .../rustsdk/customize/sts/STSDecorator.kt | 10 +- ...t.smithy.customize.ClientCodegenDecorator} | 0 aws/sdk/build.gradle.kts | 4 +- codegen-client-test/README.md | 2 +- codegen-client-test/build.gradle.kts | 2 +- .../client/smithy/ClientCodegenContext.kt | 9 +- ...egenVisitor.kt => ClientCodegenVisitor.kt} | 10 +- .../client/smithy/ClientRustSettings.kt | 6 +- ...enPlugin.kt => RustClientCodegenPlugin.kt} | 22 +- .../customizations/ClientCustomizations.kt | 9 +- .../customizations/DocsRsMetadataDecorator.kt | 12 +- .../customize/ClientCodegenDecorator.kt | 172 +++++++++++++++ .../NoOpEventStreamSigningDecorator.kt | 7 +- .../customize/RequiredCustomizations.kt | 8 +- .../smithy/customize/RustCodegenDecorator.kt | 207 ------------------ .../smithy/endpoint/EndpointsDecorator.kt | 9 +- .../smithy/generators/ServiceGenerator.kt | 4 +- .../client/FluentClientDecorator.kt | 8 +- .../client/testutil/CodegenIntegrationTest.kt | 32 ++- .../codegen/client/testutil/TestHelpers.kt | 4 +- ...ware.amazon.smithy.build.SmithyBuildPlugin | 2 +- .../HttpVersionListGeneratorTest.kt | 8 +- .../client/smithy/CodegenVisitorTest.kt | 8 +- .../protocol/ProtocolTestGeneratorTest.kt | 12 +- .../codegen/core/smithy/CodegenContext.kt | 2 +- .../codegen/core/smithy/CoreRustSettings.kt | 4 +- .../smithy/PythonCodegenServerPlugin.kt | 10 +- .../smithy/PythonServerCodegenVisitor.kt | 5 +- .../PythonServerCodegenDecorator.kt | 19 +- .../server/smithy/RustCodegenServerPlugin.kt | 11 +- .../server/smithy/ServerCodegenVisitor.kt | 4 +- .../AdditionalErrorsDecorator.kt | 15 +- .../ServerRequiredCustomizations.kt | 11 +- .../customize/ServerCodegenDecorator.kt | 132 +++++++++++ .../server/smithy/ServerCodegenVisitorTest.kt | 7 +- .../customize/CombinedCodegenDecoratorTest.kt | 40 ---- .../examples/Makefile | 2 +- .../aws-smithy-http-server/examples/Makefile | 2 +- 64 files changed, 478 insertions(+), 591 deletions(-) rename aws/sdk-codegen/src/main/resources/META-INF/services/{software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator => software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator} (100%) rename codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/{CodegenVisitor.kt => ClientCodegenVisitor.kt} (97%) rename codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/{RustCodegenPlugin.kt => RustClientCodegenPlugin.kt} (86%) create mode 100644 codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt delete mode 100644 codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RustCodegenDecorator.kt create mode 100644 codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt delete mode 100644 codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/CombinedCodegenDecoratorTest.kt diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index fc4c4c2578..6ca0411734 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -9,4 +9,22 @@ # message = "Fix typos in module documentation for generated crates" # references = ["smithy-rs#920"] # meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"} -# author = "rcoh" \ No newline at end of file +# author = "rcoh" + +[[smithy-rs]] +message = "The Rust client codegen plugin is now called `rust-client-codegen` instead of `rust-codegen`. Be sure to update your `smithy-build.json` files to refer to the correct plugin name." +references = ["smithy-rs#2099"] +meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client"} +author = "jdisanti" + +[[smithy-rs]] +message = "Client codegen plugins need to define a service named `software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator` (this is the new file name for the plugin definition in `resources/META-INF/services`)." +references = ["smithy-rs#2099"] +meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client"} +author = "jdisanti" + +[[smithy-rs]] +message = "Server codegen plugins need to define a service named `software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator` (this is the new file name for the plugin definition in `resources/META-INF/services`)." +references = ["smithy-rs#2099"] +meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "server"} +author = "jdisanti" diff --git a/aws/sdk-adhoc-test/build.gradle.kts b/aws/sdk-adhoc-test/build.gradle.kts index 5137acd20e..5868b76027 100644 --- a/aws/sdk-adhoc-test/build.gradle.kts +++ b/aws/sdk-adhoc-test/build.gradle.kts @@ -16,7 +16,7 @@ val smithyVersion: String by project val defaultRustDocFlags: String by project val properties = PropertyRetriever(rootProject, project) -val pluginName = "rust-codegen" +val pluginName = "rust-client-codegen" val workingDirUnderBuildDir = "smithyprojections/sdk-adhoc-test/" configure { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt index 8102ef579c..0f22a7a2ce 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt @@ -5,11 +5,10 @@ package software.amazon.smithy.rustsdk -import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.customizations.DocsRsMetadataDecorator import software.amazon.smithy.rust.codegen.client.smithy.customizations.DocsRsMetadataSettings -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator import software.amazon.smithy.rustsdk.customize.apigateway.ApiGatewayDecorator import software.amazon.smithy.rustsdk.customize.auth.DisabledAuthDecorator import software.amazon.smithy.rustsdk.customize.ec2.Ec2Decorator @@ -18,7 +17,7 @@ import software.amazon.smithy.rustsdk.customize.route53.Route53Decorator import software.amazon.smithy.rustsdk.customize.s3.S3Decorator import software.amazon.smithy.rustsdk.customize.sts.STSDecorator -val DECORATORS = listOf( +val DECORATORS: List = listOf( // General AWS Decorators CredentialsProviderDecorator(), RegionDecorator(), @@ -50,7 +49,7 @@ val DECORATORS = listOf( DocsRsMetadataDecorator(DocsRsMetadataSettings(targets = listOf("x86_64-unknown-linux-gnu"), allFeatures = true)), ) -class AwsCodegenDecorator : CombinedCodegenDecorator(DECORATORS) { +class AwsCodegenDecorator : CombinedClientCodegenDecorator(DECORATORS) { override val name: String = "AwsSdkCodegenDecorator" override val order: Byte = -1 } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointDecorator.kt index 94da0fb8d5..ecd7682617 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointDecorator.kt @@ -12,10 +12,9 @@ import software.amazon.smithy.model.node.ObjectNode import software.amazon.smithy.model.node.StringNode import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.core.rustlang.RustModule import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter @@ -39,7 +38,7 @@ import software.amazon.smithy.rust.codegen.core.util.expectTrait import software.amazon.smithy.rust.codegen.core.util.orNull import kotlin.io.path.readText -class AwsEndpointDecorator : RustCodegenDecorator { +class AwsEndpointDecorator : ClientCodegenDecorator { override val name: String = "AwsEndpoint" override val order: Byte = 0 @@ -83,9 +82,6 @@ class AwsEndpointDecorator : RustCodegenDecorator { return baseCustomizations + PubUseEndpoint(codegenContext.runtimeConfig) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class EndpointConfigCustomization( diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointsStdLib.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointsStdLib.kt index 3782117d8a..1c05afda42 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointsStdLib.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsEndpointsStdLib.kt @@ -8,12 +8,10 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.model.node.Node import software.amazon.smithy.model.node.ObjectNode import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.awsStandardLib -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import kotlin.io.path.readText /** @@ -23,15 +21,11 @@ import kotlin.io.path.readText * * For test purposes, [awsStandardLib] can be used directly with a manually supplied partitions.json */ -class AwsEndpointsStdLib() : RustCodegenDecorator { +class AwsEndpointsStdLib() : ClientCodegenDecorator { private var partitionsCache: ObjectNode? = null override val name: String = "AwsEndpointsStdLib" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean { - return clazz.isAssignableFrom(ClientCodegenContext::class.java) - } - private fun partitionMetadata(sdkSettings: SdkSettings): ObjectNode { if (partitionsCache == null) { val partitionsJson = when (val path = sdkSettings.partitionsConfigPath) { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.kt index 61a13c7650..c06b58994b 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.kt @@ -9,13 +9,12 @@ import software.amazon.smithy.codegen.core.Symbol import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.traits.TitleTrait import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.CustomizableOperationGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerics import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientSection -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Attribute import software.amazon.smithy.rust.codegen.core.rustlang.DependencyScope import software.amazon.smithy.rust.codegen.core.rustlang.Feature @@ -85,7 +84,7 @@ private class AwsClientGenerics(private val types: Types) : FluentClientGenerics override fun toRustGenerics() = RustGenerics() } -class AwsFluentClientDecorator : RustCodegenDecorator { +class AwsFluentClientDecorator : ClientCodegenDecorator { override val name: String = "FluentClient" // Must run after the AwsPresigningDecorator so that the presignable trait is correctly added to operations @@ -130,9 +129,6 @@ class AwsFluentClientDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } private class AwsFluentClientExtensions(types: Types) { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt index d2fcf095b4..3a9002e5e8 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt @@ -17,10 +17,9 @@ import software.amazon.smithy.model.traits.HttpQueryTrait import software.amazon.smithy.model.traits.HttpTrait import software.amazon.smithy.model.transform.ModelTransformer import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientSection -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.docs @@ -85,7 +84,7 @@ internal val PRESIGNABLE_OPERATIONS by lazy { class AwsPresigningDecorator internal constructor( private val presignableOperations: Map = PRESIGNABLE_OPERATIONS, -) : RustCodegenDecorator { +) : ClientCodegenDecorator { companion object { const val ORDER: Byte = 0 } @@ -117,9 +116,6 @@ class AwsPresigningDecorator internal constructor( return presignableTransforms.fold(intermediate) { m, t -> t.transform(m) } } - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - private fun addSyntheticOperations(model: Model): Model { val presignableOps = model.shapes() .filter { shape -> shape is OperationShape && presignableOperations.containsKey(shape.id) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsReadmeDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsReadmeDecorator.kt index 15767c0c1c..d8868bb94c 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsReadmeDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsReadmeDecorator.kt @@ -10,10 +10,8 @@ import org.jsoup.nodes.Element import org.jsoup.nodes.TextNode import software.amazon.smithy.model.traits.DocumentationTrait import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.raw -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations import software.amazon.smithy.rust.codegen.core.util.getTrait @@ -27,13 +25,10 @@ private const val SPACE_SIGIL = "[[smithy-rs-nbsp]]" /** * Generates a README.md for each service crate for display on crates.io. */ -class AwsReadmeDecorator : RustCodegenDecorator { +class AwsReadmeDecorator : ClientCodegenDecorator { override val name: String = "AwsReadmeDecorator" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - override fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations = if (generateReadme(codegenContext)) { mapOf("package" to mapOf("readme" to "README.md")) diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CrateLicenseDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CrateLicenseDecorator.kt index b1847809ed..22ebd0ff1e 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CrateLicenseDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CrateLicenseDecorator.kt @@ -6,15 +6,12 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.raw -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate -class CrateLicenseDecorator : RustCodegenDecorator { +class CrateLicenseDecorator : ClientCodegenDecorator { override val name: String = "CrateLicense" - override val order: Byte = 0 override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { @@ -23,7 +20,4 @@ class CrateLicenseDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt index 8b489923f1..eae73bd5d2 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt @@ -7,15 +7,13 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization @@ -23,7 +21,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSectio import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection -class CredentialsProviderDecorator : RustCodegenDecorator { +class CredentialsProviderDecorator : ClientCodegenDecorator { override val name: String = "CredentialsProvider" override val order: Byte = 0 @@ -48,9 +46,6 @@ class CredentialsProviderDecorator : RustCodegenDecorator { return baseCustomizations + PubUseCredentials(codegenContext.runtimeConfig) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } /** diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpConnectorConfigCustomization.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpConnectorConfigCustomization.kt index 215662aeca..fcf3cc0c2b 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpConnectorConfigCustomization.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpConnectorConfigCustomization.kt @@ -6,10 +6,9 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate @@ -17,13 +16,10 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType -class HttpConnectorDecorator : RustCodegenDecorator { +class HttpConnectorDecorator : ClientCodegenDecorator { override val name: String = "HttpConnectorDecorator" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - override fun configCustomizations( codegenContext: ClientCodegenContext, baseCustomizations: List, diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt index a94531f43c..0a5a4cfd02 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt @@ -8,14 +8,12 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.aws.traits.HttpChecksumTrait import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.core.rustlang.Visibility import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization @@ -39,7 +37,7 @@ fun RuntimeConfig.awsInlineableBodyWithChecksum() = RuntimeType.forInlineDepende ), ) -class HttpRequestChecksumDecorator : RustCodegenDecorator { +class HttpRequestChecksumDecorator : ClientCodegenDecorator { override val name: String = "HttpRequestChecksum" override val order: Byte = 0 @@ -50,9 +48,6 @@ class HttpRequestChecksumDecorator : RustCodegenDecorator { return baseCustomizations + HttpRequestChecksumCustomization(codegenContext, operation) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } private fun HttpChecksumTrait.requestAlgorithmMember( diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpResponseChecksumDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpResponseChecksumDecorator.kt index fbbb878fbc..f1a907a316 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpResponseChecksumDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpResponseChecksumDecorator.kt @@ -9,11 +9,9 @@ import software.amazon.smithy.aws.traits.HttpChecksumTrait import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection import software.amazon.smithy.rust.codegen.core.util.expectMember @@ -29,7 +27,7 @@ private fun HttpChecksumTrait.requestValidationModeMember( return operationShape.inputShape(codegenContext.model).expectMember(requestValidationModeMember) } -class HttpResponseChecksumDecorator : RustCodegenDecorator { +class HttpResponseChecksumDecorator : ClientCodegenDecorator { override val name: String = "HttpResponseChecksum" override val order: Byte = 0 @@ -40,9 +38,6 @@ class HttpResponseChecksumDecorator : RustCodegenDecorator { return baseCustomizations + HttpResponseChecksumCustomization(codegenContext, operation) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } // This generator was implemented based on this spec: diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/IntegrationTestDependencies.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/IntegrationTestDependencies.kt index 42a55481b1..0eafec8fcd 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/IntegrationTestDependencies.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/IntegrationTestDependencies.kt @@ -6,8 +6,7 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.AsyncStd import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Companion.AsyncStream @@ -28,7 +27,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency.Compani import software.amazon.smithy.rust.codegen.core.rustlang.DependencyScope import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection @@ -36,7 +34,7 @@ import java.nio.file.Files import java.nio.file.Paths import kotlin.io.path.absolute -class IntegrationTestDecorator : RustCodegenDecorator { +class IntegrationTestDecorator : ClientCodegenDecorator { override val name: String = "IntegrationTest" override val order: Byte = 0 @@ -65,9 +63,6 @@ class IntegrationTestDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class IntegrationTestDependencies( diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt index 24cf36aa0b..7ed847f883 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt @@ -5,19 +5,16 @@ package software.amazon.smithy.rustsdk -import software.amazon.smithy.model.Model import software.amazon.smithy.model.node.Node import software.amazon.smithy.model.shapes.OperationShape -import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rulesengine.language.syntax.parameters.Builtins import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate @@ -78,14 +75,10 @@ fn test_1() { } */ -class RegionDecorator : RustCodegenDecorator { +class RegionDecorator : ClientCodegenDecorator { override val name: String = "Region" override val order: Byte = 0 - override fun transformModel(service: ServiceShape, model: Model): Model { - return super.transformModel(service, model) - } - override fun configCustomizations( codegenContext: ClientCodegenContext, baseCustomizations: List, @@ -136,9 +129,6 @@ class RegionDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class RegionProviderConfig(codegenContext: CodegenContext) : ConfigCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryClassifierDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryClassifierDecorator.kt index cbf4ca57e9..0d488fb21f 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryClassifierDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryClassifierDecorator.kt @@ -7,17 +7,15 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection -class RetryClassifierDecorator : RustCodegenDecorator { +class RetryClassifierDecorator : ClientCodegenDecorator { override val name: String = "RetryPolicy" override val order: Byte = 0 @@ -28,9 +26,6 @@ class RetryClassifierDecorator : RustCodegenDecorator { return baseCustomizations + RetryClassifierFeature(codegenContext.runtimeConfig) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class RetryClassifierFeature(private val runtimeConfig: RuntimeConfig) : OperationCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt index 2ae5e8d614..66b0e48570 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt @@ -6,15 +6,13 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.RustModule import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RustCrate @@ -24,7 +22,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.RustCrate * - `From<&aws_types::SdkConfig> for ::config::Builder`: Enabling customization * - `pub fn new(&aws_types::SdkConfig) -> ::Config`: Direct construction without customization */ -class SdkConfigDecorator : RustCodegenDecorator { +class SdkConfigDecorator : ClientCodegenDecorator { override val name: String = "SdkConfig" override val order: Byte = 0 @@ -68,9 +66,6 @@ class SdkConfigDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class NewFromShared(runtimeConfig: RuntimeConfig) : ConfigCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/ServiceConfigDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/ServiceConfigDecorator.kt index e8729060e8..a364b1743a 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/ServiceConfigDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/ServiceConfigDecorator.kt @@ -6,16 +6,14 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.docs import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext -class ServiceConfigDecorator : RustCodegenDecorator { +class ServiceConfigDecorator : ClientCodegenDecorator { override val name: String = "ServiceConfigGenerator" override val order: Byte = 0 @@ -23,9 +21,6 @@ class ServiceConfigDecorator : RustCodegenDecorator, ): List = baseCustomizations + SharedConfigDocsCustomization() - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class SharedConfigDocsCustomization : ConfigCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SigV4SigningDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SigV4SigningDecorator.kt index fa5cd8eaa5..22e7e2065d 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SigV4SigningDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SigV4SigningDecorator.kt @@ -14,10 +14,9 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.traits.OptionalAuthTrait import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.EventStreamSigningConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate @@ -41,7 +40,7 @@ import software.amazon.smithy.rust.codegen.core.util.letIf * - sets a default `OperationSigningConfig` A future enhancement will customize this for specific services that need * different behavior. */ -class SigV4SigningDecorator : RustCodegenDecorator { +class SigV4SigningDecorator : ClientCodegenDecorator { override val name: String = "SigV4Signing" override val order: Byte = 0 @@ -74,9 +73,6 @@ class SigV4SigningDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class SigV4SigningConfig( diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt index 5f8c36e7fe..e4e4f579b8 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt @@ -8,15 +8,13 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.aws.traits.ServiceTrait import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection @@ -28,7 +26,7 @@ import software.amazon.smithy.rust.codegen.core.util.expectTrait /** * Inserts a UserAgent configuration into the operation */ -class UserAgentDecorator : RustCodegenDecorator { +class UserAgentDecorator : ClientCodegenDecorator { override val name: String = "UserAgent" override val order: Byte = 10 @@ -55,9 +53,6 @@ class UserAgentDecorator : RustCodegenDecorator { return baseCustomizations + UserAgentFeature(codegenContext.runtimeConfig) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } /** diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/apigateway/ApiGatewayDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/apigateway/ApiGatewayDecorator.kt index 70af3fe195..9fc0f3e4c7 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/apigateway/ApiGatewayDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/apigateway/ApiGatewayDecorator.kt @@ -8,8 +8,7 @@ package software.amazon.smithy.rustsdk.customize.apigateway import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.writable @@ -19,7 +18,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustom import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection import software.amazon.smithy.rust.codegen.core.util.letIf -class ApiGatewayDecorator : RustCodegenDecorator { +class ApiGatewayDecorator : ClientCodegenDecorator { override val name: String = "ApiGateway" override val order: Byte = 0 @@ -35,9 +34,6 @@ class ApiGatewayDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } class ApiGatewayAddAcceptHeader : OperationCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/auth/DisabledAuthDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/auth/DisabledAuthDecorator.kt index 57acccd6a5..2c65f95bd3 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/auth/DisabledAuthDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/auth/DisabledAuthDecorator.kt @@ -11,15 +11,12 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.traits.AuthTrait import software.amazon.smithy.model.transform.ModelTransformer -import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator private fun String.shapeId() = ShapeId.from(this) // / STS (and possibly other services) need to have auth manually set to [] -class DisabledAuthDecorator : RustCodegenDecorator { +class DisabledAuthDecorator : ClientCodegenDecorator { override val name: String = "OptionalAuth" override val order: Byte = 0 @@ -48,7 +45,4 @@ class DisabledAuthDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/ec2/Ec2Decorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/ec2/Ec2Decorator.kt index 630ac0af02..ee005da318 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/ec2/Ec2Decorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/ec2/Ec2Decorator.kt @@ -8,13 +8,10 @@ package software.amazon.smithy.rustsdk.customize.ec2 import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.util.letIf -class Ec2Decorator : RustCodegenDecorator { +class Ec2Decorator : ClientCodegenDecorator { override val name: String = "Ec2" override val order: Byte = 0 private val ec2 = ShapeId.from("com.amazonaws.ec2#AmazonEC2") @@ -30,7 +27,4 @@ class Ec2Decorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/glacier/GlacierDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/glacier/GlacierDecorator.kt index 166ca7e3b1..7bfc3c4e42 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/glacier/GlacierDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/glacier/GlacierDecorator.kt @@ -8,14 +8,13 @@ package software.amazon.smithy.rustsdk.customize.glacier import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization val Glacier: ShapeId = ShapeId.from("com.amazonaws.glacier#Glacier") -class GlacierDecorator : RustCodegenDecorator { +class GlacierDecorator : ClientCodegenDecorator { override val name: String = "Glacier" override val order: Byte = 0 @@ -38,7 +37,4 @@ class GlacierDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/route53/Route53Decorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/route53/Route53Decorator.kt index d58f734561..e1adcf46af 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/route53/Route53Decorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/route53/Route53Decorator.kt @@ -13,12 +13,10 @@ import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.traits.HttpLabelTrait import software.amazon.smithy.model.transform.ModelTransformer import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection @@ -30,7 +28,7 @@ import java.util.logging.Logger val Route53: ShapeId = ShapeId.from("com.amazonaws.route53#AWSDnsV20130401") -class Route53Decorator : RustCodegenDecorator { +class Route53Decorator : ClientCodegenDecorator { override val name: String = "Route53" override val order: Byte = 0 private val logger: Logger = Logger.getLogger(javaClass.name) @@ -61,9 +59,6 @@ class Route53Decorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - private fun isResourceId(shape: Shape): Boolean { return (shape is MemberShape && resourceShapes.contains(shape.target)) && shape.hasTrait() } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/s3/S3Decorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/s3/S3Decorator.kt index c484ee88b8..d59d1e2df1 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/s3/S3Decorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/s3/S3Decorator.kt @@ -14,7 +14,7 @@ import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.transform.ModelTransformer import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.ClientRestXmlFactory import software.amazon.smithy.rust.codegen.core.rustlang.RustModule @@ -37,7 +37,7 @@ import java.util.logging.Logger /** * Top level decorator for S3 */ -class S3Decorator : RustCodegenDecorator { +class S3Decorator : ClientCodegenDecorator { override val name: String = "S3" override val order: Byte = 0 private val logger: Logger = Logger.getLogger(javaClass.name) @@ -79,9 +79,6 @@ class S3Decorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - private fun isInInvalidXmlRootAllowList(shape: Shape): Boolean { return shape.isStructureShape && invalidXmlRootAllowList.contains(shape.id) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/sts/STSDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/sts/STSDecorator.kt index 874fd3979e..75d0555c8f 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/sts/STSDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/sts/STSDecorator.kt @@ -12,15 +12,12 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.traits.ErrorTrait import software.amazon.smithy.model.traits.RetryableTrait import software.amazon.smithy.model.transform.ModelTransformer -import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.util.hasTrait import software.amazon.smithy.rust.codegen.core.util.letIf import java.util.logging.Logger -class STSDecorator : RustCodegenDecorator { +class STSDecorator : ClientCodegenDecorator { override val name: String = "STS" override val order: Byte = 0 private val logger: Logger = Logger.getLogger(javaClass.name) @@ -45,7 +42,4 @@ class STSDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator b/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator similarity index 100% rename from aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator rename to aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator diff --git a/aws/sdk/build.gradle.kts b/aws/sdk/build.gradle.kts index cdeab5287c..a894601809 100644 --- a/aws/sdk/build.gradle.kts +++ b/aws/sdk/build.gradle.kts @@ -88,7 +88,7 @@ fun generateSmithyBuild(services: AwsServices): String { "imports": [${files.joinToString()}], "plugins": { - "rust-codegen": { + "rust-client-codegen": { "runtimeConfig": { "relativePath": "../", "version": "DEFAULT" @@ -158,7 +158,7 @@ tasks.register("relocateServices") { awsServices.services.forEach { logger.info("Relocating ${it.module}...") copy { - from("$buildDir/smithyprojections/sdk/${it.module}/rust-codegen") + from("$buildDir/smithyprojections/sdk/${it.module}/rust-client-codegen") into(sdkOutputDir.resolve(it.module)) } diff --git a/codegen-client-test/README.md b/codegen-client-test/README.md index 5371b664ec..bde4b403e8 100644 --- a/codegen-client-test/README.md +++ b/codegen-client-test/README.md @@ -2,7 +2,7 @@ This module defines integration tests of the code generation machinery. `./build.gradle.kts` will generate a `smithy-build.json` file as part of the -build. The `rust-codegen` Smithy build plugin then invokes our codegen +build. The `rust-client-codegen` Smithy build plugin then invokes our codegen machinery and generates Rust crates, one for each of the integration test services defined under `model/`. diff --git a/codegen-client-test/build.gradle.kts b/codegen-client-test/build.gradle.kts index b142c71879..f961f08b47 100644 --- a/codegen-client-test/build.gradle.kts +++ b/codegen-client-test/build.gradle.kts @@ -16,7 +16,7 @@ val smithyVersion: String by project val defaultRustDocFlags: String by project val properties = PropertyRetriever(rootProject, project) -val pluginName = "rust-codegen" +val pluginName = "rust-client-codegen" val workingDirUnderBuildDir = "smithyprojections/codegen-client-test/" buildscript { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenContext.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenContext.kt index 6f58a557d5..38567aa0cd 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenContext.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenContext.kt @@ -8,15 +8,14 @@ package software.amazon.smithy.rust.codegen.client.smithy import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider /** - * [ClientCodegenContext] contains code-generation context that is _specific_ to the [RustCodegenPlugin] plugin - * from the `rust-codegen` subproject. + * [ClientCodegenContext] contains code-generation context that is _specific_ to the [RustClientCodegenPlugin] plugin + * from the `codegen-client` subproject. * * It inherits from [CodegenContext], which contains code-generation context that is common to _all_ smithy-rs plugins. */ @@ -28,7 +27,7 @@ data class ClientCodegenContext( override val settings: ClientRustSettings, // Expose the `rootDecorator`, enabling customizations to compose by referencing information from the root codegen // decorator - val rootDecorator: RustCodegenDecorator, + val rootDecorator: ClientCodegenDecorator, ) : CodegenContext( model, symbolProvider, serviceShape, protocol, settings, CodegenTarget.CLIENT, ) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitor.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenVisitor.kt similarity index 97% rename from codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitor.kt rename to codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenVisitor.kt index 42caec342c..3cab46a451 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitor.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientCodegenVisitor.kt @@ -17,7 +17,7 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.model.transform.ModelTransformer -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.ClientProtocolLoader @@ -44,11 +44,11 @@ import software.amazon.smithy.rust.codegen.core.util.runCommand import java.util.logging.Logger /** - * Base Entrypoint for Code generation + * Entry point for client code generation */ -class CodegenVisitor( +class ClientCodegenVisitor( context: PluginContext, - private val codegenDecorator: RustCodegenDecorator, + private val codegenDecorator: ClientCodegenDecorator, ) : ShapeVisitor.Default() { private val logger = Logger.getLogger(javaClass.name) @@ -76,7 +76,7 @@ class CodegenVisitor( ).protocolFor(context.model, service) protocolGeneratorFactory = generator model = codegenDecorator.transformModel(service, baseModel) - symbolProvider = RustCodegenPlugin.baseSymbolProvider(model, service, symbolVisitorConfig) + symbolProvider = RustClientCodegenPlugin.baseSymbolProvider(model, service, symbolVisitorConfig) codegenContext = ClientCodegenContext(model, symbolProvider, service, protocol, settings, codegenDecorator) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientRustSettings.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientRustSettings.kt index 6450c9a71f..5fb6eb1d1b 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientRustSettings.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/ClientRustSettings.kt @@ -18,12 +18,12 @@ import java.util.Optional /** * [ClientRustSettings] and [ClientCodegenConfig] classes. * - * These are specializations of [RustSettings] and [CodegenConfig] for the `rust-codegen` client Smithy plugin. Refer - * to the documentation of those for the inherited properties. + * These are specializations of [CoreRustSettings] and [CodegenConfig] for the `rust-client-codegen` + * client Smithy plugin. Refer to the documentation of those for the inherited properties. */ /** - * Settings used by [RustCodegenPlugin]. + * Settings used by [RustClientCodegenPlugin]. */ data class ClientRustSettings( override val service: ShapeId, diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustCodegenPlugin.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt similarity index 86% rename from codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustCodegenPlugin.kt rename to codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt index 6b31380578..005eaa8bf6 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustCodegenPlugin.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt @@ -10,12 +10,11 @@ import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rust.codegen.client.smithy.customizations.ClientCustomizations -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStreamSigningDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.client.testutil.DecoratableBuildPlugin import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.NonExhaustive import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider @@ -27,17 +26,18 @@ import java.util.logging.Level import java.util.logging.Logger /** - * Rust Codegen Plugin + * Rust Client Codegen Plugin * * This is the entrypoint for code generation, triggered by the smithy-build plugin. * `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which - * enables the smithy-build plugin to invoke `execute` with all of the Smithy plugin context + models. + * enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models. */ -class RustCodegenPlugin : DecoratableBuildPlugin() { - override fun getName(): String = "rust-codegen" +class RustClientCodegenPlugin : DecoratableBuildPlugin() { + override fun getName(): String = "rust-client-codegen" + override fun executeWithDecorator( context: PluginContext, - vararg decorator: RustCodegenDecorator, + vararg decorator: ClientCodegenDecorator, ) { // Suppress extremely noisy logs about reserved words Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF @@ -47,7 +47,7 @@ class RustCodegenPlugin : DecoratableBuildPlugin { +class ClientCustomizations : ClientCodegenDecorator { override val name: String = "ClientCustomizations" override val order: Byte = 0 @@ -22,7 +20,4 @@ class ClientCustomizations : RustCodegenDecorator, ): List = baseCustomizations + ClientDocsGenerator() - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customizations/DocsRsMetadataDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customizations/DocsRsMetadataDecorator.kt index 7809ccffbd..442cedcf23 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customizations/DocsRsMetadataDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customizations/DocsRsMetadataDecorator.kt @@ -6,9 +6,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.customizations import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations /** @@ -50,15 +48,11 @@ fun DocsRsMetadataSettings.asMap(): Map { * # Notes * This decorator is not used by default, code generators must manually configure and include it in their builds. */ -class DocsRsMetadataDecorator(private val docsRsMetadataSettings: DocsRsMetadataSettings) : - RustCodegenDecorator { - override val name: String = "docsrs-metadata" +class DocsRsMetadataDecorator(private val docsRsMetadataSettings: DocsRsMetadataSettings) : ClientCodegenDecorator { + override val name: String = "DocsRsMetadataDecorator" override val order: Byte = 0 override fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations { return docsRsMetadataSettings.asMap() } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt new file mode 100644 index 0000000000..dae7af0ac1 --- /dev/null +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt @@ -0,0 +1,172 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.client.smithy.customize + +import software.amazon.smithy.build.PluginContext +import software.amazon.smithy.model.Model +import software.amazon.smithy.model.shapes.OperationShape +import software.amazon.smithy.model.shapes.ServiceShape +import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization +import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization +import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.core.smithy.RustCrate +import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization +import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization +import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations +import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolMap +import software.amazon.smithy.rust.codegen.core.util.deepMergeWith +import java.util.ServiceLoader +import java.util.logging.Logger + +typealias ClientProtocolMap = ProtocolMap + +/** + * [ClientCodegenDecorator] allows downstream users to customize code generation. + * + * For example, AWS-specific code generation generates customizations required to support + * AWS services. A different downstream customer may wish to add a different set of derive + * attributes to the generated classes. + */ +interface ClientCodegenDecorator { + /** + * The name of this [ClientCodegenDecorator], used for logging and debug information + */ + val name: String + + /** + * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first + */ + val order: Byte + + /** + * Whether this decorator can be discovered on the classpath (defaults to true). + * This is intended to only be overridden for decorators written specifically for tests. + */ + fun classpathDiscoverable(): Boolean = true + + fun configCustomizations( + codegenContext: ClientCodegenContext, + baseCustomizations: List, + ): List = baseCustomizations + + fun operationCustomizations( + codegenContext: ClientCodegenContext, + operation: OperationShape, + baseCustomizations: List, + ): List = baseCustomizations + + fun libRsCustomizations( + codegenContext: ClientCodegenContext, + baseCustomizations: List, + ): List = baseCustomizations + + /** + * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be + * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` + * could be returned. Properties here overwrite the default properties. + */ + fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations = emptyMap() + + fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {} + + fun protocols(serviceId: ShapeId, currentProtocols: ClientProtocolMap): ClientProtocolMap = currentProtocols + + fun transformModel(service: ServiceShape, model: Model): Model = model + + fun endpointCustomizations(codegenContext: ClientCodegenContext): List = listOf() +} + +/** + * [CombinedClientCodegenDecorator] merges the results of multiple decorators into a single decorator. + * + * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. + */ +open class CombinedClientCodegenDecorator(decorators: List) : ClientCodegenDecorator { + private val orderedDecorators = decorators.sortedBy { it.order } + override val name: String + get() = "CombinedClientCodegenDecorator" + override val order: Byte + get() = 0 + + override fun configCustomizations( + codegenContext: ClientCodegenContext, + baseCustomizations: List, + ): List { + return orderedDecorators.foldRight(baseCustomizations) { decorator: ClientCodegenDecorator, customizations -> + decorator.configCustomizations(codegenContext, customizations) + } + } + + override fun operationCustomizations( + codegenContext: ClientCodegenContext, + operation: OperationShape, + baseCustomizations: List, + ): List { + return orderedDecorators.foldRight(baseCustomizations) { decorator: ClientCodegenDecorator, customizations -> + decorator.operationCustomizations(codegenContext, operation, customizations) + } + } + + override fun libRsCustomizations( + codegenContext: ClientCodegenContext, + baseCustomizations: List, + ): List { + return orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> + decorator.libRsCustomizations( + codegenContext, + customizations, + ) + } + } + + override fun protocols(serviceId: ShapeId, currentProtocols: ClientProtocolMap): ClientProtocolMap { + return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> + decorator.protocols(serviceId, protocolMap) + } + } + + override fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations { + return orderedDecorators.foldRight(emptyMap()) { decorator, customizations -> + customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) + } + } + + override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { + return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } + } + + override fun transformModel(service: ServiceShape, model: Model): Model { + return orderedDecorators.foldRight(model) { decorator, otherModel -> + decorator.transformModel(service, otherModel) + } + } + + override fun endpointCustomizations(codegenContext: ClientCodegenContext): List { + return orderedDecorators.flatMap { it.endpointCustomizations(codegenContext) } + } + + companion object { + fun fromClasspath( + context: PluginContext, + vararg extras: ClientCodegenDecorator, + logger: Logger = Logger.getLogger("RustClientCodegenSPILoader"), + ): CombinedClientCodegenDecorator { + val decorators = ServiceLoader.load( + ClientCodegenDecorator::class.java, + context.pluginClassLoader.orElse(ClientCodegenDecorator::class.java.classLoader), + ) + + val filteredDecorators = decorators.asSequence() + .onEach { logger.info("Discovered Codegen Decorator: ${it.javaClass.name}") } + .filter { it.classpathDiscoverable() } + .onEach { logger.info("Adding Codegen Decorator: ${it.javaClass.name}") } + .toList() + return CombinedClientCodegenDecorator(filteredDecorators + extras) + } + } +} diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/NoOpEventStreamSigningDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/NoOpEventStreamSigningDecorator.kt index cddfc1cc10..7d924ee75a 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/NoOpEventStreamSigningDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/NoOpEventStreamSigningDecorator.kt @@ -5,6 +5,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.customize +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.EventStreamSigningConfig import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate @@ -18,7 +19,7 @@ import software.amazon.smithy.rust.codegen.core.util.hasEventStreamOperations * The NoOpEventStreamSigningDecorator: * - adds a `new_event_stream_signer()` method to `config` to create an Event Stream NoOp signer */ -open class NoOpEventStreamSigningDecorator : RustCodegenDecorator { +open class NoOpEventStreamSigningDecorator : ClientCodegenDecorator { override val name: String = "NoOpEventStreamSigning" override val order: Byte = Byte.MIN_VALUE @@ -28,7 +29,7 @@ open class NoOpEventStreamSigningDecorator : RustCodegenD !baseCustomizations.any { it is EventStreamSigningConfig } override fun configCustomizations( - codegenContext: C, + codegenContext: ClientCodegenContext, baseCustomizations: List, ): List { if (!applies(codegenContext, baseCustomizations)) { @@ -39,8 +40,6 @@ open class NoOpEventStreamSigningDecorator : RustCodegenD codegenContext.runtimeConfig, ) } - - override fun supportsCodegenContext(clazz: Class) = true } class NoOpEventStreamSigningConfig( diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RequiredCustomizations.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RequiredCustomizations.kt index 0567f3a13a..926259e42e 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RequiredCustomizations.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RequiredCustomizations.kt @@ -17,10 +17,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.Resilien import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyReExportCustomization import software.amazon.smithy.rust.codegen.client.smithy.customizations.pubUseSmithyTypes import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Feature -import software.amazon.smithy.rust.codegen.core.rustlang.rust -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization @@ -30,7 +27,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomiza * * This exists as a convenient place to gather these modifications, these are not true customizations. */ -class RequiredCustomizations : RustCodegenDecorator { +class RequiredCustomizations : ClientCodegenDecorator { override val name: String = "Required" override val order: Byte = -1 @@ -67,7 +64,4 @@ class RequiredCustomizations : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RustCodegenDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RustCodegenDecorator.kt deleted file mode 100644 index 14e81ee962..0000000000 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RustCodegenDecorator.kt +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.rust.codegen.client.smithy.customize - -import software.amazon.smithy.build.PluginContext -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.shapes.OperationShape -import software.amazon.smithy.model.shapes.ServiceShape -import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization -import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext -import software.amazon.smithy.rust.codegen.core.smithy.RustCrate -import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization -import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization -import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations -import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolMap -import software.amazon.smithy.rust.codegen.core.util.deepMergeWith -import java.util.ServiceLoader -import java.util.logging.Logger - -/** - * [RustCodegenDecorator] allows downstream users to customize code generation. - * - * For example, AWS-specific code generation generates customizations required to support - * AWS services. A different downstream customer may wish to add a different set of derive - * attributes to the generated classes. - */ -interface RustCodegenDecorator { - /** - * The name of this [RustCodegenDecorator], used for logging and debug information - */ - val name: String - - /** - * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first - */ - val order: Byte - - fun configCustomizations( - codegenContext: C, - baseCustomizations: List, - ): List = baseCustomizations - - // This is only used by decorators for smithy-rs _clients_. - fun operationCustomizations( - codegenContext: C, - operation: OperationShape, - baseCustomizations: List, - ): List = baseCustomizations - - fun libRsCustomizations( - codegenContext: C, - baseCustomizations: List, - ): List = baseCustomizations - - /** - * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be - * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` - * could be returned. Properties here overwrite the default properties. - */ - fun crateManifestCustomizations(codegenContext: C): ManifestCustomizations = emptyMap() - - fun extras(codegenContext: C, rustCrate: RustCrate) {} - - fun protocols(serviceId: ShapeId, currentProtocols: ProtocolMap): ProtocolMap = currentProtocols - - fun transformModel(service: ServiceShape, model: Model): Model = model - - fun endpointCustomizations(codegenContext: C): List = listOf() - - fun supportsCodegenContext(clazz: Class): Boolean -} - -/** - * [CombinedCodegenDecorator] merges the results of multiple decorators into a single decorator. - * - * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. - */ -open class CombinedCodegenDecorator(decorators: List>) : - RustCodegenDecorator { - private val orderedDecorators = decorators.sortedBy { it.order } - override val name: String - get() = "MetaDecorator" - override val order: Byte - get() = 0 - - fun withDecorator(vararg decorator: RustCodegenDecorator) = - CombinedCodegenDecorator(orderedDecorators + decorator) - - override fun configCustomizations( - codegenContext: C, - baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator: RustCodegenDecorator, customizations -> - decorator.configCustomizations(codegenContext, customizations) - } - } - - override fun operationCustomizations( - codegenContext: C, - operation: OperationShape, - baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator: RustCodegenDecorator, customizations -> - decorator.operationCustomizations(codegenContext, operation, customizations) - } - } - - override fun libRsCustomizations( - codegenContext: C, - baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> - decorator.libRsCustomizations( - codegenContext, - customizations, - ) - } - } - - override fun protocols(serviceId: ShapeId, currentProtocols: ProtocolMap): ProtocolMap { - return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> - decorator.protocols(serviceId, protocolMap) - } - } - - override fun crateManifestCustomizations(codegenContext: C): ManifestCustomizations { - return orderedDecorators.foldRight(emptyMap()) { decorator, customizations -> - customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) - } - } - - override fun extras(codegenContext: C, rustCrate: RustCrate) { - return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } - } - - override fun transformModel(service: ServiceShape, model: Model): Model { - return orderedDecorators.foldRight(model) { decorator, otherModel -> - decorator.transformModel(service, otherModel) - } - } - - override fun endpointCustomizations(codegenContext: C): List { - return orderedDecorators.flatMap { it.endpointCustomizations(codegenContext) } - } - - override fun supportsCodegenContext(clazz: Class): Boolean = - // `CombinedCodegenDecorator` can work with all types of codegen context. - CodegenContext::class.java.isAssignableFrom(clazz) - - companion object { - inline fun fromClasspath( - context: PluginContext, - vararg extras: RustCodegenDecorator, - logger: Logger = Logger.getLogger("RustCodegenSPILoader"), - ): CombinedCodegenDecorator { - val decorators = ServiceLoader.load( - RustCodegenDecorator::class.java, - context.pluginClassLoader.orElse(RustCodegenDecorator::class.java.classLoader), - ) - - val filteredDecorators = filterDecorators(decorators, logger).toList() - return CombinedCodegenDecorator(filteredDecorators + extras) - } - - /* - * This function has been extracted solely for the purposes of easily unit testing the important filtering logic. - * Unfortunately, it must be part of the public API because public API inline functions are not allowed to use - * non-public-API declarations. - * See https://kotlinlang.org/docs/inline-functions.html#restrictions-for-public-api-inline-functions. - */ - inline fun filterDecorators( - decorators: Iterable>, - logger: Logger = Logger.getLogger("RustCodegenSPILoader"), - ): Sequence> = - decorators.asSequence() - .onEach { - logger.info("Discovered Codegen Decorator: ${it.javaClass.name}") - } - // The JVM's `ServiceLoader` is woefully underpowered in that it can not load classes with generic - // parameters with _fixed_ parameters (like what we're trying to do here; we only want `RustCodegenDecorator` - // classes with code-generation context matching the input `T`). - // There are various workarounds: https://stackoverflow.com/questions/5451734/loading-generic-service-implementations-via-java-util-serviceloader - // All involve loading _all_ classes from the classpath (i.e. all `RustCodegenDecorator<*>`), and then - // filtering them. - // Note that attempting to downcast a generic class `C` to `C` where `U: T` is not possible to do - // in Kotlin (and presumably all JVM-based languages) _at runtime_. Not even when using reified type - // parameters of inline functions. See https://kotlinlang.org/docs/generics.html#type-erasure for details. - .filter { - val clazz = C::class.java - it.supportsCodegenContext(clazz) - } - .onEach { - logger.info("Adding Codegen Decorator: ${it.javaClass.name}") - } - .map { - // Cast is safe because of the filter above. - // Not that it really has an effect at runtime, since its unchecked. - @Suppress("UNCHECKED_CAST") - it as RustCodegenDecorator - } - } -} diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointsDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointsDecorator.kt index 6e360dc064..bb4329a6c0 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointsDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointsDecorator.kt @@ -14,19 +14,17 @@ import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameters import software.amazon.smithy.rulesengine.traits.ContextIndex import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointParamsGenerator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointTests import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointsModule import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.SmithyEndpointsStdLib import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection @@ -73,13 +71,10 @@ interface EndpointCustomization { * * If the service DOES NOT provide custom endpoint rules, this decorator is a no-op. */ -class EndpointsDecorator : RustCodegenDecorator { +class EndpointsDecorator : ClientCodegenDecorator { override val name: String = "Endpoints" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) - override fun operationCustomizations( codegenContext: ClientCodegenContext, operation: OperationShape, diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceGenerator.kt index c86934a87f..f21f56dfaf 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceGenerator.kt @@ -7,7 +7,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.generators import software.amazon.smithy.model.knowledge.TopDownIndex import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfigGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolTestGenerator @@ -29,7 +29,7 @@ class ServiceGenerator( private val protocolGenerator: ClientProtocolGenerator, private val protocolSupport: ProtocolSupport, private val clientCodegenContext: ClientCodegenContext, - private val decorator: RustCodegenDecorator, + private val decorator: ClientCodegenDecorator, ) { private val index = TopDownIndex.of(clientCodegenContext.model) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt index e16c2b1e64..f4327700bd 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt @@ -8,8 +8,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.generators.client import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Feature import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust @@ -23,7 +22,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection -class FluentClientDecorator : RustCodegenDecorator { +class FluentClientDecorator : ClientCodegenDecorator { override val name: String = "FluentClient" override val order: Byte = 0 @@ -60,9 +59,6 @@ class FluentClientDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } sealed class FluentClientSection(name: String) : Section(name) { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/CodegenIntegrationTest.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/CodegenIntegrationTest.kt index 350741c4f2..5d6a65b741 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/CodegenIntegrationTest.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/CodegenIntegrationTest.kt @@ -10,10 +10,8 @@ import software.amazon.smithy.build.SmithyBuildPlugin import software.amazon.smithy.model.Model import software.amazon.smithy.model.node.ObjectNode import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.RustCodegenPlugin -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext +import software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.testutil.generatePluginContext @@ -29,7 +27,7 @@ import java.nio.file.Path */ fun clientIntegrationTest( model: Model, - addtionalDecorators: List> = listOf(), + addtionalDecorators: List = listOf(), addModuleToEventStreamAllowList: Boolean = false, service: String? = null, runtimeConfig: RuntimeConfig? = null, @@ -39,7 +37,7 @@ fun clientIntegrationTest( ): Path { return codegenIntegrationTest( model, - RustCodegenPlugin(), + RustClientCodegenPlugin(), addtionalDecorators, addModuleToEventStreamAllowList = addModuleToEventStreamAllowList, service = service, @@ -56,10 +54,10 @@ fun clientIntegrationTest( * This exists to allow tests to easily customize the _real_ build without needing to list out customizations * or attempt to manually discover them from the path */ -abstract class DecoratableBuildPlugin : SmithyBuildPlugin { +abstract class DecoratableBuildPlugin : SmithyBuildPlugin { abstract fun executeWithDecorator( context: PluginContext, - vararg decorator: RustCodegenDecorator, + vararg decorator: ClientCodegenDecorator, ) override fun execute(context: PluginContext) { @@ -68,15 +66,15 @@ abstract class DecoratableBuildPlugin : SmithyBuildPlugin } // TODO(https://github.com/awslabs/smithy-rs/issues/1864): move to core once CodegenDecorator is in core -private fun codegenIntegrationTest( +private fun codegenIntegrationTest( model: Model, - buildPlugin: DecoratableBuildPlugin, - additionalDecorators: List>, + buildPlugin: DecoratableBuildPlugin, + additionalDecorators: List, additionalSettings: ObjectNode = ObjectNode.builder().build(), addModuleToEventStreamAllowList: Boolean = false, service: String? = null, runtimeConfig: RuntimeConfig? = null, - overrideTestDir: File? = null, test: (C, RustCrate) -> Unit, + overrideTestDir: File? = null, test: (ClientCodegenContext, RustCrate) -> Unit, command: ((Path) -> Unit)? = null, ): Path { val (ctx, testDir) = generatePluginContext( @@ -88,15 +86,13 @@ private fun codegenIntegrationTest( overrideTestDir, ) - val codegenDecorator = object : RustCodegenDecorator { + val codegenDecorator = object : ClientCodegenDecorator { override val name: String = "Add tests" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean { - // never discoverable on the classpath - return false - } - override fun extras(codegenContext: C, rustCrate: RustCrate) { + override fun classpathDiscoverable(): Boolean = false + + override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { test(codegenContext, rustCrate) } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/TestHelpers.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/TestHelpers.kt index be6be79dee..1b3f813959 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/TestHelpers.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/TestHelpers.kt @@ -11,7 +11,7 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenConfig import software.amazon.smithy.rust.codegen.client.smithy.ClientRustSettings -import software.amazon.smithy.rust.codegen.client.smithy.RustCodegenPlugin +import software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings @@ -48,7 +48,7 @@ fun clientTestRustSettings( ) fun testSymbolProvider(model: Model, serviceShape: ServiceShape? = null): RustSymbolProvider = - RustCodegenPlugin.baseSymbolProvider( + RustClientCodegenPlugin.baseSymbolProvider( model, serviceShape ?: ServiceShape.builder().version("test").id("test#Service").build(), TestSymbolVisitorConfig, diff --git a/codegen-client/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin b/codegen-client/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin index d4cc744ee6..90eae8d76b 100644 --- a/codegen-client/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin +++ b/codegen-client/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin @@ -2,4 +2,4 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 # -software.amazon.smithy.rust.codegen.client.smithy.RustCodegenPlugin +software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/customizations/HttpVersionListGeneratorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/customizations/HttpVersionListGeneratorTest.kt index 5113094a8d..495e348706 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/customizations/HttpVersionListGeneratorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/customizations/HttpVersionListGeneratorTest.kt @@ -7,17 +7,15 @@ package software.amazon.smithy.rust.codegen.client.customizations import org.junit.jupiter.api.Test import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.EventStreamSigningConfig import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.testutil.TokioTest @@ -198,10 +196,10 @@ internal class HttpVersionListGeneratorTest { } } -class FakeSigningDecorator : RustCodegenDecorator { +class FakeSigningDecorator : ClientCodegenDecorator { override val name: String = "fakesigning" override val order: Byte = 0 - override fun supportsCodegenContext(clazz: Class): Boolean = false + override fun classpathDiscoverable(): Boolean = false override fun configCustomizations( codegenContext: ClientCodegenContext, baseCustomizations: List, diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitorTest.kt index b86e3b35c7..f337d05659 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/CodegenVisitorTest.kt @@ -9,7 +9,7 @@ import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.customizations.ClientCustomizations -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStreamSigningDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator @@ -45,16 +45,16 @@ class CodegenVisitorTest { greeting: String } """.asSmithyModel(smithyVersion = "2.0") - val (ctx, testDir) = generatePluginContext(model) + val (ctx, _) = generatePluginContext(model) val codegenDecorator = - CombinedCodegenDecorator.fromClasspath( + CombinedClientCodegenDecorator.fromClasspath( ctx, ClientCustomizations(), RequiredCustomizations(), FluentClientDecorator(), NoOpEventStreamSigningDecorator(), ) - val visitor = CodegenVisitor(ctx, codegenDecorator) + val visitor = ClientCodegenVisitor(ctx, codegenDecorator) val baselineModel = visitor.baselineTransform(model) baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true } diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt index e6b1f53f2f..7e3ccfbef5 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt @@ -12,8 +12,8 @@ import software.amazon.smithy.aws.traits.protocols.RestJson1Trait import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.CodegenVisitor -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenVisitor +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter import software.amazon.smithy.rust.codegen.core.rustlang.escape import software.amazon.smithy.rust.codegen.core.rustlang.rust @@ -222,20 +222,18 @@ class ProtocolTestGeneratorTest { correctResponse: String = """Ok(crate::output::SayHelloOutput::builder().value("hey there!").build())""", ): Path { val (pluginContext, testDir) = generatePluginContext(model) - val codegenDecorator = object : RustCodegenDecorator { + val codegenDecorator = object : ClientCodegenDecorator { override val name: String = "mock" override val order: Byte = 0 + override fun classpathDiscoverable(): Boolean = false override fun protocols( serviceId: ShapeId, currentProtocols: ProtocolMap, ): ProtocolMap = // Intentionally replace the builtin implementation of RestJson1 with our fake protocol mapOf(RestJson1Trait.ID to TestProtocolFactory(httpRequestBuilder, body, correctResponse)) - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ClientCodegenContext::class.java) } - val visitor = CodegenVisitor( + val visitor = ClientCodegenVisitor( pluginContext, codegenDecorator, ) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CodegenContext.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CodegenContext.kt index 743eb63ebc..17a21c887c 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CodegenContext.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CodegenContext.kt @@ -14,7 +14,7 @@ import software.amazon.smithy.model.shapes.ShapeId * * Code-generation context is pervasive read-only global data that gets passed around to the generators. * - * If your data is specific to the `rust-codegen` client plugin, put it in [ClientCodegenContext] instead. + * If your data is specific to the `rust-client-codegen` client plugin, put it in [ClientCodegenContext] instead. * If your data is specific to the `rust-server-codegen` server plugin, put it in [ServerCodegenContext] instead. */ open class CodegenContext( diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CoreRustSettings.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CoreRustSettings.kt index ffe91eb170..72db4046d7 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CoreRustSettings.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/CoreRustSettings.kt @@ -32,7 +32,7 @@ const val CODEGEN_SETTINGS = "codegen" /** * [CoreCodegenConfig] contains code-generation configuration that is _common to all_ smithy-rs plugins. * - * If your configuration is specific to the `rust-codegen` client plugin, put it in [ClientCodegenContext] instead. + * If your configuration is specific to the `rust-client-codegen` client plugin, put it in [ClientCodegenContext] instead. * If your configuration is specific to the `rust-server-codegen` server plugin, put it in [ServerCodegenContext] instead. * * [formatTimeoutSeconds]: Timeout for running cargo fmt at the end of code generation @@ -64,7 +64,7 @@ open class CoreCodegenConfig( /** * [CoreRustSettings] contains crate settings that are _common to all_ smithy-rs plugins. * - * If your setting is specific to the crate that the `rust-codegen` client plugin generates, put it in + * If your setting is specific to the crate that the `rust-client-codegen` client plugin generates, put it in * [ClientCodegenContext] instead. * If your setting is specific to the crate that the `rust-server-codegen` server plugin generates, put it in * [ServerCodegenContext] instead. diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt index 218eb8af1b..446260ae96 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt @@ -11,7 +11,6 @@ import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rust.codegen.client.smithy.EventStreamSymbolProvider -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget @@ -19,9 +18,8 @@ import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig import software.amazon.smithy.rust.codegen.server.python.smithy.customizations.DECORATORS import software.amazon.smithy.rust.codegen.server.smithy.ConstrainedShapeSymbolProvider -import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.CombinedServerCodegenDecorator import java.util.logging.Level import java.util.logging.Logger @@ -44,10 +42,10 @@ class PythonCodegenServerPlugin : SmithyBuildPlugin { // - location (e.g. the mutate section of an operation) // - context (e.g. the of the operation) // - writer: The active RustWriter at the given location - val codegenDecorator: CombinedCodegenDecorator = - CombinedCodegenDecorator.fromClasspath( + val codegenDecorator: CombinedServerCodegenDecorator = + CombinedServerCodegenDecorator.fromClasspath( context, - CombinedCodegenDecorator(DECORATORS + ServerRequiredCustomizations()), + CombinedServerCodegenDecorator(DECORATORS + ServerRequiredCustomizations()), ) // PythonServerCodegenVisitor is the main driver of code generation that traverses the model and generates code diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt index c9d73999f3..00ea40dc7a 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt @@ -15,7 +15,6 @@ import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape import software.amazon.smithy.model.traits.EnumTrait -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RustCrate @@ -26,8 +25,8 @@ import software.amazon.smithy.rust.codegen.server.python.smithy.generators.Pytho import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenVisitor import software.amazon.smithy.rust.codegen.server.smithy.ServerSymbolProviders +import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader /** @@ -39,7 +38,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtoco */ class PythonServerCodegenVisitor( context: PluginContext, - codegenDecorator: RustCodegenDecorator, + codegenDecorator: ServerCodegenDecorator, ) : ServerCodegenVisitor(context, codegenDecorator) { init { diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt index fce0a5a27b..4c83ad9028 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt @@ -6,13 +6,11 @@ package software.amazon.smithy.rust.codegen.server.python.smithy.customizations import software.amazon.smithy.model.neighbor.Walker -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.docs import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection @@ -22,7 +20,7 @@ import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerRunt import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerModuleGenerator import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.customizations.AddInternalServerErrorToAllOperationsDecorator -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator /** * Configure the [lib] section of `Cargo.toml`. @@ -31,7 +29,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.Ser * name = "$CRATE_NAME" * crate-type = ["cdylib"] */ -class CdylibManifestDecorator : RustCodegenDecorator { +class CdylibManifestDecorator : ServerCodegenDecorator { override val name: String = "CdylibDecorator" override val order: Byte = 0 @@ -45,9 +43,6 @@ class CdylibManifestDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } /** @@ -71,7 +66,7 @@ class PubUsePythonTypes(private val codegenContext: ServerCodegenContext) : LibR /** * Render the Python shared library module export. */ -class PythonExportModuleDecorator : RustCodegenDecorator { +class PythonExportModuleDecorator : ServerCodegenDecorator { override val name: String = "PythonExportModuleDecorator" override val order: Byte = 0 @@ -80,15 +75,12 @@ class PythonExportModuleDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } /** * Decorator applying the customization from [PubUsePythonTypes] class. */ -class PubUsePythonTypesDecorator : RustCodegenDecorator { +class PubUsePythonTypesDecorator : ServerCodegenDecorator { override val name: String = "PubUsePythonTypesDecorator" override val order: Byte = 0 @@ -98,9 +90,6 @@ class PubUsePythonTypesDecorator : RustCodegenDecorator { return baseCustomizations + PubUsePythonTypes(codegenContext) } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } val DECORATORS = listOf( diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt index 5bbc073431..1da7d944fa 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt @@ -13,23 +13,22 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rust.codegen.client.smithy.EventStreamSymbolProvider import software.amazon.smithy.rust.codegen.client.smithy.StreamingShapeMetadataProvider import software.amazon.smithy.rust.codegen.client.smithy.StreamingShapeSymbolProvider -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.CombinedServerCodegenDecorator import java.util.logging.Level import java.util.logging.Logger /** - * Rust Codegen Plugin + * Rust Server Codegen Plugin * * This is the entrypoint for code generation, triggered by the smithy-build plugin. * `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which - * enables the smithy-build plugin to invoke `execute` with all of the Smithy plugin context + models. + * enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models. */ class RustCodegenServerPlugin : SmithyBuildPlugin { private val logger = Logger.getLogger(javaClass.name) @@ -44,8 +43,8 @@ class RustCodegenServerPlugin : SmithyBuildPlugin { // - location (e.g. the mutate section of an operation) // - context (e.g. the of the operation) // - writer: The active RustWriter at the given location - val codegenDecorator: CombinedCodegenDecorator = - CombinedCodegenDecorator.fromClasspath(context, ServerRequiredCustomizations()) + val codegenDecorator: CombinedServerCodegenDecorator = + CombinedServerCodegenDecorator.fromClasspath(context, ServerRequiredCustomizations()) // ServerCodegenVisitor is the main driver of code generation that traverses the model and generates code logger.info("Loaded plugin to generate pure Rust bindings for the server SDK") 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 07009c3eb9..74a79753a4 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 @@ -27,7 +27,6 @@ import software.amazon.smithy.model.shapes.UnionShape import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.model.traits.LengthTrait import software.amazon.smithy.model.transform.ModelTransformer -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.ConstrainedModule @@ -46,6 +45,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.transformers.RecursiveSha import software.amazon.smithy.rust.codegen.core.util.CommandFailed import software.amazon.smithy.rust.codegen.core.util.hasTrait import software.amazon.smithy.rust.codegen.core.util.runCommand +import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator import software.amazon.smithy.rust.codegen.server.smithy.generators.CollectionConstraintViolationGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.CollectionTraitInfo import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedCollectionGenerator @@ -79,7 +79,7 @@ import java.util.logging.Logger */ open class ServerCodegenVisitor( context: PluginContext, - private val codegenDecorator: RustCodegenDecorator, + private val codegenDecorator: ServerCodegenDecorator, ) : ShapeVisitor.Default() { protected val logger = Logger.getLogger(javaClass.name) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/AdditionalErrorsDecorator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/AdditionalErrorsDecorator.kt index 3f5e5cdab0..032de89304 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/AdditionalErrorsDecorator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/AdditionalErrorsDecorator.kt @@ -13,11 +13,8 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.traits.ErrorTrait import software.amazon.smithy.model.traits.RequiredTrait import software.amazon.smithy.model.transform.ModelTransformer -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.transformers.allErrors -import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator /** * Add at least one error to all operations in the model. @@ -33,15 +30,12 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.Ser * mkdir -p "$D" && echo "$C" > "$D/$F" * ``` */ -class AddInternalServerErrorToInfallibleOperationsDecorator : RustCodegenDecorator { +class AddInternalServerErrorToInfallibleOperationsDecorator : ServerCodegenDecorator { override val name: String = "AddInternalServerErrorToInfallibleOperations" override val order: Byte = 0 override fun transformModel(service: ServiceShape, model: Model): Model = addErrorShapeToModelOperations(service, model) { shape -> shape.allErrors(model).isEmpty() } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } /** @@ -62,15 +56,12 @@ class AddInternalServerErrorToInfallibleOperationsDecorator : RustCodegenDecorat * mkdir -p "$D" && echo "$C" > "$D/$F" * ``` */ -class AddInternalServerErrorToAllOperationsDecorator : RustCodegenDecorator { +class AddInternalServerErrorToAllOperationsDecorator : ServerCodegenDecorator { override val name: String = "AddInternalServerErrorToAllOperations" override val order: Byte = 0 override fun transformModel(service: ServiceShape, model: Model): Model = addErrorShapeToModelOperations(service, model) { true } - - override fun supportsCodegenContext(clazz: Class): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } fun addErrorShapeToModelOperations(service: ServiceShape, model: Model, opSelector: (OperationShape) -> Boolean): Model { diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt index 9b2e94c87d..2949d605de 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt @@ -8,22 +8,20 @@ package software.amazon.smithy.rust.codegen.server.smithy.customizations import software.amazon.smithy.rust.codegen.client.smithy.customizations.AllowLintsGenerator import software.amazon.smithy.rust.codegen.client.smithy.customizations.CrateVersionGenerator import software.amazon.smithy.rust.codegen.client.smithy.customizations.pubUseSmithyTypes -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.core.rustlang.Feature -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator /** * A set of customizations that are included in all protocols. * * This exists as a convenient place to gather these modifications, these are not true customizations. * - * See [RequiredCustomizations] from the `rust-codegen` subproject for the client version of this decorator. + * See [RequiredCustomizations] from the `codegen-client` subproject for the client version of this decorator. */ -class ServerRequiredCustomizations : RustCodegenDecorator { +class ServerRequiredCustomizations : ServerCodegenDecorator { override val name: String = "ServerRequired" override val order: Byte = -1 @@ -39,7 +37,4 @@ class ServerRequiredCustomizations : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt new file mode 100644 index 0000000000..9a6cd86ac4 --- /dev/null +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt @@ -0,0 +1,132 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.server.smithy.customize + +import software.amazon.smithy.build.PluginContext +import software.amazon.smithy.model.Model +import software.amazon.smithy.model.shapes.ServiceShape +import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.rust.codegen.core.smithy.RustCrate +import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization +import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations +import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolMap +import software.amazon.smithy.rust.codegen.core.util.deepMergeWith +import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import java.util.ServiceLoader +import java.util.logging.Logger + +typealias ServerProtocolMap = ProtocolMap + +/** + * [ServerCodegenDecorator] allows downstream users to customize code generation. + * + * For example, AWS-specific code generation generates customizations required to support + * AWS services. A different downstream customer may wish to add a different set of derive + * attributes to the generated classes. + */ +interface ServerCodegenDecorator { + /** + * The name of this [ServerCodegenDecorator], used for logging and debug information + */ + val name: String + + /** + * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first + */ + val order: Byte + + /** + * Whether this decorator can be discovered on the classpath (defaults to true). + * This is intended to only be overridden for decorators written specifically for tests. + */ + fun classpathDiscoverable(): Boolean = true + + fun libRsCustomizations( + codegenContext: ServerCodegenContext, + baseCustomizations: List, + ): List = baseCustomizations + + /** + * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be + * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` + * could be returned. Properties here overwrite the default properties. + */ + fun crateManifestCustomizations(codegenContext: ServerCodegenContext): ManifestCustomizations = emptyMap() + + fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) {} + + fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap = currentProtocols + + fun transformModel(service: ServiceShape, model: Model): Model = model +} + +/** + * [CombinedServerCodegenDecorator] merges the results of multiple decorators into a single decorator. + * + * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. + */ +class CombinedServerCodegenDecorator(decorators: List) : ServerCodegenDecorator { + private val orderedDecorators = decorators.sortedBy { it.order } + override val name: String + get() = "CombinedServerCodegenDecorator" + override val order: Byte + get() = 0 + + override fun libRsCustomizations( + codegenContext: ServerCodegenContext, + baseCustomizations: List, + ): List { + return orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> + decorator.libRsCustomizations( + codegenContext, + customizations, + ) + } + } + + override fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap { + return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> + decorator.protocols(serviceId, protocolMap) + } + } + + override fun crateManifestCustomizations(codegenContext: ServerCodegenContext): ManifestCustomizations { + return orderedDecorators.foldRight(emptyMap()) { decorator, customizations -> + customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) + } + } + + override fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) { + return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } + } + + override fun transformModel(service: ServiceShape, model: Model): Model { + return orderedDecorators.foldRight(model) { decorator, otherModel -> + decorator.transformModel(service, otherModel) + } + } + + companion object { + fun fromClasspath( + context: PluginContext, + vararg extras: ServerCodegenDecorator, + logger: Logger = Logger.getLogger("RustServerCodegenSPILoader"), + ): CombinedServerCodegenDecorator { + val decorators = ServiceLoader.load( + ServerCodegenDecorator::class.java, + context.pluginClassLoader.orElse(ServerCodegenDecorator::class.java.classLoader), + ) + + val filteredDecorators = decorators.asSequence() + .onEach { logger.info("Discovered Codegen Decorator: ${it.javaClass.name}") } + .filter { it.classpathDiscoverable() } + .onEach { logger.info("Adding Codegen Decorator: ${it.javaClass.name}") } + .toList() + return CombinedServerCodegenDecorator(filteredDecorators + extras) + } + } +} 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 e51d5b813a..2f23c143f4 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,10 @@ 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.client.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.core.testutil.generatePluginContext import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator +import software.amazon.smithy.rust.codegen.server.smithy.customize.CombinedServerCodegenDecorator import kotlin.io.path.writeText class ServerCodegenVisitorTest { @@ -46,8 +45,8 @@ class ServerCodegenVisitorTest { """.asSmithyModel(smithyVersion = "2.0") val (ctx, testDir) = generatePluginContext(model) testDir.resolve("src/main.rs").writeText("fn main() {}") - val codegenDecorator: CombinedCodegenDecorator = - CombinedCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) + val codegenDecorator: CombinedServerCodegenDecorator = + CombinedServerCodegenDecorator.fromClasspath(ctx, ServerRequiredCustomizations()) val visitor = ServerCodegenVisitor(ctx, codegenDecorator) val baselineModel = visitor.baselineTransformInternalTest(model) baselineModel.getShapesWithTrait(ShapeId.from("smithy.api#mixin")).isEmpty() shouldBe true diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/CombinedCodegenDecoratorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/CombinedCodegenDecoratorTest.kt deleted file mode 100644 index 8287f2d2ce..0000000000 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/CombinedCodegenDecoratorTest.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.rust.codegen.smithy.customize - -import io.kotest.matchers.collections.shouldContainExactly -import org.junit.jupiter.api.Test -import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations -import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator -import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext -import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations -import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator - -internal class CombinedCodegenDecoratorTest { - private val clientDecorator: RustCodegenDecorator = RequiredCustomizations() - private val serverDecorator: RustCodegenDecorator = ServerRequiredCustomizations() - - @Test - fun filterClientDecorators() { - val filteredDecorators = CombinedCodegenDecorator.filterDecorators( - listOf(clientDecorator, serverDecorator), - ).toList() - - filteredDecorators.shouldContainExactly(clientDecorator) - } - - @Test - fun filterServerDecorators() { - val filteredDecorators = CombinedCodegenDecorator.filterDecorators( - listOf(clientDecorator, serverDecorator), - ).toList() - - filteredDecorators.shouldContainExactly(serverDecorator) - } -} diff --git a/rust-runtime/aws-smithy-http-server-python/examples/Makefile b/rust-runtime/aws-smithy-http-server-python/examples/Makefile index 1654918fff..af76cd4b3a 100644 --- a/rust-runtime/aws-smithy-http-server-python/examples/Makefile +++ b/rust-runtime/aws-smithy-http-server-python/examples/Makefile @@ -5,7 +5,7 @@ GRADLE := $(SRC_DIR)/gradlew SERVER_SDK_DST := $(CUR_DIR)/pokemon-service-server-sdk CLIENT_SDK_DST := $(CUR_DIR)/pokemon-service-client SERVER_SDK_SRC := $(SRC_DIR)/codegen-server-test/python/build/smithyprojections/codegen-server-test-python/pokemon-service-server-sdk/rust-server-codegen-python -CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client/rust-codegen +CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client/rust-client-codegen SHARED_LIBRARY_DST := $(CUR_DIR)/libpokemon_service_server_sdk.so ifeq ($(OS), Darwin) diff --git a/rust-runtime/aws-smithy-http-server/examples/Makefile b/rust-runtime/aws-smithy-http-server/examples/Makefile index a31e805d8b..9c2bf9061d 100644 --- a/rust-runtime/aws-smithy-http-server/examples/Makefile +++ b/rust-runtime/aws-smithy-http-server/examples/Makefile @@ -4,7 +4,7 @@ GRADLE := $(SRC_DIR)/gradlew SERVER_SDK_DST := $(CUR_DIR)/pokemon-service-server-sdk CLIENT_SDK_DST := $(CUR_DIR)/pokemon-service-client SERVER_SDK_SRC := $(SRC_DIR)/codegen-server-test/build/smithyprojections/codegen-server-test/pokemon-service-server-sdk/rust-server-codegen -CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client/rust-codegen +CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client/rust-client-codegen all: codegen From 71d842c8d6887cb5dc49698adf49335340974772 Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Wed, 14 Dec 2022 10:44:32 -0800 Subject: [PATCH 2/3] Incorporate feedback --- .../customize/ClientCodegenDecorator.kt | 96 ++---------- .../smithy/customize/CoreCodegenDecorator.kt | 147 ++++++++++++++++++ .../customize/ServerCodegenDecorator.kt | 98 ++---------- 3 files changed, 170 insertions(+), 171 deletions(-) create mode 100644 codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt index dae7af0ac1..f4034c1b9b 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/ClientCodegenDecorator.kt @@ -6,20 +6,16 @@ package software.amazon.smithy.rust.codegen.client.smithy.customize import software.amazon.smithy.build.PluginContext -import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.OperationShape -import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator -import software.amazon.smithy.rust.codegen.core.smithy.RustCrate +import software.amazon.smithy.rust.codegen.core.smithy.customize.CombinedCoreCodegenDecorator +import software.amazon.smithy.rust.codegen.core.smithy.customize.CoreCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization -import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization -import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolMap -import software.amazon.smithy.rust.codegen.core.util.deepMergeWith import java.util.ServiceLoader import java.util.logging.Logger @@ -32,23 +28,7 @@ typealias ClientProtocolMap = ProtocolMap { fun configCustomizations( codegenContext: ClientCodegenContext, baseCustomizations: List, @@ -60,24 +40,8 @@ interface ClientCodegenDecorator { baseCustomizations: List, ): List = baseCustomizations - fun libRsCustomizations( - codegenContext: ClientCodegenContext, - baseCustomizations: List, - ): List = baseCustomizations - - /** - * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be - * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` - * could be returned. Properties here overwrite the default properties. - */ - fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations = emptyMap() - - fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {} - fun protocols(serviceId: ShapeId, currentProtocols: ClientProtocolMap): ClientProtocolMap = currentProtocols - fun transformModel(service: ServiceShape, model: Model): Model = model - fun endpointCustomizations(codegenContext: ClientCodegenContext): List = listOf() } @@ -86,8 +50,8 @@ interface ClientCodegenDecorator { * * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. */ -open class CombinedClientCodegenDecorator(decorators: List) : ClientCodegenDecorator { - private val orderedDecorators = decorators.sortedBy { it.order } +open class CombinedClientCodegenDecorator(decorators: List) : + CombinedCoreCodegenDecorator(decorators), ClientCodegenDecorator { override val name: String get() = "CombinedClientCodegenDecorator" override val order: Byte @@ -96,59 +60,25 @@ open class CombinedClientCodegenDecorator(decorators: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator: ClientCodegenDecorator, customizations -> - decorator.configCustomizations(codegenContext, customizations) - } + ): List = combineCustomizations(baseCustomizations) { decorator, customizations -> + decorator.configCustomizations(codegenContext, customizations) } override fun operationCustomizations( codegenContext: ClientCodegenContext, operation: OperationShape, baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator: ClientCodegenDecorator, customizations -> - decorator.operationCustomizations(codegenContext, operation, customizations) - } + ): List = combineCustomizations(baseCustomizations) { decorator, customizations -> + decorator.operationCustomizations(codegenContext, operation, customizations) } - override fun libRsCustomizations( - codegenContext: ClientCodegenContext, - baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> - decorator.libRsCustomizations( - codegenContext, - customizations, - ) - } - } - - override fun protocols(serviceId: ShapeId, currentProtocols: ClientProtocolMap): ClientProtocolMap { - return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> + override fun protocols(serviceId: ShapeId, currentProtocols: ClientProtocolMap): ClientProtocolMap = + combineCustomizations(currentProtocols) { decorator, protocolMap -> decorator.protocols(serviceId, protocolMap) } - } - - override fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations { - return orderedDecorators.foldRight(emptyMap()) { decorator, customizations -> - customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) - } - } - - override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { - return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } - } - - override fun transformModel(service: ServiceShape, model: Model): Model { - return orderedDecorators.foldRight(model) { decorator, otherModel -> - decorator.transformModel(service, otherModel) - } - } - override fun endpointCustomizations(codegenContext: ClientCodegenContext): List { - return orderedDecorators.flatMap { it.endpointCustomizations(codegenContext) } - } + override fun endpointCustomizations(codegenContext: ClientCodegenContext): List = + addCustomizations { decorator -> decorator.endpointCustomizations(codegenContext) } companion object { fun fromClasspath( diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt new file mode 100644 index 0000000000..1df251c99f --- /dev/null +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt @@ -0,0 +1,147 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.core.smithy.customize + +import software.amazon.smithy.build.PluginContext +import software.amazon.smithy.model.Model +import software.amazon.smithy.model.shapes.ServiceShape +import software.amazon.smithy.rust.codegen.core.smithy.RustCrate +import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization +import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations +import software.amazon.smithy.rust.codegen.core.util.deepMergeWith +import java.util.ServiceLoader +import java.util.logging.Logger + +/** + * Represents the bare minimum for codegen plugin customization. + */ +interface CoreCodegenDecorator { + /** + * The name of this decorator, used for logging and debug information + */ + val name: String + + /** + * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first + */ + val order: Byte + + /** + * Whether this decorator can be discovered on the classpath (defaults to true). + * This is intended to only be overridden for decorators written specifically for tests. + */ + fun classpathDiscoverable(): Boolean = true + + /** + * Hook to transform the Smithy model before codegen takes place. + */ + fun transformModel(service: ServiceShape, model: Model): Model = model + + /** + * Hook to add additional modules to the generated crate. + */ + fun extras(codegenContext: CodegenContext, rustCrate: RustCrate) {} + + /** + * Hook to customize the generated `Cargo.toml` file. + * + * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be + * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` + * could be returned. Properties here overwrite the default properties. + */ + fun crateManifestCustomizations(codegenContext: CodegenContext): ManifestCustomizations = emptyMap() + + /** + * Hook to customize the generated `lib.rs` file. + */ + fun libRsCustomizations( + codegenContext: CodegenContext, + baseCustomizations: List, + ): List = baseCustomizations +} + +/** + * Implementations for combining decorators for the core customizations. + */ +abstract class CombinedCoreCodegenDecorator>( + decorators: List, +) : CoreCodegenDecorator { + private val orderedDecorators = decorators.sortedBy { it.order } + + final override fun libRsCustomizations( + codegenContext: CodegenContext, + baseCustomizations: List, + ): List = + combineCustomizations(baseCustomizations) { decorator, customizations -> + decorator.libRsCustomizations(codegenContext, customizations) + } + + final override fun crateManifestCustomizations(codegenContext: CodegenContext): ManifestCustomizations = + combineCustomizations(emptyMap()) { decorator, customizations -> + customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) + } + + final override fun extras(codegenContext: CodegenContext, rustCrate: RustCrate) { + return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } + } + + final override fun transformModel(service: ServiceShape, model: Model): Model = + combineCustomizations(model) { decorator, otherModel -> + decorator.transformModel(service, otherModel) + } + + /** + * Combines customizations from multiple ordered codegen decorators. + * + * Using this combinator allows for customizations to remove other customizations since the `mergeCustomizations` + * function can mutate the entire returned customization list. + */ + protected fun combineCustomizations( + /** Initial customizations. These will remain at the front of the list unless `mergeCustomizations` changes order. */ + baseCustomizations: CombinedCustomizations, + /** A function that retrieves customizations from a decorator and combines them with the given customizations. */ + mergeCustomizations: (Decorator, CombinedCustomizations) -> CombinedCustomizations, + ): CombinedCustomizations = + orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> + mergeCustomizations(decorator, customizations) + } + + /** + * Combines customizations from multiple ordered codegen decorators in a purely additive way. + * + * Unlike `combineCustomizations`, customizations combined in this way cannot remove other customizations. + */ + protected fun addCustomizations( + /** Returns customizations from a decorator. */ + getCustomizations: (Decorator) -> List, + ): List = orderedDecorators.flatMap(getCustomizations) + + companion object { + /** + * Loads decorators of the given class from the classpath and filters them by the `classpathDiscoverable` + * method on `CoreCodegenDecorator`. + */ + @JvmStatic + protected fun > decoratorsFromClasspath( + context: PluginContext, + decoratorClass: Class, + logger: Logger, + vararg extras: Decorator, + ): List { + val decorators = ServiceLoader.load( + decoratorClass, + context.pluginClassLoader.orElse(decoratorClass.classLoader), + ) + + val filteredDecorators = decorators.asSequence() + .onEach { logger.info("Discovered Codegen Decorator: ${it!!::class.java.name}") } + .filter { it!!.classpathDiscoverable() } + .onEach { logger.info("Adding Codegen Decorator: ${it!!::class.java.name}") } + .toList() + return filteredDecorators + extras + } + } +} diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt index 9a6cd86ac4..b5b0f19295 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt @@ -6,62 +6,21 @@ package software.amazon.smithy.rust.codegen.server.smithy.customize import software.amazon.smithy.build.PluginContext -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.rust.codegen.core.smithy.RustCrate -import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization -import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations +import software.amazon.smithy.rust.codegen.core.smithy.customize.CombinedCoreCodegenDecorator +import software.amazon.smithy.rust.codegen.core.smithy.customize.CoreCodegenDecorator import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolMap -import software.amazon.smithy.rust.codegen.core.util.deepMergeWith import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator -import java.util.ServiceLoader import java.util.logging.Logger typealias ServerProtocolMap = ProtocolMap /** * [ServerCodegenDecorator] allows downstream users to customize code generation. - * - * For example, AWS-specific code generation generates customizations required to support - * AWS services. A different downstream customer may wish to add a different set of derive - * attributes to the generated classes. */ -interface ServerCodegenDecorator { - /** - * The name of this [ServerCodegenDecorator], used for logging and debug information - */ - val name: String - - /** - * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first - */ - val order: Byte - - /** - * Whether this decorator can be discovered on the classpath (defaults to true). - * This is intended to only be overridden for decorators written specifically for tests. - */ - fun classpathDiscoverable(): Boolean = true - - fun libRsCustomizations( - codegenContext: ServerCodegenContext, - baseCustomizations: List, - ): List = baseCustomizations - - /** - * Returns a map of Cargo.toml properties to change. For example, if a `homepage` needs to be - * added to the Cargo.toml `[package]` section, a `mapOf("package" to mapOf("homepage", "https://example.com"))` - * could be returned. Properties here overwrite the default properties. - */ - fun crateManifestCustomizations(codegenContext: ServerCodegenContext): ManifestCustomizations = emptyMap() - - fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) {} - +interface ServerCodegenDecorator : CoreCodegenDecorator { fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap = currentProtocols - - fun transformModel(service: ServiceShape, model: Model): Model = model } /** @@ -69,46 +28,18 @@ interface ServerCodegenDecorator { * * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. */ -class CombinedServerCodegenDecorator(decorators: List) : ServerCodegenDecorator { - private val orderedDecorators = decorators.sortedBy { it.order } +class CombinedServerCodegenDecorator(decorators: List) : + CombinedCoreCodegenDecorator(decorators), + ServerCodegenDecorator { override val name: String get() = "CombinedServerCodegenDecorator" override val order: Byte get() = 0 - override fun libRsCustomizations( - codegenContext: ServerCodegenContext, - baseCustomizations: List, - ): List { - return orderedDecorators.foldRight(baseCustomizations) { decorator, customizations -> - decorator.libRsCustomizations( - codegenContext, - customizations, - ) - } - } - - override fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap { - return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> + override fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap = + combineCustomizations(currentProtocols) { decorator, protocolMap -> decorator.protocols(serviceId, protocolMap) } - } - - override fun crateManifestCustomizations(codegenContext: ServerCodegenContext): ManifestCustomizations { - return orderedDecorators.foldRight(emptyMap()) { decorator, customizations -> - customizations.deepMergeWith(decorator.crateManifestCustomizations(codegenContext)) - } - } - - override fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) { - return orderedDecorators.forEach { it.extras(codegenContext, rustCrate) } - } - - override fun transformModel(service: ServiceShape, model: Model): Model { - return orderedDecorators.foldRight(model) { decorator, otherModel -> - decorator.transformModel(service, otherModel) - } - } companion object { fun fromClasspath( @@ -116,17 +47,8 @@ class CombinedServerCodegenDecorator(decorators: List) : vararg extras: ServerCodegenDecorator, logger: Logger = Logger.getLogger("RustServerCodegenSPILoader"), ): CombinedServerCodegenDecorator { - val decorators = ServiceLoader.load( - ServerCodegenDecorator::class.java, - context.pluginClassLoader.orElse(ServerCodegenDecorator::class.java.classLoader), - ) - - val filteredDecorators = decorators.asSequence() - .onEach { logger.info("Discovered Codegen Decorator: ${it.javaClass.name}") } - .filter { it.classpathDiscoverable() } - .onEach { logger.info("Adding Codegen Decorator: ${it.javaClass.name}") } - .toList() - return CombinedServerCodegenDecorator(filteredDecorators + extras) + val decorators = decoratorsFromClasspath(context, ServerCodegenDecorator::class.java, logger, *extras) + return CombinedServerCodegenDecorator(decorators) } } } From b2845332970549b613b5e2b1650321e260c64606 Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Wed, 14 Dec 2022 10:46:01 -0800 Subject: [PATCH 3/3] Fix merge issue --- .../smithy/customizations/PythonServerCodegenDecorator.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt index 4728693559..da29220e04 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt @@ -97,7 +97,7 @@ class PubUsePythonTypesDecorator : ServerCodegenDecorator { * Generates `pyproject.toml` for the crate. * - Configures Maturin as the build system */ -class PyProjectTomlDecorator : RustCodegenDecorator { +class PyProjectTomlDecorator : ServerCodegenDecorator { override val name: String = "PyProjectTomlDecorator" override val order: Byte = 0 @@ -112,9 +112,6 @@ class PyProjectTomlDecorator : RustCodegenDecorator): Boolean = - clazz.isAssignableFrom(ServerCodegenContext::class.java) } val DECORATORS = listOf(