Skip to content

Commit

Permalink
Move more things into codegen-core (#1762)
Browse files Browse the repository at this point in the history
This moves most things from `codegen-client` that should be in
`codegen-core` into `codegen-core`.

This is a continuation of the efforts started in #1697, #1730.
  • Loading branch information
david-perez authored Sep 27, 2022
1 parent 911f281 commit 5b7aa7b
Show file tree
Hide file tree
Showing 238 changed files with 2,971 additions and 2,652 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customizations.DocsRsMetadataDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customizations.DocsRsMetadataSettings
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rustsdk.customize.apigateway.ApiGatewayDecorator
import software.amazon.smithy.rustsdk.customize.auth.DisabledAuthDecorator
import software.amazon.smithy.rustsdk.customize.ec2.Ec2Decorator
Expand Down Expand Up @@ -48,7 +49,7 @@ val DECORATORS = listOf(
DocsRsMetadataDecorator(DocsRsMetadataSettings(targets = listOf("x86_64-unknown-linux-gnu"), allFeatures = true)),
)

class AwsCodegenDecorator : CombinedCodegenDecorator<ClientCodegenContext>(DECORATORS) {
class AwsCodegenDecorator : CombinedCodegenDecorator<ClientProtocolGenerator, ClientCodegenContext>(DECORATORS) {
override val name: String = "AwsSdkCodegenDecorator"
override val order: Byte = -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,36 @@ import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.node.ObjectNode
import software.amazon.smithy.model.node.StringNode
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.RustModule
import software.amazon.smithy.rust.codegen.client.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.client.rustlang.Writable
import software.amazon.smithy.rust.codegen.client.rustlang.asType
import software.amazon.smithy.rust.codegen.client.rustlang.rust
import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.client.rustlang.withBlock
import software.amazon.smithy.rust.codegen.client.rustlang.withBlockTemplate
import software.amazon.smithy.rust.codegen.client.rustlang.writable
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.customize.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customize.OperationSection
import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
import software.amazon.smithy.rust.codegen.client.smithy.generators.operationBuildError
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.RustModule
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.asType
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
import software.amazon.smithy.rust.codegen.core.rustlang.withBlockTemplate
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.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.core.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rust.codegen.core.util.orNull
import kotlin.io.path.readText

class AwsEndpointDecorator : RustCodegenDecorator<ClientCodegenContext> {
class AwsEndpointDecorator : RustCodegenDecorator<ClientProtocolGenerator, ClientCodegenContext> {
override val name: String = "AwsEndpoint"
override val order: Byte = 0

Expand Down Expand Up @@ -84,22 +85,22 @@ class AwsEndpointDecorator : RustCodegenDecorator<ClientCodegenContext> {
return baseCustomizations + PubUseEndpoint(codegenContext.runtimeConfig)
}

override fun supportsCodegenContext(clazz: Class<out CoreCodegenContext>): Boolean =
override fun supportsCodegenContext(clazz: Class<out CodegenContext>): Boolean =
clazz.isAssignableFrom(ClientCodegenContext::class.java)
}

class EndpointConfigCustomization(
private val coreCodegenContext: CoreCodegenContext,
private val codegenContext: CodegenContext,
private val endpointData: ObjectNode,
) :
ConfigCustomization() {
private val runtimeConfig = coreCodegenContext.runtimeConfig
private val runtimeConfig = codegenContext.runtimeConfig
private val resolveAwsEndpoint = runtimeConfig.awsEndpoint().asType().copy(name = "ResolveAwsEndpoint")
private val smithyEndpointResolver =
CargoDependency.SmithyHttp(runtimeConfig).asType().member("endpoint::ResolveEndpoint")
private val placeholderEndpointParams = runtimeConfig.awsEndpoint().asType().member("Params")
private val endpointShim = runtimeConfig.awsEndpoint().asType().member("EndpointShim")
private val moduleUseName = coreCodegenContext.moduleUseName()
private val moduleUseName = codegenContext.moduleUseName()
private val codegenScope = arrayOf(
"SmithyResolver" to smithyEndpointResolver,
"PlaceholderParams" to placeholderEndpointParams,
Expand All @@ -108,18 +109,24 @@ class EndpointConfigCustomization(
"aws_types" to awsTypes(runtimeConfig).asType(),
)

override fun section(section: ServiceConfig): Writable = writable {
when (section) {
is ServiceConfig.ConfigStruct -> rustTemplate(
"pub (crate) endpoint_resolver: std::sync::Arc<dyn #{SmithyResolver}<#{PlaceholderParams}>>,",
*codegenScope,
)
is ServiceConfig.ConfigImpl -> emptySection
is ServiceConfig.BuilderStruct ->
rustTemplate("endpoint_resolver: Option<std::sync::Arc<dyn #{SmithyResolver}<#{PlaceholderParams}>>>,", *codegenScope)
ServiceConfig.BuilderImpl ->
rustTemplate(
"""
override fun section(section: ServiceConfig): Writable =
writable {
when (section) {
is ServiceConfig.ConfigStruct -> rustTemplate(
"pub (crate) endpoint_resolver: std::sync::Arc<dyn #{SmithyResolver}<#{PlaceholderParams}>>,",
*codegenScope,
)

is ServiceConfig.ConfigImpl -> emptySection
is ServiceConfig.BuilderStruct ->
rustTemplate(
"endpoint_resolver: Option<std::sync::Arc<dyn #{SmithyResolver}<#{PlaceholderParams}>>>,",
*codegenScope,
)

ServiceConfig.BuilderImpl ->
rustTemplate(
"""
/// Overrides the endpoint resolver to use when making requests.
///
/// When unset, the client will used a generated endpoint resolver based on the endpoint metadata
Expand Down Expand Up @@ -147,22 +154,24 @@ class EndpointConfigCustomization(
self
}
""",
*codegenScope,
)
ServiceConfig.BuilderBuild -> {
val resolverGenerator = EndpointResolverGenerator(coreCodegenContext, endpointData)
rustTemplate(
"""
*codegenScope,
)

ServiceConfig.BuilderBuild -> {
val resolverGenerator = EndpointResolverGenerator(codegenContext, endpointData)
rustTemplate(
"""
endpoint_resolver: self.endpoint_resolver.unwrap_or_else(||
std::sync::Arc::new(#{EndpointShim}::from_resolver(#{Resolver}()))
),
""",
*codegenScope, "Resolver" to resolverGenerator.resolver(),
)
*codegenScope, "Resolver" to resolverGenerator.resolver(),
)
}

else -> emptySection
}
else -> emptySection
}
}
}

// This is an experiment in a slightly different way to create runtime types. All code MAY be refactored to use this pattern
Expand Down Expand Up @@ -208,9 +217,9 @@ class PubUseEndpoint(private val runtimeConfig: RuntimeConfig) : LibRsCustomizat
}
}

class EndpointResolverGenerator(coreCodegenContext: CoreCodegenContext, private val endpointData: ObjectNode) {
private val runtimeConfig = coreCodegenContext.runtimeConfig
private val endpointPrefix = coreCodegenContext.serviceShape.expectTrait<ServiceTrait>().endpointPrefix
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().asType()
private val awsTypes = runtimeConfig.awsTypes().asType()
private val codegenScope =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ package software.amazon.smithy.rustsdk
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.TitleTrait
import software.amazon.smithy.rust.codegen.client.rustlang.Attribute
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.DependencyScope
import software.amazon.smithy.rust.codegen.client.rustlang.Feature
import software.amazon.smithy.rust.codegen.client.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.client.rustlang.Writable
import software.amazon.smithy.rust.codegen.client.rustlang.asType
import software.amazon.smithy.rust.codegen.client.rustlang.rust
import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.client.rustlang.writable
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.RustCrate
import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.GenericTypeArg
import software.amazon.smithy.rust.codegen.client.smithy.generators.GenericsGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.CustomizableOperationGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerics
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
import software.amazon.smithy.rust.codegen.core.rustlang.RustGenerics
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.asType
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
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.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rustsdk.AwsRuntimeType.defaultMiddleware

Expand Down Expand Up @@ -77,14 +78,18 @@ private class AwsClientGenerics(private val types: Types) : FluentClientGenerics
override val bounds = writable { }

/** Bounds for generated `send()` functions */
override fun sendBounds(operation: Symbol, operationOutput: Symbol, operationError: RuntimeType, retryClassifier: RuntimeType): Writable = writable { }
override fun sendBounds(
operation: Symbol,
operationOutput: Symbol,
operationError: RuntimeType,
retryClassifier: RuntimeType,
): Writable =
writable { }

override fun toGenericsGenerator(): GenericsGenerator {
return GenericsGenerator()
}
override fun toRustGenerics() = RustGenerics()
}

class AwsFluentClientDecorator : RustCodegenDecorator<ClientCodegenContext> {
class AwsFluentClientDecorator : RustCodegenDecorator<ClientProtocolGenerator, ClientCodegenContext> {
override val name: String = "FluentClient"

// Must run after the AwsPresigningDecorator so that the presignable trait is correctly added to operations
Expand Down Expand Up @@ -124,12 +129,13 @@ class AwsFluentClientDecorator : RustCodegenDecorator<ClientCodegenContext> {
Attribute.DocInline.render(this)
rust("pub use client::Client;")
}

else -> emptySection
}
}
}

