Skip to content

Commit

Permalink
Build services with a derived configuration object
Browse files Browse the repository at this point in the history
This is a pared-down version of #2809.

This is the code-generated version of the
david-perez/smithy-rs-service-config#2 POC.

This introduces a code-generated `Config` object which is provided to
the service builder constructor via `PokemonService::builder(config:
Config)`. This will displace the current `builder_without_plugins` and
`builder_with_plugins`, deprecating them. We will eventually remove
them.

The motivation is to have a single place where to register plugins and
layers. Smithy traits can leverage this object to inject methods that
can apply plugins and layers. For example, an `@authorization` trait
implementer could vend a smithy-rs decorator that pre-applied an
authorization plugin inside the `Config` object, possibly exposing a
method to pass in any runtime arguments needed to configure the
middleware.
  • Loading branch information
david-perez committed Oct 25, 2023
1 parent 08a533f commit decd5a4
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,7 @@ class RustWriter private constructor(
if (this.className.contains("AbstractCodeWriter") || this.className.startsWith("java.lang")) {
return false
}
if (this.fileName == "RustWriter.kt") {
return false
}
return true
return this.fileName != "RustWriter.kt"
}

private val preamble = mutableListOf<Writable>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerRootGe
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.ServiceConfigGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedCollectionGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedMapGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedUnionGenerator
Expand Down Expand Up @@ -613,7 +614,10 @@ open class ServerCodegenVisitor(
serverProtocol,
).render(this)

ServiceConfigGenerator(codegenContext).render(this)

ScopeMacroGenerator(codegenContext).render(this)

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ open class ServerRootGenerator(
//! let http_plugins = HttpPlugins::new()
//! .push(LoggingPlugin)
//! .push(MetricsPlugin);
//! let builder: $builderName<Body, _, _> = $serviceName::builder_with_plugins(http_plugins, IdentityPlugin);
//! let builder: $builderName<Body, _, _, _> = $serviceName::builder_with_plugins(http_plugins, IdentityPlugin);
//! ```
//!
//! Check out [`#{SmithyHttpServer}::plugin`] to learn more about plugins.
Expand Down Expand Up @@ -237,6 +237,6 @@ open class ServerRootGenerator(
fun render(rustWriter: RustWriter) {
documentation(rustWriter)

rustWriter.rust("pub use crate::service::{$serviceName, ${serviceName}Builder, MissingOperationsError};")
rustWriter.rust("pub use crate::service::{$serviceName, ${serviceName}Builder, Config, MissingOperationsError};")
}
}
Loading

0 comments on commit decd5a4

Please sign in to comment.