-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set a default for idempotency_token in the service runtime plugin
This commit also refactors idempotency tokens to be centralized in a decorator
- Loading branch information
Showing
11 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ware/amazon/smithy/rust/codegen/client/smithy/customizations/IdempotencyTokenDecorator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.client.smithy.customizations | ||
|
||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginSection | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.IdempotencyTokenProviderCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.needsIdempotencyToken | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType | ||
import software.amazon.smithy.rust.codegen.core.util.extendIf | ||
|
||
class IdempotencyTokenDecorator : ClientCodegenDecorator { | ||
override val name: String = "IdempotencyToken" | ||
override val order: Byte = 0 | ||
|
||
private fun enabled(ctx: ClientCodegenContext) = ctx.serviceShape.needsIdempotencyToken(ctx.model) | ||
override fun configCustomizations( | ||
codegenContext: ClientCodegenContext, | ||
baseCustomizations: List<ConfigCustomization>, | ||
): List<ConfigCustomization> = baseCustomizations.extendIf(enabled(codegenContext)) { | ||
IdempotencyTokenProviderCustomization(codegenContext) | ||
} | ||
|
||
override fun operationCustomizations( | ||
codegenContext: ClientCodegenContext, | ||
operation: OperationShape, | ||
baseCustomizations: List<OperationCustomization>, | ||
): List<OperationCustomization> { | ||
return baseCustomizations + IdempotencyTokenGenerator(codegenContext, operation) | ||
} | ||
|
||
override fun serviceRuntimePluginCustomizations( | ||
codegenContext: ClientCodegenContext, | ||
baseCustomizations: List<ServiceRuntimePluginCustomization>, | ||
): List<ServiceRuntimePluginCustomization> { | ||
return baseCustomizations.extendIf(enabled(codegenContext)) { | ||
object : ServiceRuntimePluginCustomization() { | ||
override fun section(section: ServiceRuntimePluginSection) = writable { | ||
if (section is ServiceRuntimePluginSection.AdditionalConfig) { | ||
section.putConfigValue(this, defaultTokenProvider((codegenContext.runtimeConfig))) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun defaultTokenProvider(runtimeConfig: RuntimeConfig) = | ||
writable { rust("#T()", RuntimeType.idempotencyToken(runtimeConfig).resolve("default_provider")) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters