Skip to content

Commit

Permalink
New test added that has commented out recursive structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahad Zubair committed Oct 18, 2024
1 parent 651538a commit 6deae2f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope
import software.amazon.smithy.rust.codegen.core.util.dq

object TestEnumType : EnumType() {
Expand Down Expand Up @@ -49,4 +50,35 @@ object TestEnumType : EnumType() {
""",
)
}

override fun implFromForStrForUnnamedEnum(context: EnumGeneratorContext): Writable =
writable {
rustTemplate(
"""
impl<T> #{From}<T> for ${context.enumName} where T: #{AsRef}<str> {
fn from(s: T) -> Self {
${context.enumName}(s.as_ref().to_owned())
}
}
""",
*preludeScope,
)
}

override fun implFromStrForUnnamedEnum(context: EnumGeneratorContext): Writable =
writable {
// Add an infallible FromStr implementation for uniformity
rustTemplate(
"""
impl ::std::str::FromStr for ${context.enumName} {
type Err = ::std::convert::Infallible;
fn from_str(s: &str) -> #{Result}<Self, <Self as ::std::str::FromStr>::Err> {
#{Ok}(${context.enumName}::from(s))
}
}
""",
*preludeScope,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import software.amazon.smithy.model.traits.AbstractTrait
import software.amazon.smithy.model.transform.ModelTransformer
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.ServerAdditionalSettings
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.util.lookup
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest
Expand Down Expand Up @@ -305,4 +306,73 @@ class ConstraintsTest {
) { _, _ ->
}
}

@Test
fun `KeyList should work`() {
val model =
"""
namespace test
use aws.protocols#restJson1
use smithy.framework#ValidationException
@restJson1
service SampleService {
operations: [BatchGetItem]
}
@http(uri: "/dailySummary", method: "POST")
operation BatchGetItem {
input : BatchGetItemInput
errors: [ValidationException]
}
structure BatchGetItemInput {
RequestItems: KeyList
}
@length(
min: 1
max: 100
)
list KeyList {
member: Key
}
map Key {
key: AttributeName
value: SomeValue
}
@length(
min: 0
max: 65535
)
string AttributeName
string SomeValue
//union AttributeValue {
// M: MapAttributeValue
// L: ListAttributeValue
//}
//map MapAttributeValue {
// key: AttributeName
// value: AttributeValue
//}
//list ListAttributeValue {
// member: AttributeValue
//}
""".asSmithyModel(smithyVersion = "2")

val dir = File("/Users/fahadzub/kaam/baykar/smithy-gen/keys")
if (dir.exists()) {
dir.deleteRecursively()
}

// Simply compiling the crate is sufficient as a test.
serverIntegrationTest(
model,
IntegrationTestParams(
service = "test#SampleService",
additionalSettings = ServerAdditionalSettings.builder().generateCodegenComments(true).toObjectNode(),
overrideTestDir = dir,
),
) { _, _ ->
}
}
}

0 comments on commit 6deae2f

Please sign in to comment.