Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
rust("${section.serviceConfigBuilder}.set_credentials_cache(${section.sdkConfig}.credentials_cache().cloned());")
}
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust("${section.serviceConfigBuilder}.set_credentials_cache(${section.sdkConfig}.credentials_cache().cloned());")
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider().cloned());")
}
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider().cloned());")
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,15 +70,15 @@ 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()

val map = when (builtIn.type!!) {
ParameterType.STRING -> writable { rust("|s|s.to_string()") }
ParameterType.BOOLEAN -> null
}
return SdkConfigSection.copyField(fieldName, map)
return SdkConfigCustomization.copyField(fieldName, map)
}

/**
Expand All @@ -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),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,17 +108,15 @@ 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 ->
{
rust(
"""
${section.serviceConfigBuilder} =
${section.serviceConfigBuilder}.region(${section.sdkConfig}.region().cloned());
""",
)
}
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust(
"""
${section.serviceConfigBuilder} =
${section.serviceConfigBuilder}.region(${section.sdkConfig}.region().cloned());
""",
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -34,27 +32,31 @@ 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.
*
* This handles the common case where the field name is identical in both cases and an accessor is used.
*
* # 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});",
"map" to mapBlock,
)
}
}
}

/**
Expand All @@ -64,22 +66,20 @@ 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 {
rust(
"""
// resiliency
${section.serviceConfigBuilder}.set_retry_config(${section.sdkConfig}.retry_config().cloned());
${section.serviceConfigBuilder}.set_timeout_config(${section.sdkConfig}.timeout_config().cloned());
${section.serviceConfigBuilder}.set_sleep_impl(${section.sdkConfig}.sleep_impl());

${section.serviceConfigBuilder}.set_http_connector(${section.sdkConfig}.http_connector().cloned());

""",
)
}
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust(
"""
// resiliency
${section.serviceConfigBuilder}.set_retry_config(${section.sdkConfig}.retry_config().cloned());
${section.serviceConfigBuilder}.set_timeout_config(${section.sdkConfig}.timeout_config().cloned());
${section.serviceConfigBuilder}.set_sleep_impl(${section.sdkConfig}.sleep_impl());

${section.serviceConfigBuilder}.set_http_connector(${section.sdkConfig}.http_connector().cloned());

""",
)
},
)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -124,15 +123,15 @@ class SdkConfigDecorator : ClientCodegenDecorator {
}
}
""",
"augmentBuilder" to codegenContext.rootDecorator.extraSections(codegenContext)
.filter { (t, _) -> t is SdkConfigSection }.map { (_, sectionWriter) ->
sectionWriter(
SdkConfigSection.CopySdkConfigToClientConfig(
sdkConfig = "input",
serviceConfigBuilder = "builder",
),
)
}.join("\n"),
"augmentBuilder" to writable {
writeCustomizations(
codegenContext.rootDecorator.extraSections(codegenContext),
SdkConfigSection.CopySdkConfigToClientConfig(
sdkConfig = "input",
serviceConfigBuilder = "builder",
),
)
},
*codegenScope,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,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
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
Expand Down Expand Up @@ -56,10 +56,10 @@ class UserAgentDecorator : ClientCodegenDecorator {
return baseCustomizations + UserAgentFeature(codegenContext.runtimeConfig)
}

override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> {
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
return listOf(
SdkConfigSection.create { section ->
writable { rust("${section.serviceConfigBuilder}.set_app_name(${section.sdkConfig}.app_name().cloned());") }
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust("${section.serviceConfigBuilder}.set_app_name(${section.sdkConfig}.app_name().cloned());")
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ 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
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
import software.amazon.smithy.rust.codegen.core.util.extendIf
Expand Down Expand Up @@ -133,16 +133,14 @@ class AwsEndpointDecorator : ClientCodegenDecorator {
}
}

override fun extraSections(codegenContext: ClientCodegenContext): List<Pair<AdHocSection<*>, (Section) -> Writable>> {
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
return codegenContext.isRegionalized().thenSingletonListOf {
SdkConfigSection.create { section ->
{
rust(
"""
${section.serviceConfigBuilder}.set_aws_endpoint_resolver(${section.sdkConfig}.endpoint_resolver().clone());
""",
)
}
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
rust(
"""
${section.serviceConfigBuilder}.set_aws_endpoint_resolver(${section.sdkConfig}.endpoint_resolver().clone());
""",
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator
import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization
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
Expand Down Expand Up @@ -72,7 +72,7 @@ sealed class FluentClientSection(name: String) : Section(name) {
data class FluentClientDocs(val serviceShape: ServiceShape) : FluentClientSection("FluentClientDocs")
}

abstract class FluentClientCustomization : NamedSectionGenerator<FluentClientSection>()
abstract class FluentClientCustomization : NamedCustomization<FluentClientSection>()

class GenericFluentClient(codegenContext: CodegenContext) : FluentClientCustomization() {
private val moduleUseName = codegenContext.moduleUseName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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.NamedSectionGenerator
import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization

/**
* Add a `make_token` field to Service config. See below for the resulting generated code.
*/
class IdempotencyTokenProviderCustomization : NamedSectionGenerator<ServiceConfig>() {
class IdempotencyTokenProviderCustomization : NamedCustomization<ServiceConfig>() {
override fun section(section: ServiceConfig): Writable {
return when (section) {
is ServiceConfig.ConfigStruct -> writable {
Expand Down
Loading