From 9c824df381fe01b02372ee60f1b68435c7b9ef9c Mon Sep 17 00:00:00 2001 From: david-perez Date: Mon, 22 Aug 2022 18:37:56 +0200 Subject: [PATCH] Make `ServerProtocolLoader` delegate to `ProtocolLoader` (#1524) While the set of supported protocols and their implementation is different among servers and clients, the logic to load a specific protocol given a Smithy model is identical. This commit makes `ServerProtocolLoader` inherit from `ProtocolLoader` so that said logic can be reused among clients and servers. --- .../smithy/protocols/ServerProtocolLoader.kt | 28 ++----------------- .../rust/codegen/smithy/protocols/Protocol.kt | 2 +- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerProtocolLoader.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerProtocolLoader.kt index 1f3cabcb9..ebbd163b6 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerProtocolLoader.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerProtocolLoader.kt @@ -9,35 +9,13 @@ import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait import software.amazon.smithy.aws.traits.protocols.RestJson1Trait import software.amazon.smithy.aws.traits.protocols.RestXmlTrait -import software.amazon.smithy.codegen.core.CodegenException -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.knowledge.ServiceIndex -import software.amazon.smithy.model.shapes.ServiceShape -import software.amazon.smithy.model.shapes.ShapeId -import software.amazon.smithy.model.traits.Trait import software.amazon.smithy.rust.codegen.smithy.ServerCodegenContext -import software.amazon.smithy.rust.codegen.smithy.generators.protocol.ProtocolGenerator import software.amazon.smithy.rust.codegen.smithy.protocols.AwsJsonVersion -import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolGeneratorFactory +import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolLoader import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolMap -/* - * Protocol dispatcher, responsible for protocol selection. - */ -class ServerProtocolLoader(private val supportedProtocols: ProtocolMap) { - fun protocolFor( - model: Model, - serviceShape: ServiceShape, - ): Pair> { - val protocols: MutableMap = ServiceIndex.of(model).getProtocols(serviceShape) - val matchingProtocols = - protocols.keys.mapNotNull { protocolId -> supportedProtocols[protocolId]?.let { protocolId to it } } - if (matchingProtocols.isEmpty()) { - throw CodegenException("No matching protocol — service offers: ${protocols.keys}. We offer: ${supportedProtocols.keys}") - } - val pair = matchingProtocols.first() - return Pair(pair.first, pair.second) - } +class ServerProtocolLoader(supportedProtocols: ProtocolMap) : + ProtocolLoader(supportedProtocols) { companion object { val DefaultProtocols = mapOf( diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/Protocol.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/Protocol.kt index 10537993c..63a8569f3 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/Protocol.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/Protocol.kt @@ -100,7 +100,7 @@ interface ProtocolGeneratorFactory(private val supportedProtocols: ProtocolMap) { +open class ProtocolLoader(private val supportedProtocols: ProtocolMap) { fun protocolFor( model: Model, serviceShape: ServiceShape, -- GitLab