-
Notifications
You must be signed in to change notification settings - Fork 188
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
Source defaults from the model instead of implicitly #2985
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package software.amazon.smithy.rust.codegen.core.smithy | ||
|
||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.model.node.Node | ||
import software.amazon.smithy.model.shapes.Shape | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustType | ||
|
@@ -102,6 +103,8 @@ sealed class Default { | |
* This symbol should use the Rust `std::default::Default` when unset | ||
*/ | ||
object RustDefault : Default() | ||
|
||
data class NonZeroDefault(val value: Node) : Default() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs would be nice. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import software.amazon.smithy.codegen.core.SymbolProvider | |||||||
import software.amazon.smithy.model.Model | ||||||||
import software.amazon.smithy.model.knowledge.NullableIndex | ||||||||
import software.amazon.smithy.model.knowledge.NullableIndex.CheckMode | ||||||||
import software.amazon.smithy.model.node.Node | ||||||||
import software.amazon.smithy.model.shapes.BigDecimalShape | ||||||||
import software.amazon.smithy.model.shapes.BigIntegerShape | ||||||||
import software.amazon.smithy.model.shapes.BlobShape | ||||||||
|
@@ -37,6 +38,7 @@ import software.amazon.smithy.model.shapes.StringShape | |||||||
import software.amazon.smithy.model.shapes.StructureShape | ||||||||
import software.amazon.smithy.model.shapes.TimestampShape | ||||||||
import software.amazon.smithy.model.shapes.UnionShape | ||||||||
import software.amazon.smithy.model.traits.DefaultTrait | ||||||||
import software.amazon.smithy.model.traits.EnumTrait | ||||||||
import software.amazon.smithy.model.traits.ErrorTrait | ||||||||
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute | ||||||||
|
@@ -48,6 +50,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.traits.RustBoxTrait | |||||||
import software.amazon.smithy.rust.codegen.core.util.PANIC | ||||||||
import software.amazon.smithy.rust.codegen.core.util.hasTrait | ||||||||
import software.amazon.smithy.rust.codegen.core.util.letIf | ||||||||
import software.amazon.smithy.rust.codegen.core.util.orNull | ||||||||
import software.amazon.smithy.rust.codegen.core.util.toPascalCase | ||||||||
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase | ||||||||
import kotlin.reflect.KClass | ||||||||
|
@@ -79,16 +82,18 @@ data class MaybeRenamed(val name: String, val renamedFrom: String?) | |||||||
/** | ||||||||
* Make the return [value] optional if the [member] symbol is as well optional. | ||||||||
*/ | ||||||||
fun SymbolProvider.wrapOptional(member: MemberShape, value: String): String = value.letIf(toSymbol(member).isOptional()) { | ||||||||
"Some($value)" | ||||||||
} | ||||||||
fun SymbolProvider.wrapOptional(member: MemberShape, value: String): String = | ||||||||
value.letIf(toSymbol(member).isOptional()) { | ||||||||
"Some($value)" | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Make the return [value] optional if the [member] symbol is not optional. | ||||||||
*/ | ||||||||
fun SymbolProvider.toOptional(member: MemberShape, value: String): String = value.letIf(!toSymbol(member).isOptional()) { | ||||||||
"Some($value)" | ||||||||
} | ||||||||
fun SymbolProvider.toOptional(member: MemberShape, value: String): String = | ||||||||
value.letIf(!toSymbol(member).isOptional()) { | ||||||||
"Some($value)" | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Services can rename their contained shapes. See https://awslabs.github.io/smithy/1.0/spec/core/model.html#service | ||||||||
|
@@ -170,7 +175,7 @@ open class SymbolVisitor( | |||||||
} | ||||||||
|
||||||||
private fun simpleShape(shape: SimpleShape): Symbol { | ||||||||
return symbolBuilder(shape, SimpleShapes.getValue(shape::class)).setDefault(Default.RustDefault).build() | ||||||||
return symbolBuilder(shape, SimpleShapes.getValue(shape::class)).build() | ||||||||
} | ||||||||
|
||||||||
override fun booleanShape(shape: BooleanShape): Symbol = simpleShape(shape) | ||||||||
|
@@ -263,13 +268,21 @@ open class SymbolVisitor( | |||||||
|
||||||||
override fun memberShape(shape: MemberShape): Symbol { | ||||||||
val target = model.expectShape(shape.target) | ||||||||
val defaultValue = shape.getMemberTrait(model, DefaultTrait::class.java).orNull()?.let { trait -> | ||||||||
when (val value = trait.toNode()) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have to distinguish the case where the user passes in a value to Generated client code would always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
Node.from(""), Node.from(0), Node.from(false), Node.arrayNode(), Node.objectNode() -> Default.RustDefault | ||||||||
Node.nullNode() -> Default.NoDefault | ||||||||
else -> { Default.NonZeroDefault(value) | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
} ?: Default.NoDefault | ||||||||
// Handle boxing first, so we end up with Option<Box<_>>, not Box<Option<_>>. | ||||||||
return handleOptionality( | ||||||||
handleRustBoxing(toSymbol(target), shape), | ||||||||
shape, | ||||||||
nullableIndex, | ||||||||
config.nullabilityCheckMode, | ||||||||
) | ||||||||
).toBuilder().setDefault(defaultValue).build() | ||||||||
} | ||||||||
|
||||||||
override fun timestampShape(shape: TimestampShape?): Symbol { | ||||||||
|
@@ -297,7 +310,12 @@ fun symbolBuilder(shape: Shape?, rustType: RustType): Symbol.Builder = | |||||||
// If we ever generate a `thisisabug.rs`, there is a bug in our symbol generation | ||||||||
.definitionFile("thisisabug.rs") | ||||||||
|
||||||||
fun handleOptionality(symbol: Symbol, member: MemberShape, nullableIndex: NullableIndex, nullabilityCheckMode: CheckMode): Symbol = | ||||||||
fun handleOptionality( | ||||||||
symbol: Symbol, | ||||||||
member: MemberShape, | ||||||||
nullableIndex: NullableIndex, | ||||||||
nullabilityCheckMode: CheckMode, | ||||||||
): Symbol = | ||||||||
symbol.letIf(nullableIndex.isMemberNullable(member, nullabilityCheckMode)) { symbol.makeOptional() } | ||||||||
|
||||||||
/** | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.core.smithy.generators | ||
|
||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.SimpleShape | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.core.smithy.Default | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.defaultValue | ||
|
||
class DefaultValueGenerator( | ||
runtimeConfig: RuntimeConfig, | ||
private val symbolProvider: RustSymbolProvider, | ||
private val model: Model, | ||
) { | ||
private val instantiator = PrimitiveInstantiator(runtimeConfig, symbolProvider) | ||
|
||
data class DefaultValue(val isRustDefault: Boolean, val expr: Writable) | ||
|
||
/** Returns the default value as set by the defaultValue trait */ | ||
fun defaultValue(member: MemberShape): DefaultValue? { | ||
val target = model.expectShape(member.target) | ||
return when (val default = symbolProvider.toSymbol(member).defaultValue()) { | ||
is Default.NoDefault -> null | ||
is Default.RustDefault -> DefaultValue(isRustDefault = true, writable("Default::default")) | ||
is Default.NonZeroDefault -> { | ||
val instantiation = instantiator.instantiate(target as SimpleShape, default.value) | ||
DefaultValue(isRustDefault = false, writable { rust("||#T", instantiation) }) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.