-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for sparse datastructures & blobs
- Loading branch information
Showing
7 changed files
with
640 additions
and
437 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
codegen-serde/src/main/kotlin/software/amazon/smithy/rust/codegen/serde/SerdeDecorator.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,80 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.serde | ||
|
||
import software.amazon.smithy.model.neighbor.Walker | ||
import software.amazon.smithy.model.shapes.Shape | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Feature | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule | ||
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate | ||
import software.amazon.smithy.rust.codegen.core.util.hasTrait | ||
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext | ||
import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator | ||
|
||
val SerdeFeature = Feature("serde", false, listOf("dep:serde")) | ||
val Module = | ||
RustModule.public( | ||
"serde_impl", | ||
additionalAttributes = listOf(Attribute.featureGate(SerdeFeature.name)), | ||
documentationOverride = "Implementations of `serde` for model types", | ||
) | ||
|
||
class ClientSerdeDecorator : ClientCodegenDecorator { | ||
override val name: String = "ClientSerdeDecorator" | ||
override val order: Byte = 0 | ||
|
||
override fun extras( | ||
codegenContext: ClientCodegenContext, | ||
rustCrate: RustCrate, | ||
) { | ||
rustCrate.mergeFeature(SerdeFeature) | ||
val generator = SerializeImplGenerator(codegenContext) | ||
rustCrate.withModule(Module) { | ||
serializationRoots(codegenContext).forEach { | ||
generator.generateRootSerializerForShape( | ||
it, | ||
)(this) | ||
} | ||
addDependency(SupportStructures.serializeRedacted().toSymbol()) | ||
addDependency(SupportStructures.serializeUnredacted().toSymbol()) | ||
} | ||
} | ||
} | ||
|
||
class ServerSerdeDecorator : ServerCodegenDecorator { | ||
override val name: String = "ServerSerdeDecorator" | ||
override val order: Byte = 0 | ||
|
||
override fun extras( | ||
codegenContext: ServerCodegenContext, | ||
rustCrate: RustCrate, | ||
) { | ||
rustCrate.mergeFeature(SerdeFeature) | ||
val generator = SerializeImplGenerator(codegenContext) | ||
rustCrate.withModule(Module) { | ||
serializationRoots(codegenContext).forEach { | ||
generator.generateRootSerializerForShape( | ||
it, | ||
)(this) | ||
} | ||
addDependency(SupportStructures.serializeRedacted().toSymbol()) | ||
addDependency(SupportStructures.serializeUnredacted().toSymbol()) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* All entry points for serialization in the service closure. | ||
*/ | ||
fun serializationRoots(ctx: CodegenContext): List<Shape> { | ||
val serviceShape = ctx.serviceShape | ||
val walker = Walker(ctx.model) | ||
return walker.walkShapes(serviceShape).filter { it.hasTrait<SerdeTrait>() } | ||
} |
Oops, something went wrong.