Move `MakeOperationGenerator`, `ResponseBindingGenerator`, and `RequestBindingGenerator` to `codegen-client` (#2132)

* Rename `MakeOperationGenerator` -> `ClientMakeOperationGenerator`

* Introduce `MakeOperationGenerator` interface

* Remove `MakeOperationGenerator` from `ProtocolGenerator`

* Move `MakeOperationGenerator` to `codegen-client`

* Rename `ClientMakeOperationGenerator` -> `MakeOperationGenerator`

* Move `RequestBindingGenerator` to `codegen-client`

* Move `ResponseBindingGenerator` to `codegen-client`

* Fix outdated imports
This commit is contained in:
Julian Antonielli 2023-01-03 16:39:27 +00:00 committed by GitHub
parent a84e61af63
commit 5f075609c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 30 additions and 46 deletions

View File

@ -20,6 +20,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
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.MakeOperationGenerator
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
@ -35,7 +36,6 @@ 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
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBoundProtocolPayloadGenerator
import software.amazon.smithy.rust.codegen.core.util.cloneOperation
import software.amazon.smithy.rust.codegen.core.util.expectTrait

View File

@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.generators
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.traits.EndpointTrait
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.rustFormatString
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
@ -16,7 +17,6 @@ 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.RustSymbolProvider
import software.amazon.smithy.rust.codegen.core.smithy.generators.OperationBuildError
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.rustFormatString
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
import software.amazon.smithy.rust.codegen.core.util.inputShape

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy.generators.http
package software.amazon.smithy.rust.codegen.client.smithy.generators.http
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.knowledge.HttpBinding
@ -27,6 +27,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.generators.OperationBuildError
import software.amazon.smithy.rust.codegen.core.smithy.generators.builderSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.HttpBindingGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
@ -50,8 +51,6 @@ fun SmithyPattern.rustFormatString(prefix: String, separator: String): String {
return base.dq()
}
// TODO(https://github.com/awslabs/smithy-rs/issues/1901) Move to `codegen-client` and update docs.
// `MakeOperationGenerator` needs to be moved to `codegen-client` first, which is not easy.
/**
* Generates methods to serialize and deserialize requests based on the HTTP trait. Specifically:
* 1. `fn update_http_request(builder: http::request::Builder) -> Builder`

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy.generators.http
package software.amazon.smithy.rust.codegen.client.smithy.generators.http
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.OperationShape
@ -12,11 +12,10 @@ import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.generators.builderSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.HttpBindingGenerator
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBindingDescriptor
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
// TODO(https://github.com/awslabs/smithy-rs/issues/1901) Move to `codegen-client` and update docs.
// `MakeOperationGenerator` needs to be moved to `codegen-client` first, which is not easy.
class ResponseBindingGenerator(
protocol: Protocol,
private val codegenContext: CodegenContext,

View File

@ -19,7 +19,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSectio
import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizations
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolTraitImplGenerator
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
@ -28,9 +27,13 @@ import software.amazon.smithy.rust.codegen.core.util.inputShape
open class ClientProtocolGenerator(
codegenContext: CodegenContext,
private val protocol: Protocol,
/**
* Operations generate a `make_operation(&config)` method to build a `aws_smithy_http::Operation` that can be dispatched
* This is the serializer side of request dispatch
*/
private val makeOperationGenerator: MakeOperationGenerator,
private val traitGenerator: ProtocolTraitImplGenerator,
) : ProtocolGenerator(codegenContext, protocol, makeOperationGenerator, traitGenerator) {
) : ProtocolGenerator(codegenContext, protocol, traitGenerator) {
/**
* Render all code required for serializing requests and deserializing responses for the operation
*

View File

@ -3,11 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy.generators.protocol
package software.amazon.smithy.rust.codegen.client.smithy.generators.protocol
import software.amazon.smithy.aws.traits.ServiceTrait
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RequestBindingGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.docs
@ -21,8 +22,8 @@ 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.customize.writeCustomizations
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.RequestBindingGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolPayloadGenerator
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpLocation
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.util.dq
@ -31,8 +32,6 @@ import software.amazon.smithy.rust.codegen.core.util.getTrait
import software.amazon.smithy.rust.codegen.core.util.inputShape
import software.amazon.smithy.rust.codegen.core.util.letIf
// TODO(https://github.com/awslabs/smithy-rs/issues/1901): Move to `codegen-client`.
/** Generates the `make_operation` function on input structs */
open class MakeOperationGenerator(
protected val codegenContext: CodegenContext,
@ -46,7 +45,7 @@ open class MakeOperationGenerator(
protected val model = codegenContext.model
protected val runtimeConfig = codegenContext.runtimeConfig
protected val symbolProvider = codegenContext.symbolProvider
protected val httpBindingResolver = protocol.httpBindingResolver
private val httpBindingResolver = protocol.httpBindingResolver
private val defaultClassifier = RuntimeType.smithyHttp(runtimeConfig)
.resolve("retry::DefaultResponseRetryClassifier")

View File

@ -9,7 +9,9 @@ import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.ResponseBindingGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
@ -29,8 +31,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizat
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.builderSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.ResponseBindingGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolTraitImplGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.setterName
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBindingDescriptor

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy.generators.http
package software.amazon.smithy.rust.codegen.client.smithy.generators.http
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
@ -11,6 +11,8 @@ import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.client.testutil.testSymbolProvider
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.rust
@ -24,8 +26,6 @@ import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
import software.amazon.smithy.rust.codegen.core.testutil.renderWithModelBuilder
import software.amazon.smithy.rust.codegen.core.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.core.testutil.testSymbolProvider
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.expectTrait

View File

@ -3,11 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy.generators.http
package software.amazon.smithy.rust.codegen.client.smithy.generators.http
import org.junit.jupiter.api.Test
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
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.rust
@ -22,7 +23,6 @@ import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
import software.amazon.smithy.rust.codegen.core.testutil.renderWithModelBuilder
import software.amazon.smithy.rust.codegen.core.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
import software.amazon.smithy.rust.codegen.core.util.outputShape

View File

@ -22,7 +22,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
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.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolPayloadGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolTraitImplGenerator

View File

@ -16,7 +16,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBindingReso
/**
* [RestRequestSpecGenerator] generates a restJson1 or restXml specific `RequestSpec`. Both protocols are routed the same.
*
* This class has to live in the `codegen` subproject instead of in the `codegen-server` subproject because it is used
* This class has to live in the `codegen-core` subproject instead of in the `codegen-server` subproject because it is used
* by the implementations of the `serverRouterRequestSpec` of the [Protocol] interface, which is used by both subprojects
* (even though only the `codegen-server` subproject calls `serverRouterRequestSpec`).
*/

View File

@ -72,11 +72,6 @@ abstract class ProtocolGenerator(
* and an output shape is build from an `http::Response`.
*/
private val protocol: Protocol,
/**
* Operations generate a `make_operation(&config)` method to build a `aws_smithy_http::Operation` that can be dispatched
* This is the serializer side of request dispatch
*/
private val makeOperationGenerator: MakeOperationGenerator,
/**
* Operations generate implementations of ParseHttpResponse or ParseStrictResponse.
* This is the deserializer side of request dispatch (parsing the response)

View File

@ -8,16 +8,14 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators.protocol
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolTraitImplGenerator
open class ServerProtocolGenerator(
codegenContext: CodegenContext,
val protocol: ServerProtocol,
makeOperationGenerator: MakeOperationGenerator,
private val traitGenerator: ProtocolTraitImplGenerator,
) : ProtocolGenerator(codegenContext, protocol, makeOperationGenerator, traitGenerator) {
) : ProtocolGenerator(codegenContext, protocol, traitGenerator) {
/**
* The server implementation uses this method to generate implementations of the `from_request` and `into_response`
* traits for operation input and output shapes, respectively.

View File

@ -45,7 +45,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustom
import software.amazon.smithy.rust.codegen.core.smithy.generators.TypeConversionGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.http.HttpMessageType
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolTraitImplGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.setterName
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
@ -90,13 +89,6 @@ class ServerHttpBoundProtocolGenerator(
) : ServerProtocolGenerator(
codegenContext,
protocol,
MakeOperationGenerator(
codegenContext,
protocol,
HttpBoundProtocolPayloadGenerator(codegenContext, protocol),
public = true,
includeDefaultPayloadHeaders = true,
),
ServerHttpBoundProtocolTraitImplGenerator(codegenContext, protocol),
) {
// Define suffixes for operation input / output / error wrappers
@ -984,17 +976,17 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
}
}
}
val (queryBindingsTargettingCollection, queryBindingsTargettingSimple) =
val (queryBindingsTargetingCollection, queryBindingsTargetingSimple) =
queryBindings.partition { model.expectShape(it.member.target) is CollectionShape }
queryBindingsTargettingSimple.forEach {
queryBindingsTargetingSimple.forEach {
rust("let mut seen_${symbolProvider.toMemberName(it.member)} = false;")
}
queryBindingsTargettingCollection.forEach {
queryBindingsTargetingCollection.forEach {
rust("let mut ${symbolProvider.toMemberName(it.member)} = Vec::new();")
}
rustBlock("for (k, v) in pairs") {
queryBindingsTargettingSimple.forEach {
queryBindingsTargetingSimple.forEach {
val deserializer = generateParseStrFn(it, false)
val memberName = symbolProvider.toMemberName(it.member)
rustTemplate(
@ -1009,7 +1001,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
"deserializer" to deserializer,
)
}
queryBindingsTargettingCollection.forEachIndexed { idx, it ->
queryBindingsTargetingCollection.forEachIndexed { idx, it ->
rustBlock("${if (idx > 0) "else " else ""}if k == ${it.locationName.dq()}") {
val targetCollectionShape = model.expectShape(it.member.target, CollectionShape::class.java)
val memberShape = model.expectShape(targetCollectionShape.member.target)
@ -1097,7 +1089,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
}
}
}
queryBindingsTargettingCollection.forEach { binding ->
queryBindingsTargetingCollection.forEach { binding ->
// TODO(https://github.com/awslabs/smithy-rs/issues/1401) Constraint traits on member shapes are not
// implemented yet.
val hasConstrainedTarget =