Skip to content

Commit

Permalink
Remove fromCoreProtocol in favour of as downcasting (#2000)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlbarber authored Nov 17, 2022
1 parent 06c23f6 commit 1f405f6
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class PythonServerCodegenVisitor(
rustCrate,
protocolGenerator,
protocolGeneratorFactory.support(),
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
codegenContext,
)
.render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ open class ServerCodegenVisitor(
rustCrate,
protocolGenerator,
protocolGeneratorFactory.support(),
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
codegenContext,
)
.render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ interface ServerProtocol : Protocol {
* Returns a boolean indicating whether to perform this check.
*/
fun serverContentTypeCheckNoModeledInput(): Boolean = false

companion object {
/** Upgrades the core protocol to a `ServerProtocol`. */
fun fromCoreProtocol(protocol: Protocol): ServerProtocol = when (protocol) {
is AwsJson -> ServerAwsJsonProtocol.fromCoreProtocol(protocol)
is RestJson -> ServerRestJsonProtocol.fromCoreProtocol(protocol)
is RestXml -> ServerRestXmlProtocol.fromCoreProtocol(protocol)
else -> throw IllegalStateException("unsupported protocol")
}
}
}

class ServerAwsJsonProtocol(
Expand Down Expand Up @@ -120,11 +110,6 @@ class ServerAwsJsonProtocol(
override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator =
ServerAwsJsonSerializerGenerator(serverCodegenContext, httpBindingResolver, awsJsonVersion)

companion object {
fun fromCoreProtocol(awsJson: AwsJson): ServerAwsJsonProtocol =
ServerAwsJsonProtocol(awsJson.codegenContext as ServerCodegenContext, awsJson.version)
}

override fun markerStruct(): RuntimeType {
return when (version) {
is AwsJsonVersion.Json10 -> {
Expand Down Expand Up @@ -194,10 +179,6 @@ class ServerRestJsonProtocol(
override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator =
ServerRestJsonSerializerGenerator(serverCodegenContext, httpBindingResolver)

companion object {
fun fromCoreProtocol(restJson: RestJson): ServerRestJsonProtocol = ServerRestJsonProtocol(restJson.codegenContext as ServerCodegenContext)
}

override fun markerStruct() = ServerRuntimeType.Protocol("RestJson1", "rest_json_1", runtimeConfig)

override fun routerType() = restRouterType(runtimeConfig)
Expand All @@ -222,12 +203,6 @@ class ServerRestXmlProtocol(
) : RestXml(codegenContext), ServerProtocol {
val runtimeConfig = codegenContext.runtimeConfig

companion object {
fun fromCoreProtocol(restXml: RestXml): ServerRestXmlProtocol {
return ServerRestXmlProtocol(restXml.codegenContext)
}
}

override fun markerStruct() = ServerRuntimeType.Protocol("RestXml", "rest_xml", runtimeConfig)

override fun routerType() = restRouterType(runtimeConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
private val operationDeserModule = RustModule.private("operation_deser")
private val operationSerModule = RustModule.private("operation_ser")
private val typeConversionGenerator = TypeConversionGenerator(model, symbolProvider, runtimeConfig)
private val serverProtocol = ServerProtocol.fromCoreProtocol(protocol)

private val codegenScope = arrayOf(
"AsyncTrait" to ServerCargoDependency.AsyncTrait.asType(),
Expand Down Expand Up @@ -252,7 +251,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
""".trimIndent(),
*codegenScope,
"I" to inputSymbol,
"Marker" to serverProtocol.markerStruct(),
"Marker" to protocol.markerStruct(),
"parse_request" to serverParseRequest(operationShape),
"verifyAcceptHeader" to verifyAcceptHeader,
"verifyRequestContentTypeHeader" to verifyRequestContentTypeHeader,
Expand Down Expand Up @@ -315,7 +314,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
*codegenScope,
"O" to outputSymbol,
"E" to errorSymbol,
"Marker" to serverProtocol.markerStruct(),
"Marker" to protocol.markerStruct(),
"serialize_response" to serverSerializeResponse(operationShape),
"serialize_error" to serverSerializeError(operationShape),
)
Expand Down Expand Up @@ -348,7 +347,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
""".trimIndent(),
*codegenScope,
"O" to outputSymbol,
"Marker" to serverProtocol.markerStruct(),
"Marker" to protocol.markerStruct(),
"serialize_response" to serverSerializeResponse(operationShape),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package software.amazon.smithy.rust.codegen.server.smithy.protocols
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolGeneratorFactory
import software.amazon.smithy.rust.codegen.core.smithy.protocols.RestXml
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerRestXmlProtocol

Expand All @@ -17,7 +16,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.Ser
* with RestXml specific configurations.
*/
class ServerRestXmlFactory : ProtocolGeneratorFactory<ServerHttpBoundProtocolGenerator, ServerCodegenContext> {
override fun protocol(codegenContext: ServerCodegenContext): Protocol = RestXml(codegenContext)
override fun protocol(codegenContext: ServerCodegenContext): Protocol = ServerRestXmlProtocol(codegenContext)

override fun buildProtocolGenerator(codegenContext: ServerCodegenContext): ServerHttpBoundProtocolGenerator =
ServerHttpBoundProtocolGenerator(codegenContext, ServerRestXmlProtocol(codegenContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ServerOperationRegistryGeneratorTest {

val index = TopDownIndex.of(serverCodegenContext.model)
val operations = index.getContainedOperations(serverCodegenContext.serviceShape).sortedBy { it.id }
val protocol = ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(serverCodegenContext))
val protocol = protocolGeneratorFactory.protocol(serverCodegenContext) as ServerProtocol

val generator = ServerOperationRegistryGenerator(serverCodegenContext, protocol, operations)
val writer = RustWriter.forModule("operation_registry")
Expand Down

0 comments on commit 1f405f6

Please sign in to comment.