Skip to content

Commit

Permalink
Add more client re-exports (#2437)
Browse files Browse the repository at this point in the history
* Add more client re-exports

This commit adds more client re-exports proposed in
#1759.
Specifically, it re-exports the following types:
- `aws_smithy_http::body::SdkBody`
- `aws_smithy_http::byte_stream::error::Error`
- `aws_smithy_http::operation::{Request, Response}`

* Update CHANGELOG.next.toml

---------

Co-authored-by: Yuki Saito <[email protected]>
  • Loading branch information
ysaito1001 and ysaito1001 authored Mar 9, 2023
1 parent f474294 commit 7f4152c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,15 @@ message = "Fix `FilterByOperationName` plugin. This previous caused services wit
references = ["smithy-rs#2441"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "server" }
author = "hlbarber"

[[aws-sdk-rust]]
message = "Add more client re-exports. Specifically, it re-exports `aws_smithy_http::body::SdkBody`, `aws_smithy_http::byte_stream::error::Error`, and `aws_smithy_http::operation::{Request, Response}`."
references = ["smithy-rs#2437", "aws-sdk-rust#600"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "ysaito1001"

[[smithy-rs]]
message = "Add more client re-exports. Specifically, it re-exports `aws_smithy_http::body::SdkBody`, `aws_smithy_http::byte_stream::error::Error`, and `aws_smithy_http::operation::{Request, Response}`."
references = ["smithy-rs#2437", "aws-sdk-rust#600"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" }
author = "ysaito1001"
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ class CustomizableOperationGenerator(
rustTemplate(
"""
pub use #{Operation};
pub use #{Request};
pub use #{Response};
pub use #{ClassifyRetry};
pub use #{RetryKind};
""",
"Operation" to smithyHttp.resolve("operation::Operation"),
"Request" to smithyHttp.resolve("operation::Request"),
"Response" to smithyHttp.resolve("operation::Response"),
"ClassifyRetry" to smithyHttp.resolve("retry::ClassifyRetry"),
"RetryKind" to smithyTypes.resolve("retry::RetryKind"),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import software.amazon.smithy.rust.codegen.core.util.letIf
private data class PubUseType(
val type: RuntimeType,
val shouldExport: (Model) -> Boolean,
val alias: String? = null,
)

/** Returns true if the model has normal streaming operations (excluding event streams) */
Expand Down Expand Up @@ -48,7 +49,10 @@ private fun hasBlobs(model: Model): Boolean = structUnionMembersMatchPredicate(m
private fun hasDateTimes(model: Model): Boolean = structUnionMembersMatchPredicate(model, Shape::isTimestampShape)

/** Returns a list of types that should be re-exported for the given model */
internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<RuntimeType> {
internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<RuntimeType> =
pubUseTypesThatShouldBeExported(codegenContext, model).map { it.type }

private fun pubUseTypesThatShouldBeExported(codegenContext: CodegenContext, model: Model): List<PubUseType> {
val runtimeConfig = codegenContext.runtimeConfig
return (
listOf(
Expand All @@ -58,16 +62,25 @@ internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<Run
listOf(
PubUseType(http.resolve("byte_stream::ByteStream"), ::hasStreamingOperations),
PubUseType(http.resolve("byte_stream::AggregatedBytes"), ::hasStreamingOperations),
PubUseType(http.resolve("byte_stream::error::Error"), ::hasStreamingOperations, "ByteStreamError"),
PubUseType(http.resolve("body::SdkBody"), ::hasStreamingOperations),
)
}
).filter { pubUseType -> pubUseType.shouldExport(model) }.map { it.type }
).filter { pubUseType -> pubUseType.shouldExport(model) }
}

/** Adds re-export statements for Smithy primitives */
fun pubUseSmithyPrimitives(codegenContext: CodegenContext, model: Model): Writable = writable {
val types = pubUseTypes(codegenContext, model)
val types = pubUseTypesThatShouldBeExported(codegenContext, model)
if (types.isNotEmpty()) {
types.forEach { type -> rust("pub use #T;", type) }
types.forEach {
val useStatement = if (it.alias == null) {
"pub use #T;"
} else {
"pub use #T as ${it.alias};"
}
rust(useStatement, it.type)
}
}
}

Expand All @@ -77,14 +90,15 @@ fun pubUseSmithyErrorTypes(codegenContext: CodegenContext): Writable = writable
val reexports = listOf(
listOf(
RuntimeType.smithyHttp(runtimeConfig).let { http ->
PubUseType(http.resolve("result::SdkError")) { true }
PubUseType(http.resolve("result::SdkError"), { _ -> true })
},
),
RuntimeType.smithyTypes(runtimeConfig).let { types ->
listOf(PubUseType(types.resolve("error::display::DisplayErrorContext")) { true })
listOf(PubUseType(types.resolve("error::display::DisplayErrorContext"), { _ -> true }))
// Only re-export `ProvideErrorMetadata` for clients
.letIf(codegenContext.target == CodegenTarget.CLIENT) { list ->
list + listOf(PubUseType(types.resolve("error::metadata::ProvideErrorMetadata")) { true })
list +
listOf(PubUseType(types.resolve("error::metadata::ProvideErrorMetadata"), { _ -> true }))
}
},
).flatten()
Expand Down

0 comments on commit 7f4152c

Please sign in to comment.