From 647d5435f284862c942378d9b2456e891efe43ee Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Thu, 8 Sep 2022 09:23:18 +0000 Subject: [PATCH] Move methods --- .../client/smithy/protocols/AwsJson.kt | 17 ------ .../client/smithy/protocols/AwsQuery.kt | 13 ----- .../client/smithy/protocols/Ec2Query.kt | 13 ----- .../client/smithy/protocols/Protocol.kt | 16 ------ .../client/smithy/protocols/RestJson.kt | 9 --- .../client/smithy/protocols/RestXml.kt | 9 --- .../smithy/PythonServerCodegenVisitor.kt | 3 +- .../PythonServerServiceGenerator.kt | 7 ++- .../server/smithy/ServerCodegenVisitor.kt | 3 +- .../ServerOperationRegistryGenerator.kt | 3 +- .../generators/ServerServiceGenerator.kt | 3 +- .../generators/protocol/ServerProtocol.kt | 55 +++++++++++++++++++ .../ServerOperationRegistryGeneratorTest.kt | 2 +- 13 files changed, 69 insertions(+), 84 deletions(-) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt index 44a0cfc176a..90bdd091ced 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt @@ -183,23 +183,6 @@ open class AwsJson( *errorScope, ) } - - /** - * Returns the operation name as required by the awsJson1.x protocols. - */ - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ) = writable { - rust("""String::from("$serviceName.$operationName")""") - } - - override fun serverRouterRuntimeConstructor() = when (awsJsonVersion) { - AwsJsonVersion.Json10 -> "new_aws_json_10_router" - AwsJsonVersion.Json11 -> "new_aws_json_11_router" - } } fun awsJsonFieldName(member: MemberShape): String = member.memberName diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt index c2b5e620dac..e58906e3d03 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt @@ -105,17 +105,4 @@ class AwsQueryProtocol(private val coreCodegenContext: CoreCodegenContext) : Pro rust("#T::parse_generic_error(payload.as_ref())", awsQueryErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable { - TODO("Not yet implemented") - } - - override fun serverRouterRuntimeConstructor(): String { - TODO("Not yet implemented") - } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt index 1d87294d895..3178ba3aea0 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt @@ -96,17 +96,4 @@ class Ec2QueryProtocol(private val coreCodegenContext: CoreCodegenContext) : Pro rust("#T::parse_generic_error(payload.as_ref())", ec2QueryErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable { - TODO("Not yet implemented") - } - - override fun serverRouterRuntimeConstructor(): String { - TODO("Not yet implemented") - } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt index eb9ce489f3a..0e7dca2d097 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt @@ -74,22 +74,6 @@ interface Protocol { * there are no response headers or statuses available to further inform the error parsing. */ fun parseEventStreamGenericError(operationShape: OperationShape): RuntimeType - - /** - * Returns a writable for the `RequestSpec` for an operation. - */ - fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable - - /** - * Returns the name of the constructor to be used on the `Router` type, to instantiate a `Router` using this - * protocol. - */ - fun serverRouterRuntimeConstructor(): String } typealias ProtocolMap = Map> diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt index 629ee1b2048..e6ff20a8398 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt @@ -141,15 +141,6 @@ open class RestJson(val coreCodegenContext: CoreCodegenContext) : Protocol { *errorScope, ) } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) - - override fun serverRouterRuntimeConstructor() = "new_rest_json_router" } fun restJsonFieldName(member: MemberShape): String { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt index 2b99fe09abd..8ea44e61c80 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt @@ -103,15 +103,6 @@ open class RestXml(val coreCodegenContext: CoreCodegenContext) : Protocol { rust("#T::parse_generic_error(payload.as_ref())", restXmlErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) - - override fun serverRouterRuntimeConstructor() = "new_rest_xml_router" } /** diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt index de0b3d920e0..1f894ea4ada 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt @@ -26,6 +26,7 @@ import software.amazon.smithy.rust.codegen.server.python.smithy.generators.Pytho import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerServiceGenerator import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerStructureGenerator import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenVisitor +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader /** @@ -136,7 +137,7 @@ class PythonServerCodegenVisitor( rustCrate, protocolGenerator, protocolGeneratorFactory.support(), - protocolGeneratorFactory.protocol(codegenContext), + ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)), codegenContext, ) .render() diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt index 9e38c48bab9..c0ca5f39566 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt @@ -14,6 +14,11 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.Pro import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol +import software.amazon.smithy.rust.codegen.smithy.CoreCodegenContext +import software.amazon.smithy.rust.codegen.smithy.RustCrate +import software.amazon.smithy.rust.codegen.smithy.generators.protocol.ProtocolGenerator +import software.amazon.smithy.rust.codegen.smithy.generators.protocol.ProtocolSupport /** * PythonServerServiceGenerator @@ -25,7 +30,7 @@ class PythonServerServiceGenerator( private val rustCrate: RustCrate, protocolGenerator: ProtocolGenerator, protocolSupport: ProtocolSupport, - protocol: Protocol, + protocol: ServerProtocol, private val context: CoreCodegenContext, ) : ServerServiceGenerator(rustCrate, protocolGenerator, protocolSupport, protocol, context) { diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index 856ee0fe9c2..921ce79aaeb 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -40,6 +40,7 @@ import software.amazon.smithy.rust.codegen.client.util.getTrait import software.amazon.smithy.rust.codegen.client.util.runCommand import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerEnumGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader import java.util.logging.Logger @@ -229,7 +230,7 @@ open class ServerCodegenVisitor( rustCrate, protocolGenerator, protocolGeneratorFactory.support(), - protocolGeneratorFactory.protocol(codegenContext), + ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)), codegenContext, ) .render() diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt index 3e7126a2ab8..c22150b430d 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt @@ -35,6 +35,7 @@ import software.amazon.smithy.rust.codegen.client.util.outputShape import software.amazon.smithy.rust.codegen.client.util.toSnakeCase import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol /** * [ServerOperationRegistryGenerator] renders the `OperationRegistry` struct, a place where users can register their @@ -49,7 +50,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType */ class ServerOperationRegistryGenerator( private val coreCodegenContext: CoreCodegenContext, - private val protocol: Protocol, + private val protocol: ServerProtocol, private val operations: List, ) { private val crateName = coreCodegenContext.settings.moduleName diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt index 2a28b8fca3d..99c59ff51b7 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt @@ -17,7 +17,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.DefaultPublicModules import software.amazon.smithy.rust.codegen.client.smithy.RustCrate import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolTestGenerator @@ -31,7 +30,7 @@ open class ServerServiceGenerator( private val rustCrate: RustCrate, private val protocolGenerator: ProtocolGenerator, private val protocolSupport: ProtocolSupport, - private val protocol: Protocol, + private val protocol: ServerProtocol, private val coreCodegenContext: CoreCodegenContext, ) { private val index = TopDownIndex.of(coreCodegenContext.model) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt index bf879d8f348..0b5a5785b0a 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt @@ -5,10 +5,13 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators.protocol +import software.amazon.smithy.model.Model import software.amazon.smithy.model.knowledge.TopDownIndex import software.amazon.smithy.model.shapes.OperationShape +import software.amazon.smithy.model.shapes.ServiceShape 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.rustTemplate import software.amazon.smithy.rust.codegen.client.rustlang.writable import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext @@ -21,6 +24,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.protocols.RestJson import software.amazon.smithy.rust.codegen.client.smithy.protocols.RestXml import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType +import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator private fun allOperations(coreCodegenContext: CoreCodegenContext): List { val index = TopDownIndex.of(coreCodegenContext.model) @@ -40,6 +44,22 @@ interface ServerProtocol : Protocol { */ fun routerConstruction(operationValues: Iterable): Writable + /** + * Returns the name of the constructor to be used on the `Router` type, to instantiate a `Router` using this + * protocol. + */ + fun serverRouterRuntimeConstructor(): String + + /** + * Returns a writable for the `RequestSpec` for an operation. + */ + fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable + companion object { /** Upgrades the core protocol to a `ServerProtocol`. */ fun fromCoreProtocol(protocol: Protocol): ServerProtocol = when (protocol) { @@ -110,6 +130,23 @@ class ServerAwsJsonProtocol( "Pairs" to pairs, ) } + + /** + * Returns the operation name as required by the awsJson1.x protocols. + */ + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ) = writable { + rust("""String::from("$serviceName.$operationName")""") + } + + override fun serverRouterRuntimeConstructor() = when (version) { + AwsJsonVersion.Json10 -> "new_aws_json_10_router" + AwsJsonVersion.Json11 -> "new_aws_json_11_router" + } } private fun restRouterType(runtimeConfig: RuntimeConfig) = RuntimeType("RestRouter", ServerCargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing::routers::rest") @@ -170,6 +207,15 @@ class ServerRestJsonProtocol( override fun routerType() = restRouterType(runtimeConfig) override fun routerConstruction(operationValues: Iterable): Writable = restRouterConstruction(this, operationValues, coreCodegenContext) + + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) + + override fun serverRouterRuntimeConstructor() = "new_rest_json_router" } class ServerRestXmlProtocol( @@ -188,4 +234,13 @@ class ServerRestXmlProtocol( override fun routerType() = restRouterType(runtimeConfig) override fun routerConstruction(operationValues: Iterable): Writable = restRouterConstruction(this, operationValues, coreCodegenContext) + + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) + + override fun serverRouterRuntimeConstructor() = "new_rest_xml_router" } diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGeneratorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGeneratorTest.kt index 820c7294628..8e9231ee618 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGeneratorTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGeneratorTest.kt @@ -67,7 +67,7 @@ class ServerOperationRegistryGeneratorTest { val index = TopDownIndex.of(serverCodegenContext.model) val operations = index.getContainedOperations(serverCodegenContext.serviceShape).sortedBy { it.id } - val protocol = protocolGeneratorFactory.protocol(serverCodegenContext) + val protocol = ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(serverCodegenContext)) val generator = ServerOperationRegistryGenerator(serverCodegenContext, protocol, operations) val writer = RustWriter.forModule("operation_registry")