Unverified Commit 4436d9ad authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Remove old service builder machinery (#2161)

* Remove HandlerGenerator and RegistryGenerator

* Shrink RequestRejection

* Remove inlineable

* Remove Router and RequestParts

* Remove old service builder tests

* Add missing test dependency

* Add missing dev dependency

* Remove unused test

* Move Router types

* Switch AllowUnused to AllowUnusedVariables
parent a559048a
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -10,16 +10,19 @@ import software.amazon.smithy.build.PluginContext
import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig
import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerEnumGenerator
import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerOperationHandlerGenerator
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.ServerCodegenContext
@@ -164,4 +167,11 @@ class PythonServerCodegenVisitor(
        )
            .render()
    }

    override fun operationShape(shape: OperationShape) {
        super.operationShape(shape)
        rustCrate.withModule(RustModule.public("python_operation_adaptor")) {
            PythonServerOperationHandlerGenerator(codegenContext, shape).render(this)
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -199,7 +199,7 @@ class PythonApplicationGenerator(
                        let ${name}_locals = #{pyo3_asyncio}::TaskLocals::new(event_loop);
                        let handler = self.handlers.get("$name").expect("Python handler for operation `$name` not found").clone();
                        let builder = builder.$name(move |input, state| {
                            #{pyo3_asyncio}::tokio::scope(${name}_locals.clone(), crate::operation_handler::$name(input, state, handler.clone()))
                            #{pyo3_asyncio}::tokio::scope(${name}_locals.clone(), crate::python_operation_adaptor::$name(input, state, handler.clone()))
                        });
                        """,
                        *codegenScope,
+30 −36
Original line number Diff line number Diff line
@@ -13,8 +13,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase
import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerOperationHandlerGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol

/**
 * The Rust code responsible to run the Python business logic on the Python interpreter
@@ -32,9 +30,8 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.Ser
 */
class PythonServerOperationHandlerGenerator(
    codegenContext: CodegenContext,
    protocol: ServerProtocol,
    private val operations: List<OperationShape>,
) : ServerOperationHandlerGenerator(codegenContext, protocol, operations) {
    private val operation: OperationShape,
) {
    private val symbolProvider = codegenContext.symbolProvider
    private val runtimeConfig = codegenContext.runtimeConfig
    private val codegenScope =
@@ -47,13 +44,11 @@ class PythonServerOperationHandlerGenerator(
            "tracing" to PythonServerCargoDependency.Tracing.toType(),
        )

    override fun render(writer: RustWriter) {
        super.render(writer)
    fun render(writer: RustWriter) {
        renderPythonOperationHandlerImpl(writer)
    }

    private fun renderPythonOperationHandlerImpl(writer: RustWriter) {
        for (operation in operations) {
        val operationName = symbolProvider.toSymbol(operation).name
        val input = "crate::input::${operationName}Input"
        val output = "crate::output::${operationName}Output"
@@ -83,7 +78,6 @@ class PythonServerOperationHandlerGenerator(
            "PyError" to renderPyError(),
        )
    }
    }

    private fun renderPyFunction(name: String, output: String): Writable =
        writable {
+0 −4
Original line number Diff line number Diff line
@@ -33,10 +33,6 @@ class PythonServerServiceGenerator(
        PythonServerOperationErrorGenerator(context.model, context.symbolProvider, operation).render(writer)
    }

    override fun renderOperationHandler(writer: RustWriter, operations: List<OperationShape>) {
        PythonServerOperationHandlerGenerator(context, protocol, operations).render(writer)
    }

    override fun renderExtras(operations: List<OperationShape>) {
        rustCrate.withModule(RustModule.public("python_server_application", "Python server and application implementation.")) {
            PythonApplicationGenerator(context, protocol, operations)
+1 −27
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@ package software.amazon.smithy.rust.codegen.server.smithy
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.CratesIo
import software.amazon.smithy.rust.codegen.core.rustlang.DependencyScope
import software.amazon.smithy.rust.codegen.core.rustlang.InlineDependency
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig

/**
@@ -27,32 +25,8 @@ object ServerCargoDependency {
    val Tower: CargoDependency = CargoDependency("tower", CratesIo("0.4"))
    val TokioDev: CargoDependency = CargoDependency("tokio", CratesIo("1.8.4"), scope = DependencyScope.Dev)
    val Regex: CargoDependency = CargoDependency("regex", CratesIo("1.5.5"))
    val HyperDev: CargoDependency = CargoDependency("hyper", CratesIo("0.14.12"), DependencyScope.Dev)

    fun smithyHttpServer(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-http-server")
    fun smithyTypes(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-types")
}

/**
 * A dependency on a snippet of code
 *
 * ServerInlineDependency should not be instantiated directly, rather, it should be constructed with
 * [software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.forInlineFun]
 *
 * ServerInlineDependencies are created as private modules within the main crate. This is useful for any code that
 * doesn't need to exist in a shared crate, but must still be generated exactly once during codegen.
 *
 * CodegenVisitor de-duplicates inline dependencies by (module, name) during code generation.
 */
object ServerInlineDependency {
    fun serverOperationHandler(runtimeConfig: RuntimeConfig): InlineDependency =
        InlineDependency.forRustFile(
            RustModule.private("server_operation_handler_trait"),
            "/inlineable/src/server_operation_handler_trait.rs",
            ServerCargoDependency.smithyHttpServer(runtimeConfig),
            CargoDependency.Http,
            ServerCargoDependency.PinProjectLite,
            ServerCargoDependency.Tower,
            ServerCargoDependency.FuturesUtil,
            ServerCargoDependency.AsyncTrait,
        )
}
Loading