Unverified Commit a15ce735 authored by Zelda Hessler's avatar Zelda Hessler Committed by GitHub
Browse files

refactor: RuntimeType creation and member resolution (#2066)

* refactor: RuntimeType creation and member resolution
refactor: RuntimeType companion functions
add: AwsCargoDependency object
refactor: move aws runtime crate fns into AwsRuntimeType
update: all code affected by the above changes

* update: split AwsCargoDependency into separate package
rename: runtimeCrate to smithyRuntimeCrate
remove: smithy prefix from RuntimeType

* fix: missing dep issue
update: RuntimeType import

* fix: missing dep issue in server
rename: pascal-case functions to be camel-case

* revert: changes to CodegenTestCommon.kt

* fix: error I introduced in a merge
fix: outdated doc comment
parent de979d87
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
/*
 * 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.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.crateLocation

fun RuntimeConfig.awsRuntimeCrate(name: String, features: Set<String> = setOf()): CargoDependency =
    CargoDependency(name, awsRoot().crateLocation(null), features = features)

object AwsCargoDependency {
    fun awsConfig(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-config")
    fun awsEndpoint(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-endpoint")
    fun awsHttp(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-http")
    fun awsSigAuth(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-sig-auth")
    fun awsSigAuthEventStream(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-sig-auth", setOf("sign-eventstream"))
    fun awsSigv4(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-sigv4")
    fun awsTypes(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-types")
}
+18 −23
Original line number Diff line number Diff line
@@ -94,18 +94,13 @@ class EndpointConfigCustomization(
) :
    ConfigCustomization() {
    private val runtimeConfig = codegenContext.runtimeConfig
    private val resolveAwsEndpoint = runtimeConfig.awsEndpoint().toType().copy(name = "ResolveAwsEndpoint")
    private val smithyEndpointResolver =
        CargoDependency.smithyHttp(runtimeConfig).toType().member("endpoint::ResolveEndpoint")
    private val placeholderEndpointParams = runtimeConfig.awsEndpoint().toType().member("Params")
    private val endpointShim = runtimeConfig.awsEndpoint().toType().member("EndpointShim")
    private val moduleUseName = codegenContext.moduleUseName()
    private val codegenScope = arrayOf(
        "SmithyResolver" to smithyEndpointResolver,
        "PlaceholderParams" to placeholderEndpointParams,
        "ResolveAwsEndpoint" to resolveAwsEndpoint,
        "EndpointShim" to endpointShim,
        "aws_types" to awsTypes(runtimeConfig).toType(),
        "SmithyResolver" to RuntimeType.smithyHttp(runtimeConfig).resolve("endpoint::ResolveEndpoint"),
        "PlaceholderParams" to AwsRuntimeType.awsEndpoint(runtimeConfig).resolve("Params"),
        "ResolveAwsEndpoint" to AwsRuntimeType.awsEndpoint(runtimeConfig).resolve("ResolveAwsEndpoint"),
        "EndpointShim" to AwsRuntimeType.awsEndpoint(runtimeConfig).resolve("EndpointShim"),
        "aws_types" to AwsRuntimeType.awsTypes(runtimeConfig),
    )

    override fun section(section: ServiceConfig): Writable = writable {
@@ -181,7 +176,7 @@ class EndpointConfigCustomization(

class EndpointResolverFeature(runtimeConfig: RuntimeConfig) :
    OperationCustomization() {
    private val placeholderEndpointParams = runtimeConfig.awsEndpoint().toType().member("Params")
    private val placeholderEndpointParams = AwsRuntimeType.awsEndpoint(runtimeConfig).resolve("Params")
    private val codegenScope = arrayOf(
        "PlaceholderParams" to placeholderEndpointParams,
        "BuildError" to runtimeConfig.operationBuildError(),
@@ -223,20 +218,20 @@ class PubUseEndpoint(private val runtimeConfig: RuntimeConfig) : LibRsCustomizat
class EndpointResolverGenerator(codegenContext: CodegenContext, private val endpointData: ObjectNode) {
    private val runtimeConfig = codegenContext.runtimeConfig
    private val endpointPrefix = codegenContext.serviceShape.expectTrait<ServiceTrait>().endpointPrefix
    private val awsEndpoint = runtimeConfig.awsEndpoint().toType()
    private val awsTypes = runtimeConfig.awsTypes().toType()
    private val awsEndpoint = AwsRuntimeType.awsEndpoint(runtimeConfig)
    private val awsTypes = AwsRuntimeType.awsTypes(runtimeConfig)
    private val codegenScope =
        arrayOf(
            "Partition" to awsEndpoint.member("Partition"),
            "endpoint" to awsEndpoint.member("partition::endpoint"),
            "CredentialScope" to awsEndpoint.member("CredentialScope"),
            "Regionalized" to awsEndpoint.member("partition::Regionalized"),
            "Protocol" to awsEndpoint.member("partition::endpoint::Protocol"),
            "SignatureVersion" to awsEndpoint.member("partition::endpoint::SignatureVersion"),
            "PartitionResolver" to awsEndpoint.member("PartitionResolver"),
            "ResolveAwsEndpoint" to awsEndpoint.member("ResolveAwsEndpoint"),
            "SigningService" to awsTypes.member("SigningService"),
            "SigningRegion" to awsTypes.member("region::SigningRegion"),
            "Partition" to awsEndpoint.resolve("Partition"),
            "endpoint" to awsEndpoint.resolve("partition::endpoint"),
            "CredentialScope" to awsEndpoint.resolve("CredentialScope"),
            "Regionalized" to awsEndpoint.resolve("partition::Regionalized"),
            "Protocol" to awsEndpoint.resolve("partition::endpoint::Protocol"),
            "SignatureVersion" to awsEndpoint.resolve("partition::endpoint::SignatureVersion"),
            "PartitionResolver" to awsEndpoint.resolve("PartitionResolver"),
            "ResolveAwsEndpoint" to awsEndpoint.resolve("ResolveAwsEndpoint"),
            "SigningService" to awsTypes.resolve("SigningService"),
            "SigningRegion" to awsTypes.resolve("region::SigningRegion"),
        )

    fun resolver(): RuntimeType {
+20 −25
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.client.Fluen
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientSection
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.DependencyScope
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
import software.amazon.smithy.rust.codegen.core.rustlang.GenericTypeArg
@@ -38,23 +37,21 @@ import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rustsdk.AwsRuntimeType.defaultMiddleware

private class Types(runtimeConfig: RuntimeConfig) {
    private val smithyTypesDep = CargoDependency.smithyTypes(runtimeConfig)
    private val smithyClientDep = CargoDependency.smithyClient(runtimeConfig)
    private val smithyHttpDep = CargoDependency.smithyHttp(runtimeConfig)
    private val smithyClient = RuntimeType.smithyClient(runtimeConfig)
    private val smithyHttp = RuntimeType.smithyHttp(runtimeConfig)
    private val smithyTypes = RuntimeType.smithyTypes(runtimeConfig)

    val awsTypes = awsTypes(runtimeConfig).toType()
    val smithyClientRetry = RuntimeType("retry", smithyClientDep, "aws_smithy_client")
    val awsSmithyClient = smithyClientDep.toType()

    val connectorSettings = RuntimeType("ConnectorSettings", smithyClientDep, "aws_smithy_client::http_connector")
    val awsTypes = AwsRuntimeType.awsTypes(runtimeConfig)
    val connectorError = smithyHttp.resolve("result::ConnectorError")
    val connectorSettings = smithyClient.resolve("http_connector::ConnectorSettings")
    val defaultMiddleware = runtimeConfig.defaultMiddleware()
    val dynConnector = RuntimeType("DynConnector", smithyClientDep, "aws_smithy_client::erase")
    val dynMiddleware = RuntimeType("DynMiddleware", smithyClientDep, "aws_smithy_client::erase")
    val retryConfig = RuntimeType("RetryConfig", smithyTypesDep, "aws_smithy_types::retry")
    val smithyConnector = RuntimeType("SmithyConnector", smithyClientDep, "aws_smithy_client::bounds")
    val timeoutConfig = RuntimeType("TimeoutConfig", smithyTypesDep, "aws_smithy_types::timeout")

    val connectorError = RuntimeType("ConnectorError", smithyHttpDep, "aws_smithy_http::result")
    val dynConnector = smithyClient.resolve("erase::DynConnector")
    val dynMiddleware = smithyClient.resolve("erase::DynMiddleware")
    val retryConfig = smithyTypes.resolve("retry::RetryConfig")
    val smithyClientBuilder = smithyClient.resolve("Builder")
    val smithyClientRetry = smithyClient.resolve("retry")
    val smithyConnector = smithyClient.resolve("bounds::SmithyConnector")
    val timeoutConfig = smithyTypes.resolve("timeout::TimeoutConfig")
}

private class AwsClientGenerics(private val types: Types) : FluentClientGenerics {
@@ -105,7 +102,7 @@ class AwsFluentClientDecorator : RustCodegenDecorator<ClientProtocolGenerator, C
                AwsPresignedFluentBuilderMethod(runtimeConfig),
                AwsFluentClientDocs(codegenContext),
            ),
            retryClassifier = runtimeConfig.awsHttp().toType().member("retry::AwsResponseRetryClassifier"),
            retryClassifier = AwsRuntimeType.awsHttp(runtimeConfig).resolve("retry::AwsResponseRetryClassifier"),
        ).render(rustCrate)
        rustCrate.withModule(CustomizableOperationGenerator.CustomizeModule) {
            renderCustomizableOperationSendMethod(runtimeConfig, generics, this)
@@ -148,7 +145,7 @@ private class AwsFluentClientExtensions(types: Types) {
        "RetryConfig" to types.retryConfig,
        "SmithyConnector" to types.smithyConnector,
        "TimeoutConfig" to types.timeoutConfig,
        "aws_smithy_client" to types.awsSmithyClient,
        "SmithyClientBuilder" to types.smithyClientBuilder,
        "aws_types" to types.awsTypes,
        "retry" to types.smithyClientRetry,
    )
@@ -197,7 +194,7 @@ private class AwsFluentClientExtensions(types: Types) {
                        c.connector(&connector_settings, conf.sleep_impl())
                    });

                    let builder = #{aws_smithy_client}::Builder::new();
                    let builder = #{SmithyClientBuilder}::new();

                    let builder = match connector {
                        // Use provided connector
@@ -235,7 +232,7 @@ private class AwsFluentClientDocs(private val codegenContext: CodegenContext) :
    private val serviceShape = codegenContext.serviceShape
    private val crateName = codegenContext.moduleUseName()
    private val codegenScope =
        arrayOf("aws_config" to codegenContext.runtimeConfig.awsConfig().copy(scope = DependencyScope.Dev).toType())
        arrayOf("aws_config" to AwsCargoDependency.awsConfig(codegenContext.runtimeConfig).copy(scope = DependencyScope.Dev).toType())

    // If no `aws-config` version is provided, assume that docs referencing `aws-config` cannot be given.
    // Also, STS and SSO must NOT reference `aws-config` since that would create a circular dependency.
@@ -300,8 +297,6 @@ private fun renderCustomizableOperationSendMethod(
    generics: FluentClientGenerics,
    writer: RustWriter,
) {
    val smithyHttp = CargoDependency.smithyHttp(runtimeConfig).toType()

    val operationGenerics = RustGenerics(GenericTypeArg("O"), GenericTypeArg("Retry"))
    val handleGenerics = generics.toRustGenerics()
    val combinedGenerics = operationGenerics + handleGenerics
@@ -309,9 +304,9 @@ private fun renderCustomizableOperationSendMethod(
    val codegenScope = arrayOf(
        "combined_generics_decl" to combinedGenerics.declaration(),
        "handle_generics_bounds" to handleGenerics.bounds(),
        "SdkSuccess" to smithyHttp.member("result::SdkSuccess"),
        "ClassifyRetry" to smithyHttp.member("retry::ClassifyRetry"),
        "ParseHttpResponse" to smithyHttp.member("response::ParseHttpResponse"),
        "SdkSuccess" to RuntimeType.sdkSuccess(runtimeConfig),
        "ClassifyRetry" to RuntimeType.classifyRetry(runtimeConfig),
        "ParseHttpResponse" to RuntimeType.parseHttpResponse(runtimeConfig),
    )

    writer.rustTemplate(
+13 −13
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDe
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientSection
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.docs
@@ -33,6 +32,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
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.RuntimeType
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.generators.error.errorSymbol
@@ -140,14 +140,14 @@ class AwsInputPresignedMethod(
    private val symbolProvider = codegenContext.symbolProvider

    private val codegenScope = arrayOf(
        "Error" to AwsRuntimeType.Presigning.member("config::Error"),
        "PresignedRequest" to AwsRuntimeType.Presigning.member("request::PresignedRequest"),
        "PresignedRequestService" to AwsRuntimeType.Presigning.member("service::PresignedRequestService"),
        "PresigningConfig" to AwsRuntimeType.Presigning.member("config::PresigningConfig"),
        "SdkError" to CargoDependency.smithyHttp(runtimeConfig).toType().member("result::SdkError"),
        "aws_sigv4" to runtimeConfig.awsRuntimeDependency("aws-sigv4").toType(),
        "sig_auth" to runtimeConfig.sigAuth().toType(),
        "tower" to CargoDependency.Tower.toType(),
        "Error" to AwsRuntimeType.Presigning.resolve("config::Error"),
        "PresignedRequest" to AwsRuntimeType.Presigning.resolve("request::PresignedRequest"),
        "PresignedRequestService" to AwsRuntimeType.Presigning.resolve("service::PresignedRequestService"),
        "PresigningConfig" to AwsRuntimeType.Presigning.resolve("config::PresigningConfig"),
        "SdkError" to RuntimeType.sdkError(runtimeConfig),
        "aws_sigv4" to AwsRuntimeType.awsSigv4(runtimeConfig),
        "sig_auth" to AwsRuntimeType.awsSigAuth(runtimeConfig),
        "tower" to RuntimeType.Tower,
        "Middleware" to runtimeConfig.defaultMiddleware(),
    )

@@ -249,10 +249,10 @@ class AwsPresignedFluentBuilderMethod(
    runtimeConfig: RuntimeConfig,
) : FluentClientCustomization() {
    private val codegenScope = arrayOf(
        "Error" to AwsRuntimeType.Presigning.member("config::Error"),
        "PresignedRequest" to AwsRuntimeType.Presigning.member("request::PresignedRequest"),
        "PresigningConfig" to AwsRuntimeType.Presigning.member("config::PresigningConfig"),
        "SdkError" to CargoDependency.smithyHttp(runtimeConfig).toType().member("result::SdkError"),
        "Error" to AwsRuntimeType.Presigning.resolve("config::Error"),
        "PresignedRequest" to AwsRuntimeType.Presigning.resolve("request::PresignedRequest"),
        "PresigningConfig" to AwsRuntimeType.Presigning.resolve("config::PresigningConfig"),
        "SdkError" to RuntimeType.sdkError(runtimeConfig),
    )

    override fun section(section: FluentClientSection): Writable =
+11 −13
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeCrateLocation
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.crateLocation
import java.io.File
import java.nio.file.Path

@@ -54,17 +53,16 @@ object AwsRuntimeType {
            CargoDependency.smithyHttpTower(this),
            CargoDependency.smithyClient(this),
            CargoDependency.Tower,
            sigAuth(),
            awsHttp(),
            awsEndpoint(),
            AwsCargoDependency.awsSigAuth(this),
            AwsCargoDependency.awsHttp(this),
            AwsCargoDependency.awsEndpoint(this),
        ),
    ).member("DefaultMiddleware")
}

fun RuntimeConfig.awsRuntimeDependency(name: String, features: Set<String> = setOf()): CargoDependency =
    CargoDependency(name, awsRoot().crateLocation(null), features = features)
    ).resolve("DefaultMiddleware")

fun RuntimeConfig.awsHttp(): CargoDependency = awsRuntimeDependency("aws-http")
fun RuntimeConfig.awsTypes(): CargoDependency = awsRuntimeDependency("aws-types")
fun RuntimeConfig.awsConfig(): CargoDependency = awsRuntimeDependency("aws-config")
fun RuntimeConfig.awsEndpoint() = awsRuntimeDependency("aws-endpoint")
    fun awsEndpoint(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsEndpoint(runtimeConfig).toType()
    fun awsHttp(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsHttp(runtimeConfig).toType()
    fun awsSigAuth(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsSigAuth(runtimeConfig).toType()
    fun awsSigAuthEventStream(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsSigAuthEventStream(runtimeConfig).toType()
    fun awsSigv4(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsSigv4(runtimeConfig).toType()
    fun awsTypes(runtimeConfig: RuntimeConfig) = AwsCargoDependency.awsTypes(runtimeConfig).toType()
}
Loading