Skip to content

Commit

Permalink
Move methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Barber committed Sep 13, 2022
1 parent fd94858 commit 647d543
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> = Map<ShapeId, ProtocolGeneratorFactory<ProtocolGenerator, C>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -136,7 +137,7 @@ class PythonServerCodegenVisitor(
rustCrate,
protocolGenerator,
protocolGeneratorFactory.support(),
protocolGeneratorFactory.protocol(codegenContext),
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
codegenContext,
)
.render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -229,7 +230,7 @@ open class ServerCodegenVisitor(
rustCrate,
protocolGenerator,
protocolGeneratorFactory.support(),
protocolGeneratorFactory.protocol(codegenContext),
ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)),
codegenContext,
)
.render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<OperationShape>,
) {
private val crateName = coreCodegenContext.settings.moduleName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<OperationShape> {
val index = TopDownIndex.of(coreCodegenContext.model)
Expand All @@ -40,6 +44,22 @@ interface ServerProtocol : Protocol {
*/
fun routerConstruction(operationValues: Iterable<Writable>): 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) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -170,6 +207,15 @@ class ServerRestJsonProtocol(
override fun routerType() = restRouterType(runtimeConfig)

override fun routerConstruction(operationValues: Iterable<Writable>): 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(
Expand All @@ -188,4 +234,13 @@ class ServerRestXmlProtocol(
override fun routerType() = restRouterType(runtimeConfig)

override fun routerConstruction(operationValues: Iterable<Writable>): 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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 647d543

Please sign in to comment.