-
Notifications
You must be signed in to change notification settings - Fork 188
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
- Loading branch information
Showing
12 changed files
with
274 additions
and
132 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
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.