Skip to content

Commit

Permalink
Move methods from Protocol to ServerProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Barber committed Sep 13, 2022
1 parent b4ac306 commit e348c8f
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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.rustBlockTemplate
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.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
Expand Down Expand Up @@ -183,23 +182,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 @@ -14,7 +14,6 @@ import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.RustModule
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.rustBlockTemplate
Expand Down Expand Up @@ -105,17 +104,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 @@ -11,7 +11,6 @@ import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.RustModule
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.rustBlockTemplate
Expand Down Expand Up @@ -96,17 +95,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 @@ -20,7 +20,6 @@ import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.model.traits.Trait
import software.amazon.smithy.rust.codegen.client.rustlang.Writable
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolGenerator
Expand Down Expand Up @@ -74,22 +73,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 @@ -16,13 +16,11 @@ import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.RustModule
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.rustTemplate
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.JsonParserGenerator
import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.StructuredDataParserGenerator
Expand Down Expand Up @@ -141,15 +139,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 @@ -13,14 +13,12 @@ import software.amazon.smithy.model.traits.AnnotationTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.client.rustlang.RustModule
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.rustBlockTemplate
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.RestXmlParserGenerator
import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.StructuredDataParserGenerator
Expand Down Expand Up @@ -103,15 +101,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 @@ -12,8 +12,8 @@ import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext
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.ServerServiceGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol

/**
* PythonServerServiceGenerator
Expand All @@ -25,7 +25,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 @@ -28,13 +28,13 @@ import software.amazon.smithy.rust.codegen.client.smithy.Outputs
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.CodegenTarget
import software.amazon.smithy.rust.codegen.client.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.client.util.getTrait
import software.amazon.smithy.rust.codegen.client.util.inputShape
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 +49,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 @@ -9,11 +9,13 @@ import software.amazon.smithy.model.knowledge.TopDownIndex
import software.amazon.smithy.model.shapes.OperationShape
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
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator
import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJson
import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJsonVersion
import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol
Expand All @@ -40,6 +42,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 +128,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 +205,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 +232,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 @@ -12,6 +12,7 @@ import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.rust.codegen.client.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.client.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.client.util.lookup
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol
import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestCodegenContext

Expand Down Expand Up @@ -67,7 +68,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 e348c8f

Please sign in to comment.