Skip to content

Commit 0b4c5ab

Browse files
authored
Break codegen-server's dependency on codegen-client (#2105)
* Move the allow lints customization into `codegen-core` * Move the crate version customization into `codegen-core` * Move "pub use" extra into `codegen-core` * Move `EventStreamSymbolProvider` into `codegen-core` * Move the streaming shape providers into `codegen-core` * Refactor event stream marshall/unmarshall tests * Break `codegen-server` dependency on `codegen-client` * Split up `EventStreamTestTools` * Move codegen context creation in event stream tests * Restructure tests so that #1442 is easier to resolve in the future * Add client/server prefixes to test classes * Improve TODO comments in server event stream tests * Use correct builders for `ServerEventStreamMarshallerGeneratorTest` * Remove test cases for protocols that don't support event streams
1 parent 3a3d121 commit 0b4c5ab

File tree

27 files changed

+1245
-1011
lines changed

27 files changed

+1245
-1011
lines changed

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.Non
2121
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider
2222
import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider
2323
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
24+
import software.amazon.smithy.rust.codegen.core.smithy.EventStreamSymbolProvider
25+
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeMetadataProvider
26+
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeSymbolProvider
2427
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor
2528
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig
2629
import java.util.logging.Level

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/RequiredCustomizations.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ package software.amazon.smithy.rust.codegen.client.smithy.customize
77

88
import software.amazon.smithy.model.shapes.OperationShape
99
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
10-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.AllowLintsGenerator
11-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.CrateVersionGenerator
1210
import software.amazon.smithy.rust.codegen.client.smithy.customizations.EndpointPrefixGenerator
1311
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpChecksumRequiredGenerator
1412
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpVersionListCustomization
1513
import software.amazon.smithy.rust.codegen.client.smithy.customizations.IdempotencyTokenGenerator
1614
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyConfigCustomization
1715
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyReExportCustomization
18-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.pubUseSmithyTypes
1916
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
2017
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
2118
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
19+
import software.amazon.smithy.rust.codegen.core.smithy.customizations.AllowLintsCustomization
20+
import software.amazon.smithy.rust.codegen.core.smithy.customizations.CrateVersionCustomization
21+
import software.amazon.smithy.rust.codegen.core.smithy.customizations.pubUseSmithyTypes
2222
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization
2323
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
2424

@@ -52,8 +52,7 @@ class RequiredCustomizations : ClientCodegenDecorator {
5252
codegenContext: ClientCodegenContext,
5353
baseCustomizations: List<LibRsCustomization>,
5454
): List<LibRsCustomization> =
55-
baseCustomizations + CrateVersionGenerator() +
56-
AllowLintsGenerator()
55+
baseCustomizations + CrateVersionCustomization() + AllowLintsCustomization()
5756

