Unverified Commit afa9edbe authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Simplify `AdHocSection` (#2272)



Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
parent 17f9a873
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -15,10 +15,10 @@ 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.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
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.smithy.customize.Section
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization

class CredentialsCacheDecorator : ClientCodegenDecorator {
    override val name: String = "CredentialsCache"
@@ -38,12 +38,10 @@ class CredentialsCacheDecorator : ClientCodegenDecorator {
        return baseCustomizations + CredentialsCacheFeature(codegenContext.runtimeConfig)
    }

    override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> =
    override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> =
        listOf(
            SdkConfigSection.create { section ->
                writable {
            adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
                rust("${section.serviceConfigBuilder}.set_credentials_cache(${section.sdkConfig}.credentials_cache().cloned());")
                }
            },
        )
}
+5 −7
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable
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.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection
import software.amazon.smithy.rust.codegen.core.smithy.customize.Section
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection

@@ -40,12 +40,10 @@ class CredentialsProviderDecorator : ClientCodegenDecorator {
        return baseCustomizations + PubUseCredentials(codegenContext.runtimeConfig)
    }

    override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> =
    override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> =
        listOf(
            SdkConfigSection.create { section ->
                writable {
            adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
                rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider().cloned());")
                }
            },
        )

+4 −5
Original line number Diff line number Diff line
@@ -29,8 +29,7 @@ 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.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection
import software.amazon.smithy.rust.codegen.core.smithy.customize.Section
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
import software.amazon.smithy.rust.codegen.core.util.PANIC
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.extendIf
@@ -71,7 +70,7 @@ fun Model.sdkConfigSetter(
    serviceId: ShapeId,
    builtInSrc: Parameter,
    configParameterNameOverride: String?,
): Pair<AdHocSection<*>, (Section) -> Writable>? {
): AdHocCustomization? {
    val builtIn = loadBuiltIn(serviceId, builtInSrc) ?: return null
    val fieldName = configParameterNameOverride ?: builtIn.name.rustName()

@@ -79,7 +78,7 @@ fun Model.sdkConfigSetter(
        ParameterType.STRING -> writable { rust("|s|s.to_string()") }
        ParameterType.BOOLEAN -> null
    }
    return SdkConfigSection.copyField(fieldName, map)
    return SdkConfigCustomization.copyField(fieldName, map)
}

/**
@@ -99,7 +98,7 @@ fun decoratorForBuiltIn(
        private fun rulesetContainsBuiltIn(codegenContext: ClientCodegenContext) =
            codegenContext.getBuiltIn(builtIn) != null

        override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> {
        override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
            return listOfNotNull(
                codegenContext.model.sdkConfigSetter(codegenContext.serviceShape.id, builtIn, clientParam?.name),
            )
+10 −12
Original line number Diff line number Diff line
@@ -20,10 +20,10 @@ 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.AdHocSection
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
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.smithy.customize.Section
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.core.util.dq
@@ -108,10 +108,9 @@ class RegionDecorator : ClientCodegenDecorator {
        return baseCustomizations.extendIf(usesRegion(codegenContext)) { PubUseRegion(codegenContext.runtimeConfig) }
    }

    override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> {
    override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
        return usesRegion(codegenContext).thenSingletonListOf {
            SdkConfigSection.create { section ->
                {
            adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
                rust(
                    """
                    ${section.serviceConfigBuilder} =
@@ -121,7 +120,6 @@ class RegionDecorator : ClientCodegenDecorator {
            }
        }
    }
    }

    override fun endpointCustomizations(codegenContext: ClientCodegenContext): List<EndpointCustomization> {
        if (!usesRegion(codegenContext)) {
+36 −37
Original line number Diff line number Diff line
@@ -11,19 +11,17 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.config.Confi
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
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.join
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.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection
import software.amazon.smithy.rust.codegen.core.smithy.customize.Section
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizations

/**
 * Section enabling linkage between `SdkConfig` and <service>::Config
 */
object SdkConfigSection : AdHocSection<SdkConfigSection.CopySdkConfigToClientConfig>("SdkConfig") {
sealed class SdkConfigSection(name: String) : AdHocSection(name) {
    /**
     * [sdkConfig]: A reference to the SDK config struct
     * [serviceConfigBuilder]: A reference (owned) to the `<service>::config::Builder` struct.
@@ -34,8 +32,13 @@ object SdkConfigSection : AdHocSection<SdkConfigSection.CopySdkConfigToClientCon
     * ```
     */
    data class CopySdkConfigToClientConfig(val sdkConfig: String, val serviceConfigBuilder: String) :
        Section("CopyConfig")
        SdkConfigSection("CopySdkConfigToClientConfig")
}

/**
 * Section enabling linkage between `SdkConfig` and <service>::Config
 */
object SdkConfigCustomization {
    /**
     * Copy a field from SDK config to service config with an optional map block.
     *
@@ -43,11 +46,11 @@ object SdkConfigSection : AdHocSection<SdkConfigSection.CopySdkConfigToClientCon
     *
     * # Examples
     * ```kotlin
     * SdkConfigSection.copyField("some_string_field") { rust("|s|s.to_to_string()") }
     * SdkConfigCustomization.copyField("some_string_field") { rust("|s|s.to_to_string()") }
     * ```
     */
    fun copyField(fieldName: String, map: Writable?) = SdkConfigSection.create { section ->
        {
    fun copyField(fieldName: String, map: Writable?) =
        adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
            val mapBlock = map?.let { writable { rust(".map(#W)", it) } } ?: writable { }
            rustTemplate(
                "${section.serviceConfigBuilder}.set_$fieldName(${section.sdkConfig}.$fieldName()#{map});",
@@ -55,7 +58,6 @@ object SdkConfigSection : AdHocSection<SdkConfigSection.CopySdkConfigToClientCon
            )
        }
}
}

/**
 * SdkConfig -> <service>::Config for settings that come from generic smithy
@@ -64,10 +66,9 @@ class GenericSmithySdkConfigSettings : ClientCodegenDecorator {
    override val name: String = "GenericSmithySdkConfigSettings"
    override val order: Byte = 0

    override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> =
    override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> =
        listOf(
            SdkConfigSection.create { section ->
                writable {
            adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
                rust(
                    """
                    // resiliency
@@ -79,7 +80,6 @@ class GenericSmithySdkConfigSettings : ClientCodegenDecorator {

                    """,
                )
                }
            },
        )
}
@@ -111,8 +111,7 @@ class SdkConfigDecorator : ClientCodegenDecorator {
                impl From<&#{SdkConfig}> for Builder {
                    fn from(input: &#{SdkConfig}) -> Self {
                        let mut builder = Builder::default();
                        #{augmentBuilder}

                        #{augmentBuilder:W}

                        builder
                    }
@@ -124,15 +123,15 @@ class SdkConfigDecorator : ClientCodegenDecorator {
                    }
                }
                """,
                "augmentBuilder" to codegenContext.rootDecorator.extraSections(codegenContext)
                    .filter { (t, _) -> t is SdkConfigSection }.map { (_, sectionWriter) ->
                        sectionWriter(
                "augmentBuilder" to writable {
                    writeCustomizations(
                        codegenContext.rootDecorator.extraSections(codegenContext),
                        SdkConfigSection.CopySdkConfigToClientConfig(
                            sdkConfig = "input",
                            serviceConfigBuilder = "builder",
                        ),
                    )
                    }.join("\n"),
                },
                *codegenScope,
            )
        }
Loading