-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unknown enum variants removed from server (#1398)
The server must have the most up to date variants and the unknown enum variant should not be used. Clients are generated with it because they might not have the most recent model and the server might return an unknown variant to them. Closes #1187 Signed-off-by: Daniele Ahmed <[email protected]> Co-authored-by: Daniele Ahmed <[email protected]> Co-authored-by: david-perez <[email protected]> Co-authored-by: Matteo Bigoi <[email protected]>
- Loading branch information
1 parent
d6e2944
commit 35989d2
Showing
21 changed files
with
327 additions
and
57 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
94 changes: 94 additions & 0 deletions
94
...otlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerEnumGenerator.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,94 @@ | ||
/* | ||
* 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.shapes.StringShape | ||
import software.amazon.smithy.model.traits.EnumTrait | ||
import software.amazon.smithy.rust.codegen.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.rustlang.rustBlock | ||
import software.amazon.smithy.rust.codegen.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType | ||
import software.amazon.smithy.rust.codegen.smithy.CodegenMode | ||
import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig | ||
import software.amazon.smithy.rust.codegen.smithy.RuntimeType | ||
import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.smithy.generators.EnumGenerator | ||
import software.amazon.smithy.rust.codegen.util.dq | ||
|
||
class ServerEnumGenerator( | ||
model: Model, | ||
symbolProvider: RustSymbolProvider, | ||
private val writer: RustWriter, | ||
shape: StringShape, | ||
enumTrait: EnumTrait, | ||
private val runtimeConfig: RuntimeConfig, | ||
) : EnumGenerator(model, symbolProvider, writer, shape, enumTrait) { | ||
override var mode: CodegenMode = CodegenMode.Server | ||
private val errorStruct = "${enumName}UnknownVariantError" | ||
|
||
override fun renderFromForStr() { | ||
writer.rust( | ||
""" | ||
##[derive(Debug, PartialEq, Eq, Hash)] | ||
pub struct $errorStruct(String); | ||
""" | ||
) | ||
writer.rustBlock("impl #T<&str> for $enumName", RuntimeType.TryFrom) { | ||
write("type Error = $errorStruct;") | ||
writer.rustBlock("fn try_from(s: &str) -> Result<Self, <$enumName as #T<&str>>::Error>", RuntimeType.TryFrom) { | ||
writer.rustBlock("match s") { | ||
sortedMembers.forEach { member -> | ||
write("${member.value.dq()} => Ok($enumName::${member.derivedName()}),") | ||
} | ||
write("_ => Err($errorStruct(s.to_owned()))") | ||
} | ||
} | ||
} | ||
writer.rustTemplate( | ||
""" | ||
impl #{From}<$errorStruct> for #{RequestRejection} { | ||
fn from(e: $errorStruct) -> Self { | ||
Self::EnumVariantNotFound(Box::new(e)) | ||
} | ||
} | ||
impl #{From}<$errorStruct> for #{JsonDeserialize} { | ||
fn from(e: $errorStruct) -> Self { | ||
Self::custom(format!("unknown variant {}", e)) | ||
} | ||
} | ||
impl #{StdError} for $errorStruct { } | ||
impl #{Display} for $errorStruct { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
""", | ||
"Display" to RuntimeType.Display, | ||
"From" to RuntimeType.From, | ||
"StdError" to RuntimeType.StdError, | ||
"RequestRejection" to ServerRuntimeType.RequestRejection(runtimeConfig), | ||
"JsonDeserialize" to RuntimeType.jsonDeserialize(runtimeConfig), | ||
) | ||
} | ||
|
||
override fun renderFromStr() { | ||
writer.rust( | ||
""" | ||
impl std::str::FromStr for $enumName { | ||
type Err = $errorStruct; | ||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
$enumName::try_from(s) | ||
} | ||
} | ||
""" | ||
) | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
...n/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerEnumGeneratorTest.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,84 @@ | ||
/* | ||
* 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 io.kotest.matchers.string.shouldNotContain | ||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.model.shapes.StringShape | ||
import software.amazon.smithy.rust.codegen.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestSymbolProvider | ||
import software.amazon.smithy.rust.codegen.testutil.TestRuntimeConfig | ||
import software.amazon.smithy.rust.codegen.testutil.asSmithyModel | ||
import software.amazon.smithy.rust.codegen.testutil.compileAndTest | ||
import software.amazon.smithy.rust.codegen.util.expectTrait | ||
import software.amazon.smithy.rust.codegen.util.lookup | ||
|
||
class ServerEnumGeneratorTest { | ||
private val model = """ | ||
namespace test | ||
@enum([ | ||
{ | ||
value: "t2.nano", | ||
name: "T2_NANO", | ||
documentation: "T2 instances are Burstable Performance Instances.", | ||
tags: ["ebsOnly"] | ||
}, | ||
{ | ||
value: "t2.micro", | ||
name: "T2_MICRO", | ||
documentation: "T2 instances are Burstable Performance Instances.", | ||
tags: ["ebsOnly"] | ||
}, | ||
]) | ||
string InstanceType | ||
""".asSmithyModel() | ||
|
||
@Test | ||
fun `it generates TryFrom, FromStr and errors for enums`() { | ||
val provider = serverTestSymbolProvider(model) | ||
val writer = RustWriter.forModule("model") | ||
val shape = model.lookup<StringShape>("test#InstanceType") | ||
val generator = ServerEnumGenerator(model, provider, writer, shape, shape.expectTrait(), TestRuntimeConfig) | ||
generator.render() | ||
writer.compileAndTest( | ||
""" | ||
use std::str::FromStr; | ||
assert_eq!(InstanceType::try_from("t2.nano").unwrap(), InstanceType::T2Nano); | ||
assert_eq!(InstanceType::from_str("t2.nano").unwrap(), InstanceType::T2Nano); | ||
assert_eq!(InstanceType::try_from("unknown").unwrap_err(), InstanceTypeUnknownVariantError("unknown".to_string())); | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `it generates enums without the unknown variant`() { | ||
val provider = serverTestSymbolProvider(model) | ||
val writer = RustWriter.forModule("model") | ||
val shape = model.lookup<StringShape>("test#InstanceType") | ||
val generator = ServerEnumGenerator(model, provider, writer, shape, shape.expectTrait(), TestRuntimeConfig) | ||
generator.render() | ||
writer.compileAndTest( | ||
""" | ||
// check no unknown | ||
let instance = InstanceType::T2Micro; | ||
match instance { | ||
InstanceType::T2Micro => (), | ||
InstanceType::T2Nano => (), | ||
} | ||
""" | ||
) | ||
} | ||
|
||
@Test | ||
fun `it generates enums without non_exhaustive`() { | ||
val provider = serverTestSymbolProvider(model) | ||
val writer = RustWriter.forModule("model") | ||
val shape = model.lookup<StringShape>("test#InstanceType") | ||
val generator = ServerEnumGenerator(model, provider, writer, shape, shape.expectTrait(), TestRuntimeConfig) | ||
generator.render() | ||
writer.toString() shouldNotContain "#[non_exhaustive]" | ||
} | ||
} |
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.