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

Refactor `ShapeVisitor` methods - `ServerServiceGeneratorV2` (#2493)

* Rename generators

* Move ServerServiceGenerator to ShapeVisitor::serviceShape

* Remove renderExtras

* Update ServerRootGenerator documentation

* Remove server and protocol test generation from ServerRootGenerator
parent 70bc3340
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -22,10 +22,10 @@ import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProviderConfig
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.ErrorImplGenerator
import software.amazon.smithy.rust.codegen.core.util.getTrait
import software.amazon.smithy.rust.codegen.core.util.isEventStream
import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonApplicationGenerator
import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerEnumGenerator
import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerOperationErrorGenerator
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.python.smithy.generators.PythonServerUnionGenerator
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
@@ -224,15 +224,15 @@ class PythonServerCodegenVisitor(
     * - Python operation handlers
     */
    override fun serviceShape(shape: ServiceShape) {
        super.serviceShape(shape)

        logger.info("[python-server-codegen] Generating a service $shape")
        PythonServerServiceGenerator(
            rustCrate,
            protocolGenerator,
            protocolGeneratorFactory.support(),
            protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
            codegenContext,
        )
            .render()

        val serverProtocol = protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol
        rustCrate.withModule(PythonServerRustModule.PythonServerApplication) {
            PythonApplicationGenerator(codegenContext, serverProtocol)
                .render(this)
        }
    }

    override fun operationShape(shape: OperationShape) {
+7 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@

package software.amazon.smithy.rust.codegen.server.python.smithy.generators

import software.amazon.smithy.model.knowledge.TopDownIndex
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.traits.DocumentationTrait
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
@@ -66,8 +67,13 @@ import software.amazon.smithy.rust.codegen.server.smithy.ServerRustModule.Output
class PythonApplicationGenerator(
    codegenContext: CodegenContext,
    private val protocol: ServerProtocol,
    private val operations: List<OperationShape>,
) {
    private val index = TopDownIndex.of(codegenContext.model)
    private val operations = index.getContainedOperations(codegenContext.serviceShape).toSortedSet(
        compareBy {
            it.id
        },
    ).toList()
    private val symbolProvider = codegenContext.symbolProvider
    private val libName = codegenContext.settings.moduleName.toSnakeCase()
    private val runtimeConfig = codegenContext.runtimeConfig
+0 −36
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.smithy.rust.codegen.server.python.smithy.generators

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerRustModule
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
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.generators.protocol.ServerProtocolGenerator

/**
 * PythonServerServiceGenerator
 *
 * Service generator is the main code generation entry point for Smithy services. Individual structures and unions are
 * generated in codegen visitor, but this class handles all protocol-specific code generation (i.e. operations).
 */
class PythonServerServiceGenerator(
    private val rustCrate: RustCrate,
    protocolGenerator: ServerProtocolGenerator,
    protocolSupport: ProtocolSupport,
    protocol: ServerProtocol,
    private val context: ServerCodegenContext,
) : ServerServiceGenerator(rustCrate, protocolGenerator, protocolSupport, protocol, context) {
    override fun renderExtras(operations: List<OperationShape>) {
        rustCrate.withModule(PythonServerRustModule.PythonServerApplication) {
            PythonApplicationGenerator(context, protocol, operations)
                .render(this)
        }
    }
}
+30 −8
Original line number Diff line number Diff line
@@ -68,6 +68,8 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerBuilde
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerEnumGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerOperationErrorGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerOperationGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerRootGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerRuntimeTypesReExportsGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerStructureConstrainedTraitImpl
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedCollectionGenerator
@@ -76,6 +78,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.Unconstraine
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolTestGenerator
import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader
import software.amazon.smithy.rust.codegen.server.smithy.traits.isReachableFromOperationInput
import software.amazon.smithy.rust.codegen.server.smithy.transformers.AttachValidationExceptionToConstrainedOperationInputsInAllowList
@@ -564,14 +567,33 @@ open class ServerCodegenVisitor(
     */
    override fun serviceShape(shape: ServiceShape) {
        logger.info("[rust-server-codegen] Generating a service $shape")
        val serverProtocol = protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol

        // Generate root
        rustCrate.lib {
            ServerRootGenerator(
                serverProtocol,
                codegenContext,
            ).render(this)
        }

        // Generate server re-exports
        rustCrate.withModule(ServerRustModule.Server) {
            ServerRuntimeTypesReExportsGenerator(codegenContext).render(this)
        }

        // Generate protocol tests
        rustCrate.withModule(ServerRustModule.Operation) {
            ServerProtocolTestGenerator(codegenContext, protocolGeneratorFactory.support(), protocolGenerator).render(this)
        }

        // Generate service module
        rustCrate.withModule(ServerRustModule.Service) {
            ServerServiceGenerator(
            rustCrate,
            protocolGenerator,
            protocolGeneratorFactory.support(),
            protocolGeneratorFactory.protocol(codegenContext) as ServerProtocol,
                codegenContext,
        )
            .render()
                serverProtocol,
            ).render(this)
        }
    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ object ServerRustModule {
    val Output = RustModule.public("output")
    val Types = RustModule.public("types")
    val Server = RustModule.public("server")
    val Service = RustModule.private("service")

    val UnconstrainedModule =
        software.amazon.smithy.rust.codegen.core.smithy.UnconstrainedModule
Loading