Unverified Commit e4e9a1d3 authored by Zelda Hessler's avatar Zelda Hessler Committed by GitHub
Browse files

fix: move smithy config customization into the smithy code generator (#1599)



* fix: move smithy config customization into the smithy code generator

* add: CHANGELOG entry

* update: tests

* fix: config codegen tests

* update: hide config fields and provide accessors instead

* fix: usage of private fields
add: accessor docs

* format: run ktlint

* fix: update more old code

* update: clone sleep_impl since that's why people access it

* update: CHANGELOG.next.toml

* Update CHANGELOG.next.toml

Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>

Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent 0f47f69d
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -11,6 +11,42 @@
# meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"}
# author = "rcoh"

[[aws-sdk-rust]]
message = """
Service configs are now generated with new accessors for:
- `Config::retry_config()` - Returns a reference to the inner retry configuration.
- `Config::timeout_config()` - Returns a reference to the inner timeout configuration.
- `Config::sleep_impl()` - Returns a clone of the inner async sleep implementation.

Previously, these were only accessible through `SdkConfig`.
"""
references = ["smithy-rs#1598"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "Velfi"

[[smithy-rs]]
message = """
Previously, the config customizations that added functionality related to retry configs, timeout configs, and the 
async sleep impl were defined in the smithy codegen module but were being loaded in the AWS codegen module. They
have now been updated to be loaded during smithy codegen. The affected classes are all defined in the
`software.amazon.smithy.rust.codegen.smithy.customizations` module of smithy codegen.` This change does not affect
the generated code.

These classes have been removed:
- `RetryConfigDecorator`
- `SleepImplDecorator`
- `TimeoutConfigDecorator`

These classes have been renamed:
- `RetryConfigProviderConfig` is now `RetryConfigProviderCustomization`
- `PubUseRetryConfig` is now `PubUseRetryConfigGenerator`
- `SleepImplProviderConfig` is now `SleepImplProviderCustomization`
- `TimeoutConfigProviderConfig` is now `TimeoutConfigProviderCustomization`
"""
references = ["smithy-rs#1598"]
meta = { "breaking" = true, "tada" = true, "bug" = false, "target" = "client" }
author = "Velfi"

[[smithy-rs]]
message = "Add codegen version to generated package metadata"
references = ["smithy-rs#1612"]
+0 −8
Original line number Diff line number Diff line
@@ -8,9 +8,6 @@ package software.amazon.smithy.rustsdk
import software.amazon.smithy.rust.codegen.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.smithy.customizations.DocsRsMetadataDecorator
import software.amazon.smithy.rust.codegen.smithy.customizations.DocsRsMetadataSettings
import software.amazon.smithy.rust.codegen.smithy.customizations.RetryConfigDecorator
import software.amazon.smithy.rust.codegen.smithy.customizations.SleepImplDecorator
import software.amazon.smithy.rust.codegen.smithy.customizations.TimeoutConfigDecorator
import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator
import software.amazon.smithy.rustsdk.customize.apigateway.ApiGatewayDecorator
import software.amazon.smithy.rustsdk.customize.auth.DisabledAuthDecorator
@@ -37,11 +34,6 @@ val DECORATORS = listOf(
    AwsPresigningDecorator(),
    AwsReadmeDecorator(),

    // Smithy specific decorators
    RetryConfigDecorator(),
    SleepImplDecorator(),
    TimeoutConfigDecorator(),

    // Service specific decorators
    DisabledAuthDecorator(),
    ApiGatewayDecorator(),
+6 −8
Original line number Diff line number Diff line
@@ -138,15 +138,14 @@ private class AwsFluentClientExtensions(types: Types) {
                    C: #{SmithyConnector}<Error = E> + Send + 'static,
                    E: Into<#{ConnectorError}>,
                {
                    let retry_config = conf.retry_config.as_ref().cloned().unwrap_or_default();
                    let timeout_config = conf.timeout_config.as_ref().cloned().unwrap_or_default();
                    let sleep_impl = conf.sleep_impl.clone();
                    let retry_config = conf.retry_config().cloned().unwrap_or_default();
                    let timeout_config = conf.timeout_config().cloned().unwrap_or_default();
                    let mut builder = #{aws_smithy_client}::Builder::new()
                        .connector(#{DynConnector}::new(conn))
                        .middleware(#{DynMiddleware}::new(#{Middleware}::new()));
                    builder.set_retry_config(retry_config.into());
                    builder.set_timeout_config(timeout_config);
                    if let Some(sleep_impl) = sleep_impl {
                    if let Some(sleep_impl) = conf.sleep_impl() {
                        builder.set_sleep_impl(Some(sleep_impl));
                    }
                    let client = builder.build();
@@ -162,16 +161,15 @@ private class AwsFluentClientExtensions(types: Types) {
                /// Creates a new client from the service [`Config`](crate::Config).
                ##[cfg(any(feature = "rustls", feature = "native-tls"))]
                pub fn from_conf(conf: crate::Config) -> Self {
                    let retry_config = conf.retry_config.as_ref().cloned().unwrap_or_default();
                    let timeout_config = conf.timeout_config.as_ref().cloned().unwrap_or_default();
                    let sleep_impl = conf.sleep_impl.clone();
                    let retry_config = conf.retry_config().cloned().unwrap_or_default();
                    let timeout_config = conf.timeout_config().cloned().unwrap_or_default();
                    let mut builder = #{aws_smithy_client}::Builder::dyn_https()
                        .middleware(#{DynMiddleware}::new(#{Middleware}::new()));
                    builder.set_retry_config(retry_config.into());
                    builder.set_timeout_config(timeout_config);
                    // the builder maintains a try-state. To avoid suppressing the warning when sleep is unset,
                    // only set it if we actually have a sleep impl.
                    if let Some(sleep_impl) = sleep_impl {
                    if let Some(sleep_impl) = conf.sleep_impl() {
                        builder.set_sleep_impl(Some(sleep_impl));
                    }
                    let client = builder.build();
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class SdkConfigDecorator : RustCodegenDecorator<ClientCodegenContext> {
                        builder.set_endpoint_resolver(input.endpoint_resolver().clone());
                        builder.set_retry_config(input.retry_config().cloned());
                        builder.set_timeout_config(input.timeout_config().cloned());
                        builder.set_sleep_impl(input.sleep_impl().clone());
                        builder.set_sleep_impl(input.sleep_impl());
                        builder.set_credentials_provider(input.credentials_provider().cloned());
                        builder.set_app_name(input.app_name().cloned());
                        builder
+12 −28
Original line number Diff line number Diff line
@@ -9,11 +9,9 @@ import software.amazon.smithy.rust.codegen.rustlang.Writable
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.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization
@@ -67,39 +65,25 @@ fn test_1() {
}
 */

class RetryConfigDecorator : RustCodegenDecorator<ClientCodegenContext> {
    override val name: String = "RetryConfig"
    override val order: Byte = 0

    override fun configCustomizations(
        codegenContext: ClientCodegenContext,
        baseCustomizations: List<ConfigCustomization>,
    ): List<ConfigCustomization> {
        return baseCustomizations + RetryConfigProviderConfig(codegenContext)
    }

    override fun libRsCustomizations(
        codegenContext: ClientCodegenContext,
        baseCustomizations: List<LibRsCustomization>,
    ): List<LibRsCustomization> {
        return baseCustomizations + PubUseRetryConfig(codegenContext.runtimeConfig)
    }

    override fun supportsCodegenContext(clazz: Class<out CoreCodegenContext>): Boolean =
        clazz.isAssignableFrom(ClientCodegenContext::class.java)
}

class RetryConfigProviderConfig(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() {
class RetryConfigProviderCustomization(coreCodegenContext: CoreCodegenContext) : ConfigCustomization() {
    private val retryConfig = smithyTypesRetry(coreCodegenContext.runtimeConfig)
    private val moduleUseName = coreCodegenContext.moduleUseName()
    private val codegenScope = arrayOf("RetryConfig" to retryConfig.member("RetryConfig"))
    override fun section(section: ServiceConfig) = writable {
        when (section) {
            is ServiceConfig.ConfigStruct -> rustTemplate(
                "pub(crate) retry_config: Option<#{RetryConfig}>,",
                "retry_config: Option<#{RetryConfig}>,",
                *codegenScope,
            )
            is ServiceConfig.ConfigImpl -> emptySection
            is ServiceConfig.ConfigImpl -> {
                rustTemplate(
                    """
                    /// Return a reference to the retry configuration contained in this config, if any.
                    pub fn retry_config(&self) -> Option<&#{RetryConfig}> { self.retry_config.as_ref() }
                    """,
                    *codegenScope,
                )
            }
            is ServiceConfig.BuilderStruct ->
                rustTemplate("retry_config: Option<#{RetryConfig}>,", *codegenScope)
            ServiceConfig.BuilderImpl ->
@@ -152,7 +136,7 @@ class RetryConfigProviderConfig(coreCodegenContext: CoreCodegenContext) : Config
    }
}

class PubUseRetryConfig(private val runtimeConfig: RuntimeConfig) : LibRsCustomization() {
class PubUseRetryConfigGenerator(private val runtimeConfig: RuntimeConfig) : LibRsCustomization() {
    override fun section(section: LibRsSection): Writable {
        return when (section) {
            is LibRsSection.Body -> writable {
Loading