Skip to content

Commit 1f405f6

Browse files
authored
Remove fromCoreProtocol in favour of as downcasting (#2000)
1 parent 06c23f6 commit 1f405f6

File tree

6 files changed

+7
-34
lines changed

6 files changed

+7
-34
lines changed

codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class PythonServerCodegenVisitor(
161161
rustCrate,
162162
protocolGenerator,
163163
protocolGeneratorFactory.support(),
164-
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
164+
protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
165165
codegenContext,
166166
)
167167
.render()

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ open class ServerCodegenVisitor(
438438
rustCrate,
439439
protocolGenerator,
440440
protocolGeneratorFactory.support(),
441-
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
441+
protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
442442
codegenContext,
443443
)
444444
.render()

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,6 @@ interface ServerProtocol : Protocol {
7878
* Returns a boolean indicating whether to perform this check.
7979
*/
8080
fun serverContentTypeCheckNoModeledInput(): Boolean = false
81-
82-
companion object {
83-
/** Upgrades the core protocol to a `ServerProtocol`. */
84-
fun fromCoreProtocol(protocol: Protocol): ServerProtocol = when (protocol) {
85-
is AwsJson -> ServerAwsJsonProtocol.fromCoreProtocol(protocol)
86-
is RestJson -> ServerRestJsonProtocol.fromCoreProtocol(protocol)
87-
is RestXml -> ServerRestXmlProtocol.fromCoreProtocol(protocol)
88-
else -> throw IllegalStateException("unsupported protocol")
89-
}
90-
}
9181
}
9282

9383
class ServerAwsJsonProtocol(
@@ -120,11 +110,6 @@ class ServerAwsJsonProtocol(
120110
override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator =
121111
ServerAwsJsonSerializerGenerator(serverCodegenContext, httpBindingResolver, awsJsonVersion)
122112

123-
companion object {
124-
fun fromCoreProtocol(awsJson: AwsJson): ServerAwsJsonProtocol =
125-
ServerAwsJsonProtocol(awsJson.codegenContext as ServerCodegenContext, awsJson.version)
126-
}
127-
128113
override fun markerStruct(): RuntimeType {
129114
return when (version) {
130115
is AwsJsonVersion.Json10 -> {
@@ -194,10 +179,6 @@ class ServerRestJsonProtocol(
194179
override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator =
195180
ServerRestJsonSerializerGenerator(serverCodegenContext, httpBindingResolver)
196181

197-
companion object {
198-
fun fromCoreProtocol(restJson: RestJson): ServerRestJsonProtocol = ServerRestJsonProtocol(restJson.codegenContext as ServerCodegenContext)
199-
}
200-
201182
override fun markerStruct() = ServerRuntimeType.Protocol("RestJson1", "rest_json_1", runtimeConfig)
202183

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

225-
companion object {
226-
fun fromCoreProtocol(restXml: RestXml): ServerRestXmlProtocol {
227-
return ServerRestXmlProtocol(restXml.codegenContext)
228-
}
229-
}
230-
231206
override fun markerStruct() = ServerRuntimeType.Protocol("RestXml", "rest_xml", runtimeConfig)
232207

233208
override fun routerType() = restRouterType(runtimeConfig)

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
125125
private val operationDeserModule = RustModule.private("operation_deser")
126126
private val operationSerModule = RustModule.private("operation_ser")
127127
private val typeConversionGenerator = TypeConversionGenerator(model, symbolProvider, runtimeConfig)
128-
private val serverProtocol = ServerProtocol.fromCoreProtocol(protocol)
129128

130129
private val codegenScope = arrayOf(
131130
"AsyncTrait" to ServerCargoDependency.AsyncTrait.asType(),
@@ -252,7 +251,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
252251
""".trimIndent(),
253252
*codegenScope,
254253
"I" to inputSymbol,
255-
"Marker" to serverProtocol.markerStruct(),
254+
"Marker" to protocol.markerStruct(),
256255
"parse_request" to serverParseRequest(operationShape),
257256
"verifyAcceptHeader" to verifyAcceptHeader,
258257
"verifyRequestContentTypeHeader" to verifyRequestContentTypeHeader,
@@ -315,7 +314,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
315314
*codegenScope,
316315
"O" to outputSymbol,
317316
"E" to errorSymbol,
318-
"Marker" to serverProtocol.markerStruct(),
317+
"Marker" to protocol.markerStruct(),
319318
"serialize_response" to serverSerializeResponse(operationShape),
320319
"serialize_error" to serverSerializeError(operationShape),
321320
)
@@ -348,7 +347,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
348347
""".trimIndent(),
349348
*codegenScope,
350349
"O" to outputSymbol,
351-
"Marker" to serverProtocol.markerStruct(),
350+
"Marker" to protocol.markerStruct(),
352351
"serialize_response" to serverSerializeResponse(operationShape),
353352
)
354353
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerRestXmlFactory.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package software.amazon.smithy.rust.codegen.server.smithy.protocols
88
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
99
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
1010
import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolGeneratorFactory
11-
import software.amazon.smithy.rust.codegen.core.smithy.protocols.RestXml
1211
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
1312
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerRestXmlProtocol
1413

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

2221
override fun buildProtocolGenerator(codegenContext: ServerCodegenContext): ServerHttpBoundProtocolGenerator =
2322
ServerHttpBoundProtocolGenerator(codegenContext, ServerRestXmlProtocol(codegenContext))

codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ServerOperationRegistryGeneratorTest {
6868

6969
val index = TopDownIndex.of(serverCodegenContext.model)
7070
val operations = index.getContainedOperations(serverCodegenContext.serviceShape).sortedBy { it.id }
71-
val protocol = ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(serverCodegenContext))
71+
val protocol = protocolGeneratorFactory.protocol(serverCodegenContext) as ServerProtocol
7272

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

0 commit comments

Comments
 (0)