5857
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
5958
// Add rt-tokio feature for `ByteStream::from_path`

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/EventStreamSymbolProviderTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import software.amazon.smithy.model.shapes.ServiceShape
1212
import software.amazon.smithy.model.shapes.ShapeId
1313
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
1414
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
15+
import software.amazon.smithy.rust.codegen.core.smithy.EventStreamSymbolProvider
1516
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor
1617
import software.amazon.smithy.rust.codegen.core.smithy.rustType
1718
import software.amazon.smithy.rust.codegen.core.smithy.transformers.OperationNormalizer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.client.smithy.protocols.eventstream
7+
8+
import org.junit.jupiter.api.extension.ExtensionContext
9+
import org.junit.jupiter.params.provider.Arguments
10+
import org.junit.jupiter.params.provider.ArgumentsProvider
11+
import software.amazon.smithy.model.Model
12+
import software.amazon.smithy.model.shapes.ServiceShape
13+
import software.amazon.smithy.model.shapes.ShapeId
14+
import software.amazon.smithy.model.shapes.StructureShape
15+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
16+
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
17+
import software.amazon.smithy.rust.codegen.client.testutil.clientTestRustSettings
18+
import software.amazon.smithy.rust.codegen.client.testutil.testSymbolProvider
19+
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
20+
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
21+
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator
22+
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock
23+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
24+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestRequirements
25+
import java.util.stream.Stream
26+
27+
class TestCasesProvider : ArgumentsProvider {
28+
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> =
29+
EventStreamTestModels.TEST_CASES.map { Arguments.of(it) }.stream()
30+
}
31+
32+
abstract class ClientEventStreamBaseRequirements : EventStreamTestRequirements<ClientCodegenContext> {
33+
override fun createCodegenContext(
34+
model: Model,
35+
serviceShape: ServiceShape,
36+
protocolShapeId: ShapeId,
37+
codegenTarget: CodegenTarget,
38+
): ClientCodegenContext = ClientCodegenContext(
39+
model,
40+
testSymbolProvider(model),
41+
serviceShape,
42+
protocolShapeId,
43+
clientTestRustSettings(),
44+
CombinedClientCodegenDecorator(emptyList()),
45+
)
46+
47+
override fun renderBuilderForShape(
48+
writer: RustWriter,
49+
codegenContext: ClientCodegenContext,
50+
shape: StructureShape,
51+
) {
52+
BuilderGenerator(codegenContext.model, codegenContext.symbolProvider, shape).apply {
53+
render(writer)
54+
writer.implBlock(shape, codegenContext.symbolProvider) {
55+
renderConvenienceMethod(writer)
56+
}
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.client.smithy.protocols.eventstream
7+
8+
import org.junit.jupiter.params.ParameterizedTest
9+
import org.junit.jupiter.params.provider.ArgumentsSource
10+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
11+
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
12+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
13+
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
14+
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.EventStreamMarshallerGenerator
15+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
16+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestTools
17+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestVariety
18+
import software.amazon.smithy.rust.codegen.core.testutil.TestEventStreamProject
19+
import software.amazon.smithy.rust.codegen.core.testutil.TestRuntimeConfig
20+
21+
class ClientEventStreamMarshallerGeneratorTest {
22+
@ParameterizedTest
23+
@ArgumentsSource(TestCasesProvider::class)
24+
fun test(testCase: EventStreamTestModels.TestCase) {
25+
EventStreamTestTools.runTestCase(
26+
testCase,
27+
object : ClientEventStreamBaseRequirements() {
28+
override fun renderGenerator(
29+
codegenContext: ClientCodegenContext,
30+
project: TestEventStreamProject,
31+
protocol: Protocol,
32+
): RuntimeType = EventStreamMarshallerGenerator(
33+
project.model,
34+
CodegenTarget.CLIENT,
35+
TestRuntimeConfig,
36+
project.symbolProvider,
37+
project.streamShape,
38+
protocol.structuredDataSerializer(project.operationShape),
39+
testCase.requestContentType,
40+
).render()
41+
},
42+
CodegenTarget.CLIENT,
43+
EventStreamTestVariety.Marshall,
44+
)
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.client.smithy.protocols.eventstream
7+
8+
import org.junit.jupiter.params.ParameterizedTest
9+
import org.junit.jupiter.params.provider.ArgumentsSource
10+
import software.amazon.smithy.codegen.core.Symbol
11+
import software.amazon.smithy.model.shapes.StructureShape
12+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
13+
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
14+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
15+
import software.amazon.smithy.rust.codegen.core.smithy.generators.builderSymbol
16+
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
17+
import software.amazon.smithy.rust.codegen.core.smithy.protocols.parse.EventStreamUnmarshallerGenerator
18+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
19+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestTools
20+
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestVariety
21+
import software.amazon.smithy.rust.codegen.core.testutil.TestEventStreamProject
22+
23+
class ClientEventStreamUnmarshallerGeneratorTest {
24+
@ParameterizedTest
25+
@ArgumentsSource(TestCasesProvider::class)
26+
fun test(testCase: EventStreamTestModels.TestCase) {
27+
EventStreamTestTools.runTestCase(
28+
testCase,
29+
object : ClientEventStreamBaseRequirements() {
30+
override fun renderGenerator(
31+
codegenContext: ClientCodegenContext,
32+
project: TestEventStreamProject,
33+
protocol: Protocol,
34+
): RuntimeType {
35+
fun builderSymbol(shape: StructureShape): Symbol = shape.builderSymbol(codegenContext.symbolProvider)
36+
return EventStreamUnmarshallerGenerator(
37+
protocol,
38+
codegenContext,
39+
project.operationShape,
40+
project.streamShape,
41+
::builderSymbol,
42+
).render()
43+
}
44+
},
45+
CodegenTarget.CLIENT,
46+
EventStreamTestVariety.Unmarshall,
47+
)
48+
}
49+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package software.amazon.smithy.rust.codegen.client.smithy
6+
package software.amazon.smithy.rust.codegen.core.smithy
77

88
import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.model.Model
@@ -14,13 +14,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
1414
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
1515
import software.amazon.smithy.rust.codegen.core.rustlang.render
1616
import software.amazon.smithy.rust.codegen.core.rustlang.stripOuter
17-
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
18-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
19-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
20-
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
21-
import software.amazon.smithy.rust.codegen.core.smithy.WrappingSymbolProvider
2217
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.eventStreamErrorSymbol
23-
import software.amazon.smithy.rust.codegen.core.smithy.rustType
2418
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
2519
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticOutputTrait
2620
import software.amazon.smithy.rust.codegen.core.smithy.transformers.eventStreamErrors
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package software.amazon.smithy.rust.codegen.client.smithy
6+
package software.amazon.smithy.rust.codegen.core.smithy
77

88
import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.model.Model
@@ -14,13 +14,6 @@ import software.amazon.smithy.model.shapes.StringShape
1414
import software.amazon.smithy.model.shapes.StructureShape
1515
import software.amazon.smithy.model.shapes.UnionShape
1616
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
17-
import software.amazon.smithy.rust.codegen.core.smithy.Default
18-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
19-
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
20-
import software.amazon.smithy.rust.codegen.core.smithy.SymbolMetadataProvider
21-
import software.amazon.smithy.rust.codegen.core.smithy.WrappingSymbolProvider
22-
import software.amazon.smithy.rust.codegen.core.smithy.expectRustMetadata
23-
import software.amazon.smithy.rust.codegen.core.smithy.setDefault
2417
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
2518
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticOutputTrait
2619
import software.amazon.smithy.rust.codegen.core.util.hasStreamingMember
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package software.amazon.smithy.rust.codegen.client.smithy.customizations
6+
package software.amazon.smithy.rust.codegen.core.smithy.customizations
77

88
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
99
import software.amazon.smithy.rust.codegen.core.rustlang.writable
1010
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
1111
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
1212

13-
val AllowedRustcLints = listOf(
13+
private val allowedRustcLints = listOf(
1414
// Deprecated items should be safe to compile, so don't block the compilation.
1515
"deprecated",
1616
)
1717

18-
val AllowedClippyLints = listOf(
18+
private val allowedClippyLints = listOf(
1919
// Sometimes operations are named the same as our module e.g. output leading to `output::output`.
2020
"module_inception",
2121

@@ -54,16 +54,16 @@ val AllowedClippyLints = listOf(
5454
// "result_large_err",
5555
)
5656

57-
val AllowedRustdocLints = listOf(
57+
private val allowedRustdocLints = listOf(
5858
// Rust >=1.53.0 requires links to be wrapped in `<link>`. This is extremely hard to enforce for
5959
// docs that come from the modeled documentation, so we need to disable this lint
6060
"bare_urls",
6161
)
6262

63-
class AllowLintsGenerator(
64-
private val rustcLints: List<String> = AllowedRustcLints,
65-
private val clippyLints: List<String> = AllowedClippyLints,
66-
private val rustdocLints: List<String> = AllowedRustdocLints,
63+
class AllowLintsCustomization(
64+
private val rustcLints: List<String> = allowedRustcLints,
65+
private val clippyLints: List<String> = allowedClippyLints,
66+
private val rustdocLints: List<String> = allowedRustdocLints,
6767
) : LibRsCustomization() {
6868
override fun section(section: LibRsSection) = when (section) {
6969
is LibRsSection.Attributes -> writable {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package software.amazon.smithy.rust.codegen.client.smithy.customizations
6+
package software.amazon.smithy.rust.codegen.core.smithy.customizations
77

88
import software.amazon.smithy.rust.codegen.core.rustlang.rust
99
import software.amazon.smithy.rust.codegen.core.rustlang.writable
@@ -13,7 +13,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
1313
/**
1414
* Add `PGK_VERSION` const in lib.rs to enable knowing the version of the current module
1515
*/
16-
class CrateVersionGenerator : LibRsCustomization() {
16+
class CrateVersionCustomization : LibRsCustomization() {
1717
override fun section(section: LibRsSection) =
1818
writable {
1919
if (section is LibRsSection.Body) {

0 commit comments

Comments
 (0)