-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consolidate fluent client builder implementations #668
Merged
jdisanti
merged 10 commits into
smithy-lang:main
from
jdisanti:consolidate-client-builders
Aug 27, 2021
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
43b29b3
Consolidate fluent client builder implementations
jdisanti 97b973f
Rename FluentClientDecorator to AwsFluentClientDecorator
jdisanti 2b11a1c
Remove potentially confusing Smithy details from AWS fluent clients
jdisanti 9717ccd
Fix AWS client generics issue
jdisanti 8f27f6e
fix missing template param
rcoh 74a8979
remove problematic whitespace
rcoh 8965b41
Fix Clone derivation issue
jdisanti 23564df
Fix unintentional removal of `from_conf_conn`
jdisanti e28b543
Merge remote-tracking branch 'upstream/main' into consolidate-client-…
jdisanti c5211bf
Change `Standard` to `retry`
jdisanti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
120 changes: 120 additions & 0 deletions
120
aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.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,120 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.rustsdk | ||
|
||
import software.amazon.smithy.rust.codegen.rustlang.Attribute | ||
import software.amazon.smithy.rust.codegen.rustlang.CargoDependency | ||
import software.amazon.smithy.rust.codegen.rustlang.Feature | ||
import software.amazon.smithy.rust.codegen.rustlang.RustMetadata | ||
import software.amazon.smithy.rust.codegen.rustlang.RustModule | ||
import software.amazon.smithy.rust.codegen.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.rustlang.asType | ||
import software.amazon.smithy.rust.codegen.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.rustlang.rustBlock | ||
import software.amazon.smithy.rust.codegen.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig | ||
import software.amazon.smithy.rust.codegen.smithy.RuntimeType | ||
import software.amazon.smithy.rust.codegen.smithy.RustCrate | ||
import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.smithy.generators.ClientGenerics | ||
import software.amazon.smithy.rust.codegen.smithy.generators.FluentClientGenerator | ||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization | ||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsSection | ||
import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig | ||
|
||
private class Types(runtimeConfig: RuntimeConfig) { | ||
private val smithyClientDep = CargoDependency.SmithyClient(runtimeConfig).copy(optional = true) | ||
private val awsHyperDep = runtimeConfig.awsRuntimeDependency("aws-hyper").copy(optional = true) | ||
|
||
val awsHyper = awsHyperDep.asType() | ||
|
||
val AwsMiddleware = RuntimeType("AwsMiddleware", awsHyperDep, "aws_hyper") | ||
val DynConnector = RuntimeType("DynConnector", smithyClientDep, "smithy_client::erase") | ||
val Standard = RuntimeType("Standard", smithyClientDep, "smithy_client::retry") | ||
} | ||
|
||
class AwsFluentClientDecorator : RustCodegenDecorator { | ||
override val name: String = "FluentClient" | ||
override val order: Byte = 0 | ||
|
||
override fun extras(protocolConfig: ProtocolConfig, rustCrate: RustCrate) { | ||
val types = Types(protocolConfig.runtimeConfig) | ||
val module = RustMetadata(additionalAttributes = listOf(Attribute.Cfg.feature("client")), public = true) | ||
rustCrate.withModule(RustModule("client", module)) { writer -> | ||
FluentClientGenerator( | ||
protocolConfig, | ||
includeSmithyGenericClientDocs = false, | ||
generics = ClientGenerics( | ||
connectorDefault = "#{AwsFluentClient_DynConnector}", | ||
middlewareDefault = "#{AwsFluentClient_AwsMiddleware}", | ||
retryDefault = "#{AwsFluentClient_Standard}", | ||
codegenScope = listOf( | ||
"AwsFluentClient_AwsMiddleware" to types.AwsMiddleware, | ||
"AwsFluentClient_DynConnector" to types.DynConnector, | ||
"AwsFluentClient_Standard" to types.Standard, | ||
) | ||
), | ||
).render(writer) | ||
AwsFluentClientExtensions(types).render(writer) | ||
} | ||
val awsHyper = "aws-hyper" | ||
rustCrate.addFeature(Feature("client", true, listOf(awsHyper, "smithy-client"))) | ||
rustCrate.addFeature(Feature("rustls", default = true, listOf("$awsHyper/rustls"))) | ||
rustCrate.addFeature(Feature("native-tls", default = false, listOf("$awsHyper/native-tls"))) | ||
} | ||
|
||
override fun libRsCustomizations( | ||
protocolConfig: ProtocolConfig, | ||
baseCustomizations: List<LibRsCustomization> | ||
): List<LibRsCustomization> { | ||
return baseCustomizations + object : LibRsCustomization() { | ||
override fun section(section: LibRsSection) = when (section) { | ||
is LibRsSection.Body -> writable { | ||
Attribute.Cfg.feature("client").render(this) | ||
rust("pub use client::Client;") | ||
} | ||
else -> emptySection | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class AwsFluentClientExtensions(private val types: Types) { | ||
fun render(writer: RustWriter) { | ||
writer.rustBlock("impl<C> Client<C, aws_hyper::AwsMiddleware, smithy_client::retry::Standard>") { | ||
rustTemplate( | ||
""" | ||
pub fn from_conf_conn(conf: crate::Config, conn: C) -> Self { | ||
let client = #{aws_hyper}::Client::new(conn); | ||
Self { handle: std::sync::Arc::new(Handle { client, conf }) } | ||
} | ||
""", | ||
"aws_hyper" to types.awsHyper, | ||
) | ||
} | ||
writer.rustBlock("impl Client<smithy_client::erase::DynConnector, aws_hyper::AwsMiddleware, smithy_client::retry::Standard>") { | ||
rustTemplate( | ||
""" | ||
##[cfg(any(feature = "rustls", feature = "native-tls"))] | ||
pub async fn from_env() -> Self { | ||
// backwards compatibility shim | ||
use aws_types::region::ProvideRegion; | ||
let region = aws_types::region::default_provider().region().await; | ||
Self::from_conf(crate::Config::builder().region(region).build()) | ||
} | ||
|
||
##[cfg(any(feature = "rustls", feature = "native-tls"))] | ||
pub fn from_conf(conf: crate::Config) -> Self { | ||
let client = #{aws_hyper}::Client::https(); | ||
Self { handle: std::sync::Arc::new(Handle { client, conf }) } | ||
} | ||
""", | ||
"aws_hyper" to types.awsHyper, | ||
) | ||
} | ||
} | ||
} |
227 changes: 0 additions & 227 deletions
227
aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/FluentClientGenerator.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: probably clearer to have this be the retry module so that it's
#{retry}::Standard
which is much clearer than justStandard