-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daniele Ahmed <[email protected]>
- Loading branch information
Showing
9 changed files
with
131 additions
and
74 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
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
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
75 changes: 75 additions & 0 deletions
75
...in/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderGenerator.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,75 @@ | ||
/* | ||
* 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.Model | ||
import software.amazon.smithy.model.node.BooleanNode | ||
import software.amazon.smithy.model.node.NullNode | ||
import software.amazon.smithy.model.node.NumberNode | ||
import software.amazon.smithy.model.node.StringNode | ||
import software.amazon.smithy.model.shapes.BooleanShape | ||
import software.amazon.smithy.model.shapes.DocumentShape | ||
import software.amazon.smithy.model.shapes.EnumShape | ||
import software.amazon.smithy.model.shapes.FloatShape | ||
import software.amazon.smithy.model.shapes.IntEnumShape | ||
import software.amazon.smithy.model.shapes.IntegerShape | ||
import software.amazon.smithy.model.shapes.ListShape | ||
import software.amazon.smithy.model.shapes.MapShape | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.StringShape | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import software.amazon.smithy.model.traits.DefaultTrait | ||
import software.amazon.smithy.model.traits.EnumDefinition | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator | ||
import software.amazon.smithy.rust.codegen.core.util.dq | ||
import software.amazon.smithy.rust.codegen.core.util.expectTrait | ||
|
||
class ServerBuilderGenerator( | ||
private val model: Model, | ||
private val symbolProvider: RustSymbolProvider, | ||
shape: StructureShape, | ||
) : BuilderGenerator(model, symbolProvider, shape) { | ||
override fun renderDefaultBuilder(writer: RustWriter, member: MemberShape) { | ||
val node = member.expectTrait<DefaultTrait>().toNode()!! | ||
when (val target = model.expectShape(member.target)) { | ||
is EnumShape, is IntEnumShape -> { | ||
val value = when (target) { | ||
is IntEnumShape -> node.expectNumberNode().value | ||
else -> node.expectStringNode().value | ||
} | ||
val enumValues = when (target) { | ||
is IntEnumShape -> target.enumValues | ||
is EnumShape -> target.enumValues | ||
else -> mapOf<String, String>() | ||
} | ||
val variant = enumValues | ||
.entries | ||
.filter { entry -> entry.value == value } | ||
.map { entry -> symbolProvider.toEnumVariantName(EnumDefinition.builder().name(entry.key).value(entry.value.toString()).build())!! } | ||
.first() | ||
val symbol = symbolProvider.toSymbol(target) | ||
writer.rust(".unwrap_or($symbol::${variant.name})") | ||
} | ||
is IntegerShape, is FloatShape -> writer.rust(".unwrap_or(${node.expectNumberNode().value})") | ||
is BooleanShape -> writer.rust(".unwrap_or(${node.expectBooleanNode().value})") | ||
is StringShape -> writer.rust(".unwrap_or_else(|| String::from(${node.expectStringNode().value.dq()}))") | ||
is ListShape, is MapShape -> writer.rust(".unwrap_or_default()") | ||
is DocumentShape -> { | ||
when (node) { | ||
is NullNode -> writer.rust(".unwrap_or_default()") | ||
is BooleanNode -> writer.rust(".unwrap_or(${node.value})") | ||
is StringNode -> writer.rust(".unwrap_or_else(|| String::from(${node.value.dq()}))") | ||
is NumberNode -> writer.rust(".unwrap_or(${node.value})") | ||
else -> writer.rust(".unwrap_or_default()") | ||
} | ||
} | ||
else -> writer.rust(".unwrap_or_default()") | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/testutil/TestHelpers.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,31 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.server.testutil | ||
|
||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.StructureGenerator | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock | ||
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerBuilderGenerator | ||
|
||
/** | ||
* In tests, we frequently need to generate a struct, a builder, and an impl block to access said builder. | ||
*/ | ||
fun StructureShape.renderWithModelBuilder( | ||
model: Model, | ||
symbolProvider: RustSymbolProvider, | ||
writer: RustWriter, | ||
) { | ||
StructureGenerator(model, symbolProvider, writer, this).render(CodegenTarget.SERVER) | ||
val modelBuilder = ServerBuilderGenerator(model, symbolProvider, this) | ||
modelBuilder.render(writer) | ||
writer.implBlock(this, symbolProvider) { | ||
modelBuilder.renderConvenienceMethod(this) | ||
} | ||
} |
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
Oops, something went wrong.