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

RFC30: add Serde Decorator to RustClientCodegenPlugin #2650

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
2c262f9
serde decorator
thomas-k-cameron Apr 27, 2023
3645285
update
thomas-k-cameron Apr 27, 2023
fbcbf04
update
thomas-k-cameron Apr 27, 2023
263eb86
update
thomas-k-cameron Apr 27, 2023
2b1ee72
update
thomas-k-cameron Apr 27, 2023
0b01612
update
thomas-k-cameron Apr 27, 2023
aa9982c
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Apr 28, 2023
06a3cba
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron May 31, 2023
6e38a83
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Jun 14, 2023
495ad68
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Jun 14, 2023
a3e8546
Update codegen-client/src/main/kotlin/software/amazon/smithy/rust/cod…
thomas-k-cameron Jun 29, 2023
c49b208
update
thomas-k-cameron Jul 4, 2023
1c24991
update
thomas-k-cameron Jul 4, 2023
5b5f201
update
thomas-k-cameron Jul 4, 2023
81f7efd
update
thomas-k-cameron Jul 4, 2023
e6dbd97
update
thomas-k-cameron Jul 4, 2023
9e997b6
update
thomas-k-cameron Jul 4, 2023
bbb321e
asdf
thomas-k-cameron Jul 16, 2023
fa8c04b
asdf
thomas-k-cameron Jul 16, 2023
918f2cd
Update LibRsGenerator.kt
thomas-k-cameron Jul 16, 2023
8802e83
Update SerdeDecorator.kt
thomas-k-cameron Jul 16, 2023
5211559
asdf
thomas-k-cameron Jul 16, 2023
88c14cb
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Jul 16, 2023
c4a41c2
update
thomas-k-cameron Jul 16, 2023
dd4a511
asdf
thomas-k-cameron Jul 17, 2023
982a95d
Delete devcontainer.json
thomas-k-cameron Jul 18, 2023
954b7e9
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Jul 20, 2023
de2cdd4
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Sep 6, 2023
a67a879
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Sep 7, 2023
cbe908a
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Nov 7, 2023
7251962
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Dec 7, 2023
44e5fc4
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Dec 9, 2023
8a6ff7d
update
thomas-k-cameron Dec 11, 2023
40840ae
Merge branch 'RFC30/serde-decorator' of https://github.com/thomas-k-c…
thomas-k-cameron Dec 11, 2023
dc09315
update
thomas-k-cameron Dec 11, 2023
db679e0
update
thomas-k-cameron Dec 11, 2023
d1b369e
update
thomas-k-cameron Dec 11, 2023
0b8ba89
update
thomas-k-cameron Dec 11, 2023
077d868
asdf
thomas-k-cameron Dec 11, 2023
348db68
modified: codegen-client/src/main/kotlin/software/amazon/smithy/rus…
thomas-k-cameron Dec 11, 2023
476df23
update
thomas-k-cameron Dec 11, 2023
58c4c53
update
thomas-k-cameron Dec 11, 2023
68a60df
update
thomas-k-cameron Dec 11, 2023
abdf723
modified: codegen-client/src/main/kotlin/software/amazon/smithy/rus…
thomas-k-cameron Dec 11, 2023
1c5fd9a
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron Feb 4, 2024
366a43e
Merge branch 'main' into RFC30/serde-decorator
thomas-k-cameron May 2, 2024
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
Expand Up @@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.Sensitiv
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations
import software.amazon.smithy.rust.codegen.client.smithy.customize.SerdeDecorator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointParamsDecorator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator
Expand Down Expand Up @@ -59,6 +60,7 @@ class RustClientCodegenPlugin : ClientDecoratableBuildPlugin() {
val codegenDecorator =
CombinedClientCodegenDecorator.fromClasspath(
context,
SerdeDecorator(),
ClientCustomizations(),
RequiredCustomizations(),
FluentClientDecorator(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rust.codegen.client.smithy.customize

import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.containerDocs
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.core.smithy.generators.ModuleDocSection

/**
* Decorator that adds the `serde-serialize` and `serde-deserialize` features.
*/
class SerdeDecorator : ClientCodegenDecorator {
override val name: String = "SerdeDecorator"
override val order: Byte = 5

override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be good to also hook into the ModuleDocSection.ServiceDocs, or even add a new ModuleDocSection.Features, so that these features can be described in the docs. That way, the documentation can inform on the --cfg aws_sdk_unstable requirement. This would require registering a LibRsCustomization, so override fun libRsCustomizations(...).

There's an example here that adds information to the "Crate Organization" documentation section: https://github.com/awslabs/smithy-rs/blob/2f60a5e0904037ec5237c080c61817617b9d8c06/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customizations/ClientCustomizations.kt#L19

Copy link
Contributor Author

@thomas-k-cameron thomas-k-cameron Jul 4, 2023

Choose a reason for hiding this comment

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

I added this function but it seems like there are no changes to the generated code.

Is there anything else that I'm supposed to add?

fun feature(featureName: String): Feature {
return Feature(featureName, false, listOf("aws-smithy-types/$featureName"))
}
rustCrate.mergeFeature(feature("serde-serialize"))
rustCrate.mergeFeature(feature("serde-deserialize"))
}

override fun libRsCustomizations(
codegenContext: ClientCodegenContext,
baseCustomizations: List<LibRsCustomization>,
): List<LibRsCustomization> {
return baseCustomizations + SerdeDocGenerator(codegenContext)
}
}

class SerdeDocGenerator(private val codegenContext: ClientCodegenContext) : LibRsCustomization() {
override fun section(section: LibRsSection): Writable {
return when (section) {
is LibRsSection.ModuleDoc ->
if (section.subsection is ModuleDocSection.AWSSdkUnstable) {
serdeInfoText()
} else {
emptySection
}

else -> emptySection
}
}

private fun serdeInfoText(): Writable {
return writable {
containerDocs(
"""
## How to enable `Serialize` and `Deserialize`

This data type implements `Serialize` and `Deserialize` traits from the popular serde crate, but those traits are behind feature gate.

As they increase it's compile time dramatically, you should not turn them on unless it's necessary.
Implementation of `serde` traits in AWS SDK for Rust is still unstable, and implementation may change anytime in future.

To enable traits, you must pass `aws_sdk_unstable` to RUSTFLAGS and enable `serde-serialize` or `serde-deserialize` feature.

e.g.
```bash,no_run
export RUSTFLAGS="--cfg aws_sdk_unstable"
cargo build --features serde-serialize serde-deserialize
```

If you enable `serde-serialize` and/or `serde-deserialize` without `RUSTFLAGS="--cfg aws_sdk_unstable"`,
compilation will fail with warning.

""".trimIndent(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sealed class ModuleDocSection {
object CrateOrganization : ModuleDocSection()

object Examples : ModuleDocSection()
object AWSSdkUnstable : ModuleDocSection()
}

sealed class LibRsSection(name: String) : Section(name) {
Expand Down Expand Up @@ -78,6 +79,15 @@ class LibRsGenerator(
}
}

docSection(ModuleDocSection.AWSSdkUnstable).also { docs ->
if (docs.isNotEmpty()) {
containerDocs("\n## Enabling Unstable Features")
docs.forEach { writeTo ->
writeTo(this)
}
}
}

// Examples
docSection(ModuleDocSection.Examples).also { docs ->
if (docs.isNotEmpty() || settings.examplesUri != null) {
Expand Down
Loading