Skip to content
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

Move more things into codegen-core #1762

Merged
merged 21 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d637c4
Move things into `codegen-core` --- sa, ca work
david-perez Sep 22, 2022
8bdb97b
server-unit-tests work
david-perez Sep 22, 2022
a0da58e
client-unit-tests work
david-perez Sep 22, 2022
0ff4694
sdk works
david-perez Sep 22, 2022
ddaef40
codegen-server-test:python works
david-perez Sep 22, 2022
9d6ad32
./gradlew ktlintFormat
david-perez Sep 22, 2022
a21fe4d
Address TODOs
david-perez Sep 22, 2022
ba76e98
./gradlew ktlintFormat
david-perez Sep 22, 2022
a359e97
Copyright header
david-perez Sep 22, 2022
452593e
Merge remote-tracking branch 'upstream/main' into davidpz/move-more-t…
david-perez Sep 22, 2022
953ae53
Move more things into `codegen-core`
david-perez Sep 22, 2022
6ea6f54
Fix Python Server unit tests
david-perez Sep 23, 2022
74a5278
Fix AWS SDK Kotlin unit tests
david-perez Sep 23, 2022
0876167
Rename protocol loader in `codegen-core` from `ClientProtocolLoader` …
david-perez Sep 23, 2022
d6385d9
Move `GenericsGenerator` into `rustlang` and rename it to `RustGenerics`
david-perez Sep 23, 2022
90d250d
Rename `CoreCodegenContext` as `CodegenContext`, since it's now in th…
david-perez Sep 23, 2022
66acea3
./gradlew ktlintFormat
david-perez Sep 23, 2022
c34dc46
import `kotlin.streams.toList`
david-perez Sep 23, 2022
67dc987
Merge remote-tracking branch 'upstream/main' into davidpz/move-more-t…
david-perez Sep 26, 2022
36c3cdf
./gradlew ktlintFormat
david-perez Sep 26, 2022
667aaef
Merge remote-tracking branch 'upstream/main' into davidpz/move-more-t…
david-perez Sep 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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