-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Source defaults from the model instead of implicitly (#2985)
## Motivation and Context We weren't correctly computing defaults which lead to incorrect behavior when coupled with nullability. ## Description Minimal changeset to source defaults from the model. Other changes: - Unify enum parsing across client and server to always use `from_str` in protocol tests - Extract `PrimitiveInstantiator` from `Instantiator` so it can be used to instantiate defaults ## Testing - regular codegen tests ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
- Loading branch information
Showing
13 changed files
with
283 additions
and
133 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
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
40 changes: 40 additions & 0 deletions
40
...otlin/software/amazon/smithy/rust/codegen/core/smithy/generators/DefaultValueGenerator.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,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) }) | ||
} | ||
} | ||
} | ||
} |
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.