override fun supportsCodegenContext(clazz: Class<out CoreCodegenContext>): Boolean =
override fun supportsCodegenContext(clazz: Class<out CodegenContext>): Boolean =
clazz.isAssignableFrom(ClientCodegenContext::class.java)
}

Expand Down Expand Up @@ -203,17 +209,17 @@ private class AwsFluentClientExtensions(types: Types) {
}
}

private class AwsFluentClientDocs(private val coreCodegenContext: CoreCodegenContext) : FluentClientCustomization() {
private val serviceName = coreCodegenContext.serviceShape.expectTrait<TitleTrait>().value
private val serviceShape = coreCodegenContext.serviceShape
private val crateName = coreCodegenContext.moduleUseName()
private class AwsFluentClientDocs(private val codegenContext: CodegenContext) : FluentClientCustomization() {
private val serviceName = codegenContext.serviceShape.expectTrait<TitleTrait>().value
private val serviceShape = codegenContext.serviceShape
private val crateName = codegenContext.moduleUseName()
private val codegenScope =
arrayOf("aws_config" to coreCodegenContext.runtimeConfig.awsConfig().copy(scope = DependencyScope.Dev).asType())
arrayOf("aws_config" to codegenContext.runtimeConfig.awsConfig().copy(scope = DependencyScope.Dev).asType())

// 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.
private fun suppressUsageDocs(): Boolean =
SdkSettings.from(coreCodegenContext.settings).awsConfigVersion == null ||
SdkSettings.from(codegenContext.settings).awsConfigVersion == null ||
setOf(
ShapeId.from("com.amazonaws.sts#AWSSecurityTokenServiceV20110615"),
ShapeId.from("com.amazonaws.sso#SWBPortalService"),
Expand Down Expand Up @@ -262,6 +268,7 @@ private class AwsFluentClientDocs(private val coreCodegenContext: CoreCodegenCon
)
}
}

else -> emptySection
}
}
Expand All @@ -274,8 +281,8 @@ private fun renderCustomizableOperationSendMethod(
) {
val smithyHttp = CargoDependency.SmithyHttp(runtimeConfig).asType()

val operationGenerics = GenericsGenerator(GenericTypeArg("O"), GenericTypeArg("Retry"))
val handleGenerics = generics.toGenericsGenerator()
val operationGenerics = RustGenerics(GenericTypeArg("O"), GenericTypeArg("Retry"))
val handleGenerics = generics.toRustGenerics()
val combinedGenerics = operationGenerics + handleGenerics

val codegenScope = arrayOf(
Expand Down
Loading

0 comments on commit 5b7aa7b

Please sign in to comment.