Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-export runtime types in server generated crates #1909

Merged
merged 9 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

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

import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType

class ServerRuntimeTypesReExportsGenerator(
codegenContext: CodegenContext,
) {
private val runtimeConfig = codegenContext.runtimeConfig
private val codegenScope = arrayOf(
"Router" to ServerRuntimeType.router(runtimeConfig),
"SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(runtimeConfig).toType(),
)

fun render(writer: RustWriter) {
writer.rustTemplate(
"""
pub mod body {
pub use #{SmithyHttpServer}::body::BoxBody;
}
pub mod operation {
pub use #{SmithyHttpServer}::operation::OperationShape;
pub use #{SmithyHttpServer}::operation::Operation;
}
pub mod plugin {
pub use #{SmithyHttpServer}::plugin::Plugin;
pub use #{SmithyHttpServer}::plugin::PluginPipeline;
pub use #{SmithyHttpServer}::plugin::PluginStack;
}
pub mod request {
pub use #{SmithyHttpServer}::request::FromParts;
}
pub mod response {
pub use #{SmithyHttpServer}::response::IntoResponse;
}
pub mod routing {
pub use #{SmithyHttpServer}::routing::IntoMakeService;
pub use #{SmithyHttpServer}::routing::IntoMakeServiceWithConnectInfo;
pub use #{SmithyHttpServer}::routing::Router;

##[cfg(feature = "aws-lambda")]
pub use #{SmithyHttpServer}::routing::LambdaHandler;
}

pub use #{SmithyHttpServer}::instrumentation;
pub use #{SmithyHttpServer}::proto;

pub use #{SmithyHttpServer}::Extension;
""",
*codegenScope,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ open class ServerServiceGenerator(
}

renderExtras(operations)

rustCrate.withModule(
RustModule.public(
"server",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively here we could

pub use aws_smithy_http_server as server;

Which would allow us to preserve the root crate docs but would mean that we re-export everything from the aws-smithy-http-server crate.

Copy link
Contributor

@hlbarber hlbarber Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conclusion of a discussion about this was: no, we don't want to do a blanket re-export for now. But we should ensure we preserve the layout between codegen items and aws-smithy-http-server items so that if we reconsider it will be a non-breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't server a confusing name? What about calling the module aws_smithy_http_server to better signal where we are re-exporting from? Not a blocker TBH.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to hide where re-exports are from and allow us to re-export also from other runtime crates possibly, probably we shouldn't signal the name of the crate

Copy link
Contributor

@LukeMathWalker LukeMathWalker Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like server is clear from context and it buys us a bit of flexibility, as @82marbag was saying.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allright! Thanks for the comments.

"""
Contains the types that are re-exported from the `aws-smithy-http-server` create.
""",
),
) {
renderServerReExports(this)
}
}

// Render any extra section needed by subclasses of `ServerServiceGenerator`.
Expand All @@ -315,4 +326,9 @@ open class ServerServiceGenerator(
private fun renderOperationRegistry(writer: RustWriter, operations: List<OperationShape>) {
ServerOperationRegistryGenerator(codegenContext, protocol, operations).render(writer)
}

// Render `server` crate, re-exporting types.
private fun renderServerReExports(writer: RustWriter) {
ServerRuntimeTypesReExportsGenerator(codegenContext).render(writer)
}
}