Skip to content

Commit 52865bb

Browse files
Rfc30/feature gate for generated crates (#2183)
* add CfgUnstable for feature-gate * add features * add feature-gate * strings for feature gate * Revert "strings for feature gate" This reverts commit 1f33a5c. * Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt Co-authored-by: Zelda Hessler <zelda.hessler@pm.me> * fix dependency thing on cargo * add OutputShape to builder * EnumGenerator * StructureGenerator * UnionGenerator * todo * fixed? * SerdeDecorator * codegen stuff * update * fix * Apply suggestions from code review Co-authored-by: Zelda Hessler <zelda.hessler@pm.me> * - refactoring - pre-commit - https://github.com/awslabs/smithy-rs/pull/2183/files#r1080594621 * adds serde-serialize/deserialize * this one causes null pointer exception * interim solution * new push * fix * add Sensitive Warning * add test for CargoTomlGeneratorTest pre-commit fix * Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt Co-authored-by: Zelda Hessler <zelda.hessler@pm.me> --------- Co-authored-by: Zelda Hessler <zelda.hessler@pm.me>
1 parent 35cd929 commit 52865bb

File tree

14 files changed

+145
-8
lines changed

14 files changed

+145
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegen
1414
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
1515
import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStreamSigningDecorator
1616
import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations
17+
import software.amazon.smithy.rust.codegen.client.smithy.customize.SerdeDecorator
1718
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator
1819
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator
1920
import software.amazon.smithy.rust.codegen.client.testutil.DecoratableBuildPlugin
@@ -53,6 +54,7 @@ class RustClientCodegenPlugin : DecoratableBuildPlugin() {
5354
val codegenDecorator =
5455
CombinedClientCodegenDecorator.fromClasspath(
5556
context,
57+
SerdeDecorator(),
5658
ClientCustomizations(),
5759
RequiredCustomizations(),
5860
FluentClientDecorator(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.customize
7+
8+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
9+
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
10+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
11+
12+
/**
13+
* This class,
14+
* - Adds serde as a dependency
15+
*
16+
*/
17+
class SerdeDecorator : ClientCodegenDecorator {
18+
override val name: String = "SerdeDecorator"
19+
override val order: Byte = -1
20+
21+
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
22+
fun _feature(feature_name: String, crate_name: String): Feature {
23+
return Feature(feature_name, false, listOf(crate_name + "/" + feature_name))
24+
}
25+
rustCrate.mergeFeature(_feature("serde-serialize", "aws-smithy-types"))
26+
rustCrate.mergeFeature(_feature("serde-deserialize", "aws-smithy-types"))
27+
}
28+
}

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/CargoDependency.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.nio.file.Path
1616
sealed class DependencyScope {
1717
object Dev : DependencyScope()
1818
object Compile : DependencyScope()
19+
object CfgUnstable : DependencyScope()
1920
}
2021

2122
sealed class DependencyLocation
@@ -245,5 +246,8 @@ data class CargoDependency(
245246
fun smithyQuery(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-query")
246247
fun smithyTypes(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-types")
247248
fun smithyXml(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-xml")
249+
250+
// behind feature-gate
251+
val Serde = CargoDependency("serde", CratesIo("1.0"), features = setOf("derive"), scope = DependencyScope.CfgUnstable)
248252
}
249253
}

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,16 @@ class Attribute(val inner: Writable) {
446446
}
447447
}
448448

449+
// These were supposed to be a part of companion object but we decided to move it out to here to avoid NPE
450+
// You can find the discussion here.
451+
// https://github.com/awslabs/smithy-rs/discussions/2248
452+
public fun SerdeSerialize(): Attribute {
453+
return Attribute(cfgAttr(all(writable("aws_sdk_unstable"), feature("serde-serialize")), derive(RuntimeType.SerdeSerialize)))
454+
}
455+
public fun SerdeDeserialize(): Attribute {
456+
return Attribute(cfgAttr(all(writable("aws_sdk_unstable"), feature("serde-deserialize")), derive(RuntimeType.SerdeDeserialize)))
457+
}
458+
449459
companion object {
450460
val AllowClippyBoxedLocal = Attribute(allow("clippy::boxed_local"))
451461
val AllowClippyLetAndReturn = Attribute(allow("clippy::let_and_return"))
@@ -499,6 +509,7 @@ class Attribute(val inner: Writable) {
499509
}
500510

501511
fun all(vararg attrMacros: Writable): Writable = macroWithArgs("all", *attrMacros)
512+
fun cfgAttr(vararg attrMacros: Writable): Writable = macroWithArgs("cfg_attr", *attrMacros)
502513

503514
fun allow(lints: Collection<String>): Writable = macroWithArgs("allow", *lints.toTypedArray())
504515
fun allow(vararg lints: String): Writable = macroWithArgs("allow", *lints)

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ data class RuntimeType(val path: String, val dependency: RustDependency? = null)
240240
val ConstrainedTrait = RuntimeType("crate::constrained::Constrained", InlineDependency.constrained())
241241
val MaybeConstrained = RuntimeType("crate::constrained::MaybeConstrained", InlineDependency.constrained())
242242

243+
// serde types. Gated behind `CfgUnstable`.
244+
val Serde = CargoDependency.Serde.toType()
245+
val SerdeSerialize = Serde.resolve("Serialize")
246+
val SerdeDeserialize = Serde.resolve("Deserialize")
247+
243248
// smithy runtime types
244249
fun smithyAsync(runtimeConfig: RuntimeConfig) = CargoDependency.smithyAsync(runtimeConfig).toType()
245250
fun smithyChecksums(runtimeConfig: RuntimeConfig) = CargoDependency.smithyChecksums(runtimeConfig).toType()

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
package software.amazon.smithy.rust.codegen.core.smithy.generators
7-
87
import software.amazon.smithy.codegen.core.Symbol
98
import software.amazon.smithy.codegen.core.SymbolProvider
109
import software.amazon.smithy.model.Model
@@ -27,6 +26,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.documentShape
2726
import software.amazon.smithy.rust.codegen.core.rustlang.render
2827
import software.amazon.smithy.rust.codegen.core.rustlang.rust
2928
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
29+
import software.amazon.smithy.rust.codegen.core.rustlang.rustInline
3030
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
3131
import software.amazon.smithy.rust.codegen.core.rustlang.stripOuter
3232
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
@@ -43,6 +43,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.locatedIn
4343
import software.amazon.smithy.rust.codegen.core.smithy.makeOptional
4444
import software.amazon.smithy.rust.codegen.core.smithy.module
4545
import software.amazon.smithy.rust.codegen.core.smithy.rustType
46+
import software.amazon.smithy.rust.codegen.core.smithy.shape
4647
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
4748
import software.amazon.smithy.rust.codegen.core.util.dq
4849
import software.amazon.smithy.rust.codegen.core.util.hasTrait
@@ -206,13 +207,19 @@ class BuilderGenerator(
206207
}
207208

208209
private fun renderBuilder(writer: RustWriter) {
210+
writer.docs("This is the datatype returned when calling `Builder::build()`.")
211+
writer.rustInline("pub type OutputShape = #T;", structureSymbol)
209212
writer.docs("A builder for #D.", structureSymbol)
210213
Attribute(derive(builderDerives)).render(writer)
214+
RenderSerdeAttribute.forStructureShape(writer, shape, model)
215+
SensitiveWarning.addDoc(writer, shape)
211216
writer.rustBlock("pub struct $builderName") {
217+
// add serde
212218
for (member in members) {
213219
val memberName = symbolProvider.toMemberName(member)
214220
// All fields in the builder are optional.
215221
val memberSymbol = symbolProvider.toSymbol(member).makeOptional()
222+
SensitiveWarning.addDoc(writer, member)
216223
renderBuilderMember(this, memberName, memberSymbol)
217224
}
218225
}

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/CargoTomlGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class CargoTomlGenerator(
7474
.associate { it.name to it.toMap() },
7575
"dev-dependencies" to dependencies.filter { it.scope == DependencyScope.Dev }
7676
.associate { it.name to it.toMap() },
77+
"target.'cfg(aws_sdk_unstable)'.dependencies" to dependencies.filter { it.scope == DependencyScope.CfgUnstable }
78+
.associate { it.name to it.toMap() },
7779
"features" to cargoFeatures.toMap(),
7880
).deepMergeWith(manifestCustomizations)
7981

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
package software.amazon.smithy.rust.codegen.core.smithy.generators
7-
87
import software.amazon.smithy.codegen.core.Symbol
98
import software.amazon.smithy.model.Model
109
import software.amazon.smithy.model.shapes.StringShape
@@ -139,6 +138,8 @@ open class EnumGenerator(
139138
private fun renderUnnamedEnum() {
140139
writer.documentShape(shape, model)
141140
writer.deprecatedShape(shape)
141+
RenderSerdeAttribute.writeAttributes(writer)
142+
SensitiveWarning.addDoc(writer, shape)
142143
meta.render(writer)
143144
writer.write("struct $enumName(String);")
144145
writer.rustBlock("impl $enumName") {
@@ -178,7 +179,8 @@ open class EnumGenerator(
178179
renamedWarning.ifBlank { null },
179180
)
180181
writer.deprecatedShape(shape)
181-
182+
RenderSerdeAttribute.writeAttributes(writer)
183+
SensitiveWarning.addDoc(writer, shape)
182184
meta.render(writer)
183185
writer.rustBlock("enum $enumName") {
184186
sortedMembers.forEach { member -> member.render(writer) }
@@ -225,6 +227,7 @@ open class EnumGenerator(
225227
part of the enums that are public interface.
226228
""".trimIndent(),
227229
)
230+
// adding serde features here adds attribute to the end of the file for some reason
228231
meta.render(this)
229232
rust("struct $UnknownVariantValue(pub(crate) String);")
230233
rustBlock("impl $UnknownVariantValue") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.core.smithy.generators
7+
8+
import software.amazon.smithy.model.Model
9+
import software.amazon.smithy.model.shapes.StructureShape
10+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
11+
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
12+
import software.amazon.smithy.rust.codegen.core.util.isEventStream
13+
14+
public object RenderSerdeAttribute {
15+
public fun forStructureShape(writer: RustWriter, shape: StructureShape, model: Model) {
16+
if (shape.members().none { it.isEventStream(model) }) {
17+
writeAttributes(writer)
18+
}
19+
}
20+
21+
public fun writeAttributes(writer: RustWriter) {
22+
Attribute("").SerdeSerialize().render(writer)
23+
Attribute("").SerdeDeserialize().render(writer)
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.core.smithy.generators
7+
8+
import software.amazon.smithy.model.shapes.Shape
9+
import software.amazon.smithy.model.traits.SensitiveTrait
10+
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
11+
import software.amazon.smithy.rust.codegen.core.util.hasTrait
12+
13+
object SensitiveWarning {
14+
private const val warningMessage = "/// This data may contain sensitive information; It will not be obscured when serialized.\n"
15+
fun<T : Shape> addDoc(writer: RustWriter, shape: T) {
16+
if (shape.hasTrait<SensitiveTrait>()) {
17+
writer.writeInline(warningMessage)
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)