-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harry Barber
committed
Jun 14, 2023
1 parent
19b206f
commit 8564c71
Showing
5 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...otlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ScopeMacroGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* 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.model.knowledge.TopDownIndex | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency | ||
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext | ||
|
||
class ScopeMacroGenerator( | ||
private val codegenContext: ServerCodegenContext, | ||
) { | ||
private val runtimeConfig = codegenContext.runtimeConfig | ||
private val codegenScope = | ||
arrayOf( | ||
"SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(runtimeConfig).toType(), | ||
) | ||
|
||
/** Calculate all `operationShape`s contained within the `ServiceShape`. */ | ||
private val index = TopDownIndex.of(codegenContext.model) | ||
private val operations = index.getContainedOperations(codegenContext.serviceShape).toSortedSet(compareBy { it.id }) | ||
|
||
fun macro(): Writable = writable { | ||
val firstOperationName = codegenContext.symbolProvider.toSymbol(operations.first()).name | ||
val operationNames = operations | ||
.map { codegenContext.symbolProvider.toSymbol(it).name } | ||
.joinToString(" ") | ||
val operationBranches = operations | ||
.map { codegenContext.symbolProvider.toSymbol(it).name } | ||
.map { | ||
""" | ||
// $it Match found, pop from both `member` and `not_member` | ||
(@ $ name: ident, $ predicate: ident ($it $($ member: ident)*) ($($ temp: ident)*) ($it $($ not_member: ident)*)) => { | ||
scope! { @ $ name, $ predicate ($($ member)*) ($($ temp)*) ($($ not_member)*) } | ||
}; | ||
// $it match not found, pop from `not_member` into `temp` stack | ||
(@ $ name: ident, $ predicate: ident ($it $($ member: ident)*) ($($ temp: ident)*) ($ other: ident $($ not_member: ident)*)) => { | ||
scope! { @ $ name, $ predicate ($it $($ member)*) ($ other $($ temp)*) ($($ not_member)*) } | ||
}; | ||
""" | ||
}.joinToString("") | ||
|
||
rustTemplate( | ||
""" | ||
/// A macro to help with scoping plugins to a subset of all operations. | ||
/// | ||
/// In contrast to [`aws_smithy_http_server::scope`](#{SmithyHttpServer}::scope), this macro has knowledge | ||
/// of the service and any operations _not_ specified will be placed in the opposing group. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```rust | ||
/// scope! { | ||
/// /// Includes [`$firstOperationName`], excluding all other operations | ||
/// struct ScopeA { | ||
/// includes: [$firstOperationName], | ||
/// } | ||
/// } | ||
/// | ||
/// scope! { | ||
/// /// Excludes [`$firstOperationName`], excluding all other operations | ||
/// struct ScopeB { | ||
/// excludes: [$firstOperationName] | ||
/// } | ||
/// } | ||
/// ``` | ||
##[macro_export] | ||
macro_rules! scope { | ||
// Completed, render impls | ||
(@ $ name: ident, $ predicate: ident () ($($ temp: ident)*) ($($ not_member: ident)*)) => { | ||
$( | ||
impl #{SmithyHttpServer}::plugin::scoped::Membership<$ temp> for $ name { | ||
type Contains = #{SmithyHttpServer}::plugin::scoped::$ predicate; | ||
} | ||
)* | ||
$( | ||
impl #{SmithyHttpServer}::plugin::scoped::Membership<$ not_member> for $ name { | ||
type Contains = #{SmithyHttpServer}::plugin::scoped::$ predicate; | ||
} | ||
)* | ||
}; | ||
// All `not_member`s exhausted, move `temp` into `not_member` | ||
(@ $ name: ident, $ predicate: ident ($($ member: ident)*) ($($ temp: ident)*) ()) => { | ||
scope! { @ $ name, $ predicate ($($ member)*) () ($($ temp)*) } | ||
}; | ||
$operationBranches | ||
( | ||
$(##[$ attrs:meta])* | ||
$ vis:vis struct $ name:ident { | ||
includes: [$($ include:ident),*] | ||
} | ||
) => { | ||
use $ crate::operation_shape::*; | ||
use #{SmithyHttpServer}::scope as scope_runtime; | ||
scope_runtime! { | ||
$(##[$ attrs])* | ||
$ vis struct $ name { | ||
includes: [$($ include),*], | ||
excludes: [] | ||
} | ||
} | ||
scope! { @ $ name, False ($($ include)*) () ($operationNames) } | ||
}; | ||
( | ||
$(##[$ attrs:meta])* | ||
$ vis:vis struct $ name:ident { | ||
excludes: [$($ exclude:ident),*] | ||
} | ||
) => { | ||
use $ crate::operation_shape::*; | ||
use #{SmithyHttpServer}::scope as scope_runtime; | ||
scope_runtime! { | ||
$(##[$ attrs])* | ||
$ vis struct $ name { | ||
includes: [], | ||
excludes: [$($ exclude),*] | ||
} | ||
} | ||
scope! { @ $ name, True ($($ exclude)*) () ($operationNames) } | ||
}; | ||
} | ||
""", | ||
*codegenScope, | ||
) | ||
} | ||
|
||
fun render(writer: RustWriter) { | ||
macro()(writer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters