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 3978332f60118e5d7708b2dcf127d5e4504e7bdf..7690798a3a665367a4fcc571f5be39933aaca4d3 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 @@ -73,6 +73,9 @@ class AwsEndpointDecorator : RustCodegenDecorator { ): List { return baseCustomizations + PubUseEndpoint(codegenContext.runtimeConfig) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class EndpointConfigCustomization(private val coreCodegenContext: CoreCodegenContext, private val endpointData: ObjectNode) : 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 b4bf87eb58c918ce9fdfca46b09b5fc7be3a7078..fe091fdbda0902223a6ae41a5b7d51dee9b221fe 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 @@ -111,6 +111,9 @@ class AwsFluentClientDecorator : RustCodegenDecorator { } } } + + override fun supportsCodegenContext(clazz: Class): 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 d3765fed88ea871f92bc031f0a7f4cd07f962b7f..584f2fac0197e6287196da68d7e09d71a7f72588 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 @@ -117,6 +117,9 @@ 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 1ef704217327db139f0d19f76560a0ad7aa0e746..3b18640f28c7bf7eea1634faffd589b4ba6327ae 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 @@ -11,6 +11,7 @@ import org.jsoup.nodes.TextNode import software.amazon.smithy.model.traits.DocumentationTrait import software.amazon.smithy.rust.codegen.rustlang.raw import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.ManifestCustomizations @@ -110,6 +111,9 @@ class AwsReadmeDecorator : RustCodegenDecorator { } } + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) + /** * Strips HTML from the description and makes it human-readable Markdown. */ 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 ed152fe83a549ed38b967112fb9a0c8082bb4440..ae8940dd9225610d5926eb9a065507b44c494f5a 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 @@ -7,6 +7,7 @@ package software.amazon.smithy.rustsdk import software.amazon.smithy.rust.codegen.rustlang.raw import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator @@ -21,4 +22,7 @@ class CrateLicenseDecorator : RustCodegenDecorator { it.raw(license) } } + + override fun supportsCodegenContext(clazz: Class): 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 6853fa2aea2a0a8691cf2bc7fd5d693d412d1caf..4bcaa3d1b7311ee0bc533ceea50fea7acc0d1aaa 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 @@ -12,6 +12,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization @@ -47,6 +48,9 @@ class CredentialsProviderDecorator : RustCodegenDecorator ): List { 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/HttpRequestChecksumDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt index d953c23b022bd6ca4ecfa62b0b22684f40cde755..f049ee854692c699914b6007d0d43da178bf0939 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 @@ -14,6 +14,7 @@ import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization @@ -51,6 +52,9 @@ class HttpRequestChecksumDecorator : RustCodegenDecorator ): List { 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 b253f6fa4dc7964bc7904082837800fa51fde158..0bd4cac382216e42c6ae8e56e8a25ceb4c48a06e 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 @@ -11,6 +11,7 @@ import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.smithy.customize.OperationSection import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator @@ -38,6 +39,9 @@ class HttpResponseChecksumDecorator : RustCodegenDecorator ): List { 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 9189087098e961fc62b7c1415fd6baa7198695a4..f8d67754cdce64c7cdaa725d355499997184d53a 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 @@ -13,6 +13,7 @@ import software.amazon.smithy.rust.codegen.rustlang.DependencyScope import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization @@ -49,6 +50,9 @@ class IntegrationTestDecorator : RustCodegenDecorator { baseCustomizations } } + + override fun supportsCodegenContext(clazz: Class): 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 e2da2a16b4c79d936e25a4728ab1c1a87741a14d..74fa0c2ea1df94b9e2260453a98a3cd5f55f390b 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 @@ -95,6 +95,9 @@ class RegionDecorator : RustCodegenDecorator { ): List { return baseCustomizations + PubUseRegion(codegenContext.runtimeConfig) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class RegionProviderConfig(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryPolicyDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryPolicyDecorator.kt index 76ccbf6561a45ff51a1653d4cffd3e52714fff72..5daee32e56a962eec0f32b272cc8c4cc9cf9cc10 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryPolicyDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RetryPolicyDecorator.kt @@ -10,6 +10,7 @@ import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization @@ -27,6 +28,9 @@ class RetryPolicyDecorator : RustCodegenDecorator { ): List { return baseCustomizations + RetryPolicyFeature(codegenContext.runtimeConfig) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class RetryPolicyFeature(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 81118b45976f1b56c7b86f2457cc17a65850c838..5034de6b5d532d3eb1e9c7b83018bf18eb89bb36 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 @@ -11,6 +11,7 @@ import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator @@ -66,6 +67,9 @@ class SdkConfigDecorator : RustCodegenDecorator { ) } } + + override fun supportsCodegenContext(clazz: Class): 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 a5dd575e0393d51a3eaff48a6db3121787fcb70d..75ddac0a1e8a666679a35ce88d1c7ce4ebb49a05 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 @@ -9,6 +9,7 @@ import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.docs import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.smithy.generators.config.ServiceConfig @@ -21,6 +22,9 @@ class ServiceConfigDecorator : RustCodegenDecorator { codegenContext: ClientCodegenContext, baseCustomizations: List, ): 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 253af7d5e4eece30dc685caff1394e27622a121f..13f517a43cd8fe3791940cae5272b8d76d04ce3e 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 @@ -75,6 +75,9 @@ class SigV4SigningDecorator : RustCodegenDecorator { ) } } + + override fun supportsCodegenContext(clazz: Class): 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 d5d93672355e18a85d65ba739d33f098c5719dba..d4f54a6f354f33056ea4b8936a71a35d8eed75af 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 @@ -13,6 +13,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization @@ -55,6 +56,9 @@ class UserAgentDecorator : RustCodegenDecorator { ): List { 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 881d5a6acb4c4f285fe333288a6bb02b72c61535..0918208bc627307af1f642c267b932b0df8cc1dc 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 @@ -34,6 +34,9 @@ class ApiGatewayDecorator : RustCodegenDecorator { it + ApiGatewayAddAcceptHeader() } } + + override fun supportsCodegenContext(clazz: Class): 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 3d63ccdf29a113d91b637f5b375bad5064aacfca..5e393276e9bc390b5c76f89202b0179cae3ef287 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 @@ -12,6 +12,7 @@ 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.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator private fun String.shapeId() = ShapeId.from(this) @@ -46,4 +47,7 @@ class DisabledAuthDecorator : RustCodegenDecorator { } } } + + 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/ec2/Ec2Decorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/ec2/Ec2Decorator.kt index fef52a803d226e66413b93107542d160e3226f5a..d4152dc324d63662134a979693b7df6894a5dfb1 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 @@ -9,6 +9,7 @@ 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.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.letIf @@ -28,4 +29,7 @@ class Ec2Decorator : RustCodegenDecorator { BoxPrimitiveShapes::processModel, ) } + + 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/glacier/GlacierDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/glacier/GlacierDecorator.kt index 1bb6fcbe8605bc66f160b0dfa59bdcc7fe0fd9bd..0de378ca9e6fbee6d0e821075b80b35bc9c1b68c 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 @@ -37,4 +37,7 @@ class GlacierDecorator : RustCodegenDecorator { } return baseCustomizations + extras } + + 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/route53/Route53Decorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/route53/Route53Decorator.kt index ca62e1cd5a61bc381ba084db20319406257ab0a2..ddfab5aac0cd4bd7e1862ae5034a40f4bb467155 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 @@ -16,6 +16,7 @@ import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization import software.amazon.smithy.rust.codegen.smithy.customize.OperationSection @@ -59,6 +60,9 @@ class Route53Decorator : RustCodegenDecorator { } else baseCustomizations } + override fun supportsCodegenContext(clazz: Class): 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 d31f44ab62a6d3a66ab986ace442932e3db1baba..79ce312dd9f9a26d3cc857700011300faa5c6753 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 @@ -56,6 +56,9 @@ class S3Decorator : RustCodegenDecorator { ): List = baseCustomizations.letIf(applies(codegenContext.serviceShape.id)) { it + S3PubUse() } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class S3(coreCodegenContext: CoreCodegenContext) : RestXml(coreCodegenContext) { diff --git a/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/EndpointConfigCustomizationTest.kt b/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/EndpointConfigCustomizationTest.kt index fccb2fe4b41c0fb07e16e3fd0fb990ada24d4cd3..528c0a236160fa13e314fb6bf4ae0b06f4bace94 100644 --- a/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/EndpointConfigCustomizationTest.kt +++ b/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/EndpointConfigCustomizationTest.kt @@ -12,6 +12,7 @@ import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customizations.AllowLintsGenerator import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator @@ -147,6 +148,9 @@ internal class EndpointConfigCustomizationTest { test(rustCrate) } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } val customization = CombinedCodegenDecorator(listOf(RequiredCustomizations(), codegenDecorator)) CodegenVisitor(context, customization).execute() 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 5c89e2094b72edf130f42e5ecf6653c8e958a90c..4df3b72fca872fa511c0d5748e441f85da7f01ed 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,6 +11,7 @@ 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.rustlang.RustReservedWordSymbolProvider +import software.amazon.smithy.rust.codegen.server.python.smithy.customizations.DECORATORS import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerSymbolProvider import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations import software.amazon.smithy.rust.codegen.smithy.BaseSymbolMetadataProvider @@ -45,7 +46,10 @@ class PythonCodegenServerPlugin : SmithyBuildPlugin { // - context (e.g. the of the operation) // - writer: The active RustWriter at the given location val codegenDecorator: CombinedCodegenDecorator = - CombinedCodegenDecorator.fromClasspath(context, ServerRequiredCustomizations()) + CombinedCodegenDecorator.fromClasspath( + context, + CombinedCodegenDecorator(DECORATORS + ServerRequiredCustomizations()), + ) // PythonServerCodegenVisitor is the main driver of code generation that traverses the model and generates code logger.info("Loaded plugin to generate Rust/Python bindings for the server SSDK for projection ${context.projectionName}") 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 734df0b3e0560373af1fac41ea0e481e88ae0bc0..3d033e870b611285d39c0a211e7a727ae453a43e 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 @@ -14,9 +14,9 @@ import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerRuntimeType import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerModuleGenerator import software.amazon.smithy.rust.codegen.server.smithy.customizations.AddInternalServerErrorToAllOperationsDecorator +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext -import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.smithy.generators.LibRsSection @@ -44,6 +44,9 @@ class CdylibManifestDecorator : RustCodegenDecorator { "crate-type" to listOf("cdylib"), ), ) + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ServerCodegenContext::class.java) } /** @@ -76,6 +79,9 @@ class PythonExportModuleDecorator : RustCodegenDecorator { val serviceShapes = Walker(codegenContext.model).walkShapes(service) PythonServerModuleGenerator(codegenContext, rustCrate, serviceShapes).render() } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ServerCodegenContext::class.java) } /** @@ -91,6 +97,9 @@ class PubUsePythonTypesDecorator : RustCodegenDecorator { ): List { return baseCustomizations + PubUsePythonTypes(codegenContext) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ServerCodegenContext::class.java) } val DECORATORS = listOf( @@ -106,9 +115,3 @@ val DECORATORS = listOf( // Render the Python shared library export. PythonExportModuleDecorator(), ) - -// Combined codegen decorator for Python services. -class PythonServerCodegenDecorator : CombinedCodegenDecorator(DECORATORS) { - override val name: String = "PythonServerCodegenDecorator" - override val order: Byte = -1 -} diff --git a/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator b/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator deleted file mode 100644 index 13a1fd23750a07674e326fc691799e22d87c2dd3..0000000000000000000000000000000000000000 --- a/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator +++ /dev/null @@ -1,6 +0,0 @@ -# -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: Apache-2.0 -# - -software.amazon.smithy.rust.codegen.server.python.smithy.customizations.PythonServerCodegenDecorator 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 628d7a2843d9c2059efd57ed18448fd0e7b5c994..402e791129c8f81e2c5a739cb199f65d8652ed2c 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,6 +13,7 @@ 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.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.transformers.allErrors @@ -37,6 +38,9 @@ class AddInternalServerErrorToInfallibleOperationsDecorator : RustCodegenDecorat 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) } /** @@ -63,6 +67,9 @@ class AddInternalServerErrorToAllOperationsDecorator : RustCodegenDecorator): 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 c3925d0fc1232e7d236397077826d346e9ce81ad..e3ae4f23c4e0ffe5a707ce993f048b2bd7ea043b 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 @@ -6,6 +6,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.customizations import software.amazon.smithy.rust.codegen.rustlang.Feature +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.smithy.customizations.AllowLintsGenerator @@ -35,4 +36,7 @@ class ServerRequiredCustomizations : RustCodegenDecorator // Add rt-tokio feature for `ByteStream::from_path` rustCrate.mergeFeature(Feature("rt-tokio", true, listOf("aws-smithy-http/rt-tokio"))) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ServerCodegenContext::class.java) } 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 new file mode 100644 index 0000000000000000000000000000000000000000..285e00ab6c2e5244388af87e5e9cfa950a0fadd6 --- /dev/null +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/CombinedCodegenDecoratorTest.kt @@ -0,0 +1,35 @@ +/* + * 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.server.smithy.customizations.ServerRequiredCustomizations +import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext + +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/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/ClientCustomizations.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/ClientCustomizations.kt index 168bb506f5ce54d93f728888e9903071789673a8..029587ea80c06691b0b0d6ddee87310dc3654d94 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/ClientCustomizations.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/ClientCustomizations.kt @@ -6,6 +6,7 @@ package software.amazon.smithy.rust.codegen.smithy.customizations import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization @@ -20,4 +21,7 @@ class ClientCustomizations : RustCodegenDecorator { codegenContext: ClientCodegenContext, baseCustomizations: List, ): List = baseCustomizations + ClientDocsGenerator() + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/DocsRsMetadataDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/DocsRsMetadataDecorator.kt index d7aed5c18d3385e8253ec1b1bcb05744c4371400..212f4de72fbdecd5a689df25bad59076f3e16cfc 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/DocsRsMetadataDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/DocsRsMetadataDecorator.kt @@ -6,6 +6,7 @@ package software.amazon.smithy.rust.codegen.smithy.customizations import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.ManifestCustomizations @@ -56,4 +57,7 @@ class DocsRsMetadataDecorator(private val docsRsMetadataSettings: DocsRsMetadata override fun crateManifestCustomizations(codegenContext: ClientCodegenContext): ManifestCustomizations { return docsRsMetadataSettings.asMap() } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/RetryConfigDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/RetryConfigDecorator.kt index e7ff953b7d04ac99e32992f726fa5c75ebdd673d..9d83a80e60e954f2392fdc08815671fe68dfdf77 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/RetryConfigDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/RetryConfigDecorator.kt @@ -84,6 +84,9 @@ class RetryConfigDecorator : RustCodegenDecorator { ): List { return baseCustomizations + PubUseRetryConfig(codegenContext.runtimeConfig) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class RetryConfigProviderConfig(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() { diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/SleepImplDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/SleepImplDecorator.kt index cb3ad3cd621eb54642f59fd218bdb7fb18cd9ea0..126e264b397ccf635d5d648e62d61bdc1c719f0f 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/SleepImplDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/SleepImplDecorator.kt @@ -126,6 +126,9 @@ class SleepImplDecorator : RustCodegenDecorator { ): List { return baseCustomizations + SleepImplProviderConfig(codegenContext) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class SleepImplProviderConfig(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() { diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/TimeoutConfigDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/TimeoutConfigDecorator.kt index f82aa69b7df8ca3fc761e2ea96315e0713e748cf..8425e1963725897391e739bb48e2a934f1dd12d2 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/TimeoutConfigDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customizations/TimeoutConfigDecorator.kt @@ -113,6 +113,9 @@ class TimeoutConfigDecorator : RustCodegenDecorator { ): List { return baseCustomizations + TimeoutConfigProviderConfig(codegenContext) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } class TimeoutConfigProviderConfig(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() { diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NoOpEventStreamSigningDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NoOpEventStreamSigningDecorator.kt index 534368c6bf10a474f1ce582587953debc5ec1669..35bfdf9704bc038377bfc667bf2330084aae4c24 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NoOpEventStreamSigningDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NoOpEventStreamSigningDecorator.kt @@ -40,6 +40,8 @@ open class NoOpEventStreamSigningDecorator : RustCodegen codegenContext.runtimeConfig, ) } + + override fun supportsCodegenContext(clazz: Class) = true } class NoOpEventStreamSigningConfig( diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RequiredCustomizations.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RequiredCustomizations.kt index c34c4301a262e54f8d90bfb2d72ecdab2d202709..659f00230ca20ef5c55bbe4f0a43dbb66a186386 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RequiredCustomizations.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RequiredCustomizations.kt @@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.smithy.customize import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.rustlang.Feature import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customizations.AllowLintsGenerator import software.amazon.smithy.rust.codegen.smithy.customizations.CrateVersionGenerator @@ -48,4 +49,7 @@ class RequiredCustomizations : RustCodegenDecorator { // Add rt-tokio feature for `ByteStream::from_path` rustCrate.mergeFeature(Feature("rt-tokio", true, listOf("aws-smithy-http/rt-tokio"))) } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt index 1981285e8b6b6f134894aa66126070e32a80dfcc..d3c4e92e8296d7a918fed1142422bcb6447ce7c1 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt @@ -68,6 +68,8 @@ interface RustCodegenDecorator { currentProtocols fun transformModel(service: ServiceShape, model: Model): Model = model + + fun supportsCodegenContext(clazz: Class): Boolean } /** @@ -75,7 +77,8 @@ interface RustCodegenDecorator { * * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. */ -open class CombinedCodegenDecorator(decorators: List>) : RustCodegenDecorator { +open class CombinedCodegenDecorator(decorators: List>) : + RustCodegenDecorator { private val orderedDecorators = decorators.sortedBy { it.order } override val name: String get() = "MetaDecorator" @@ -137,6 +140,10 @@ open class CombinedCodegenDecorator(decorators: List): Boolean = + // `CombinedCodegenDecorator` can work with all types of codegen context. + CoreCodegenContext::class.java.isAssignableFrom(clazz) + companion object { inline fun fromClasspath( context: PluginContext, @@ -147,31 +154,46 @@ open class CombinedCodegenDecorator(decorators: List(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. The most elegant way to filter is arguably by checking if we can cast the loaded - // class to what we want. + // 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 { - try { - it as RustCodegenDecorator - true - } catch (e: ClassCastException) { - false - } + val clazz = T::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 } - .toList() - return CombinedCodegenDecorator(decorators + extras) - } } } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/client/FluentClientDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/client/FluentClientDecorator.kt index eaf3c066cd416879f6c457b7ce4bca427546eb20..99fbfb71e4bfa71cbc5dbfeb3fc64b5e09dc8e4c 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/client/FluentClientDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/client/FluentClientDecorator.kt @@ -99,6 +99,9 @@ class FluentClientDecorator : RustCodegenDecorator { } } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } sealed class FluentClientSection(name: String) : Section(name) { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/customizations/HttpVersionListGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/customizations/HttpVersionListGeneratorTest.kt index 4dff1e5c4f512dfdbafb9a88a3a37d8784032786..45c5bae1ac4daf7a73bbed417bce17849f03e9d9 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/customizations/HttpVersionListGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/customizations/HttpVersionListGeneratorTest.kt @@ -13,6 +13,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.RustCrate @@ -84,6 +85,9 @@ internal class HttpVersionListGeneratorTest { ) } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } val combinedCodegenDecorator: CombinedCodegenDecorator = CombinedCodegenDecorator.fromClasspath(ctx, RequiredCustomizations()).withDecorator(testWriter) @@ -146,6 +150,9 @@ internal class HttpVersionListGeneratorTest { ) } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } val combinedCodegenDecorator: CombinedCodegenDecorator = @@ -228,6 +235,9 @@ internal class HttpVersionListGeneratorTest { ) } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } val combinedCodegenDecorator: CombinedCodegenDecorator = diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EndpointTraitBindingsTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EndpointTraitBindingsTest.kt index e141f64b54547e0fc7dfa04974a355ba3d8c3109..ad845b9cc8c465c89d31e324e982e513f8eac4e8 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EndpointTraitBindingsTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EndpointTraitBindingsTest.kt @@ -15,6 +15,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.smithy.RustCrate import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.customize.RequiredCustomizations @@ -171,6 +172,9 @@ internal class EndpointTraitBindingsTest { ) } } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ClientCodegenContext::class.java) } val combinedCodegenDecorator: CombinedCodegenDecorator = CombinedCodegenDecorator.fromClasspath(ctx, RequiredCustomizations()).withDecorator(codegenDecorator) diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/protocol/ProtocolTestGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/protocol/ProtocolTestGeneratorTest.kt index ea39c74804cf194e9e52ca8841b6a8608feabf5a..e65c558dc4df62ecd2ba4635b96c1c212003735e 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/protocol/ProtocolTestGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/protocol/ProtocolTestGeneratorTest.kt @@ -231,6 +231,9 @@ class ProtocolTestGeneratorTest { ): 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) }, ) visitor.execute()