mirror of https://github.com/smithy-lang/smithy-rs
Examples for Config builder region function (#1008)
* region function documented * Example changelogs back * Solve doc error * text fixes (still fails) * added back changelog items accidently removed * refactor and fix test * fix test Co-authored-by: Russell Cohen <rcoh@amazon.com>
This commit is contained in:
parent
11de3f668d
commit
1992bf3fa6
|
@ -74,3 +74,9 @@ message = "aws_types::Config is now `Clone`"
|
|||
meta = { "breaking" = false, "tada" = false, "bug" = false }
|
||||
references = ["smithy-rs#1002", "aws-sdk-rust#352"]
|
||||
author = "rcoh"
|
||||
|
||||
[[aws-sdk-rust]]
|
||||
message = "Example for Config builder region function added"
|
||||
references = ["smithy-rs#670"]
|
||||
meta = { "breaking" = false, "tada" = false, "bug" = false }
|
||||
author = "Jacco"
|
||||
|
|
|
@ -77,7 +77,7 @@ class RegionDecorator : RustCodegenDecorator {
|
|||
codegenContext: CodegenContext,
|
||||
baseCustomizations: List<ConfigCustomization>
|
||||
): List<ConfigCustomization> {
|
||||
return baseCustomizations + RegionProviderConfig(codegenContext.runtimeConfig)
|
||||
return baseCustomizations + RegionProviderConfig(codegenContext)
|
||||
}
|
||||
|
||||
override fun operationCustomizations(
|
||||
|
@ -96,8 +96,9 @@ class RegionDecorator : RustCodegenDecorator {
|
|||
}
|
||||
}
|
||||
|
||||
class RegionProviderConfig(runtimeConfig: RuntimeConfig) : ConfigCustomization() {
|
||||
private val region = region(runtimeConfig)
|
||||
class RegionProviderConfig(codegenContext: CodegenContext) : ConfigCustomization() {
|
||||
private val region = region(codegenContext.runtimeConfig)
|
||||
private val moduleUseName = codegenContext.moduleUseName()
|
||||
private val codegenScope = arrayOf("Region" to region.member("Region"))
|
||||
override fun section(section: ServiceConfig) = writable {
|
||||
when (section) {
|
||||
|
@ -109,6 +110,16 @@ class RegionProviderConfig(runtimeConfig: RuntimeConfig) : ConfigCustomization()
|
|||
rustTemplate(
|
||||
"""
|
||||
/// Sets the AWS region to use when making requests.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```no_run
|
||||
/// use aws_types::region::Region;
|
||||
/// use $moduleUseName::config::{Builder, Config};
|
||||
///
|
||||
/// let config = $moduleUseName::Config::builder()
|
||||
/// .region(Region::new("us-east-1"))
|
||||
/// .build();
|
||||
/// ```
|
||||
pub fn region(mut self, region: impl Into<Option<#{Region}>>) -> Self {
|
||||
self.region = region.into();
|
||||
self
|
||||
|
|
|
@ -6,11 +6,15 @@
|
|||
package software.amazon.smithy.rustsdk
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import software.amazon.smithy.rust.codegen.testutil.TestWorkspace
|
||||
import software.amazon.smithy.rust.codegen.testutil.rustSettings
|
||||
import software.amazon.smithy.rust.codegen.testutil.validateConfigCustomizations
|
||||
|
||||
internal class RegionProviderConfigTest {
|
||||
@Test
|
||||
fun `generates a valid config`() {
|
||||
validateConfigCustomizations(RegionProviderConfig(AwsTestRuntimeConfig))
|
||||
val project = TestWorkspace.testProject()
|
||||
val codegenContext = awsTestCodegenContext().copy(settings = project.rustSettings())
|
||||
validateConfigCustomizations(RegionProviderConfig(codegenContext), project)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,11 @@
|
|||
|
||||
package software.amazon.smithy.rustsdk
|
||||
|
||||
import software.amazon.smithy.model.Model
|
||||
import software.amazon.smithy.rust.codegen.smithy.RuntimeCrateLocation
|
||||
import software.amazon.smithy.rust.codegen.testutil.TestRuntimeConfig
|
||||
import software.amazon.smithy.rust.codegen.testutil.asSmithyModel
|
||||
import software.amazon.smithy.rust.codegen.testutil.testCodegenContext
|
||||
import java.io.File
|
||||
|
||||
// In aws-sdk-codegen, the working dir when gradle runs tests is actually `./aws`. So, to find the smithy runtime, we need
|
||||
|
@ -18,3 +21,6 @@ val AwsTestRuntimeConfig = TestRuntimeConfig.copy(
|
|||
RuntimeCrateLocation.Path(path.absolutePath)
|
||||
}
|
||||
)
|
||||
|
||||
fun awsTestCodegenContext(model: Model? = null) =
|
||||
testCodegenContext(model ?: "namespace test".asSmithyModel()).copy(runtimeConfig = AwsTestRuntimeConfig)
|
||||
|
|
|
@ -40,11 +40,6 @@ data class CodegenContext(
|
|||
* Smithy Protocol to generate, e.g. RestJson1
|
||||
*/
|
||||
val protocol: ShapeId,
|
||||
/**
|
||||
* The name of the cargo crate to generate e.g. `aws-sdk-s3`
|
||||
* This is loaded from the smithy-build.json during codegen.
|
||||
*/
|
||||
val moduleName: String,
|
||||
/**
|
||||
* Settings loaded from smithy-build.json
|
||||
*/
|
||||
|
@ -63,7 +58,13 @@ data class CodegenContext(
|
|||
protocol: ShapeId,
|
||||
settings: RustSettings,
|
||||
mode: CodegenMode,
|
||||
) : this(model, symbolProvider, settings.runtimeConfig, serviceShape, protocol, settings.moduleName, settings, mode)
|
||||
) : this(model, symbolProvider, settings.runtimeConfig, serviceShape, protocol, settings, mode)
|
||||
|
||||
/**
|
||||
* The name of the cargo crate to generate e.g. `aws-sdk-s3`
|
||||
* This is loaded from the smithy-build.json during codegen.
|
||||
*/
|
||||
val moduleName: String by lazy { settings.moduleName }
|
||||
|
||||
/**
|
||||
* A moduleName for a crate uses kebab-case. When you want to `use` a crate in Rust code,
|
||||
|
|
|
@ -188,7 +188,7 @@ fun TestWriterDelegator.compileAndTest(runClippy: Boolean = false) {
|
|||
}
|
||||
}
|
||||
|
||||
fun TestWriterDelegator.rustSettings(stubModel: Model) =
|
||||
fun TestWriterDelegator.rustSettings(stubModel: Model = "namespace test".asSmithyModel()) =
|
||||
RustSettings(
|
||||
ShapeId.from("fake#Fake"),
|
||||
"test_${baseDir.toFile().nameWithoutExtension}",
|
||||
|
|
|
@ -67,5 +67,6 @@ fun stubConfigProject(customization: ConfigCustomization, project: TestWriterDel
|
|||
"""
|
||||
)
|
||||
}
|
||||
project.lib { it.rust("pub use config::Config;") }
|
||||
return project
|
||||
}
|
||||
|
|
|
@ -84,7 +84,6 @@ fun testCodegenContext(
|
|||
TestRuntimeConfig,
|
||||
serviceShape ?: ServiceShape.builder().version("test").id("test#Service").build(),
|
||||
ShapeId.from("test#Protocol"),
|
||||
settings.moduleName,
|
||||
settings, mode
|
||||
)
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@ class EventStreamUnmarshallerGeneratorTest {
|
|||
TestRuntimeConfig,
|
||||
test.serviceShape,
|
||||
ShapeId.from(testCase.protocolShapeId),
|
||||
"test",
|
||||
testRustSettings(test.model),
|
||||
mode = testCase.mode
|
||||
)
|
||||
|
|
|
@ -32,7 +32,6 @@ class EventStreamMarshallerGeneratorTest {
|
|||
TestRuntimeConfig,
|
||||
test.serviceShape,
|
||||
ShapeId.from(testCase.protocolShapeId),
|
||||
"test",
|
||||
testRustSettings(test.model),
|
||||
mode = testCase.mode
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue