Unverified Commit c4ee9f07 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Remove optionality from documents that are in unions (#520)

* Remove optionality from documents that are in unions

* Rely on NullableIndex to determine union document optionality
parent 97134ee5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ class SymbolVisitor(
        // an Input shape, then the field is _not optional_.
        val httpLabeledInput =
            container.hasTrait<SyntheticInputTrait>() && member.hasTrait<HttpLabelTrait>()
        return if (nullableIndex.isNullable(member) && !httpLabeledInput || model.expectShape(member.target).isDocumentShape) {
        return if (nullableIndex.isNullable(member) && !httpLabeledInput) {
            symbol.makeOptional()
        } else symbol
    }
+21 −0
Original line number Diff line number Diff line
@@ -58,8 +58,13 @@ class StructureGeneratorTest {
            // test that sensitive can be applied directly to a member or to the shape
            secretKey: SecretKey
        }

        structure StructWithDoc {
            doc: Document
        }
        """.asSmithyModel()
        val struct = model.lookup<StructureShape>("com.test#MyStruct")
        val structWithDoc = model.lookup<StructureShape>("com.test#StructWithDoc")
        val inner = model.lookup<StructureShape>("com.test#Inner")
        val credentials = model.lookup<StructureShape>("com.test#Credentials")
        val error = model.lookup<StructureShape>("com.test#MyError")
@@ -178,4 +183,20 @@ class StructureGeneratorTest {

        writer.compileAndTest()
    }

    @Test
    fun `documents are optional in structs`() {
        val provider = testSymbolProvider(model)
        val writer = RustWriter.forModule("lib")
        StructureGenerator(model, provider, writer, structWithDoc).render()

        writer.compileAndTest(
            """
            let _struct = StructWithDoc {
                // This will only compile if the document is optional
                doc: None
            };
            """
        )
    }
}
+43 −29
Original line number Diff line number Diff line
@@ -18,18 +18,16 @@ import software.amazon.smithy.rust.codegen.util.lookup
class UnionGeneratorTest {
    @Test
    fun `generate basic unions`() {
        val model = """
        namespace test
        val writer = generateUnion(
            """
            union MyUnion {
                stringConfig: String,
                @documentation("This *is* documentation about the member")
                intConfig: PrimitiveInteger
            }
        """.asSmithyModel()
        val provider: SymbolProvider = testSymbolProvider(model)
        val writer = RustWriter.forModule("model")
        val generator = UnionGenerator(model, provider, writer, model.lookup("test#MyUnion"))
        generator.render()
            """
        )

        writer.compileAndTest(
            """
            let var_a = MyUnion::StringConfig("abc".to_string());
@@ -43,17 +41,14 @@ class UnionGeneratorTest {

    @Test
    fun `generate conversion helper methods`() {
        val model = """
        namespace test
        val writer = generateUnion(
            """
            union MyUnion {
                stringValue: String,
                intValue: PrimitiveInteger
            }
        """.asSmithyModel()
        val provider: SymbolProvider = testSymbolProvider(model)
        val writer = RustWriter.forModule("model")
        val generator = UnionGenerator(model, provider, writer, model.lookup("test#MyUnion"))
        generator.render()
            """
        )

        writer.compileAndTest(
            """
@@ -70,4 +65,23 @@ class UnionGeneratorTest {
            """
        )
    }

    @Test
    fun `documents are not optional in unions`() {
        val writer = generateUnion("union MyUnion { doc: Document, other: String }")
        writer.compileAndTest(
            """
            // If the document isn't optional, this will compile
            MyUnion::Doc(smithy_types::Document::Null);
            """
        )
    }

    private fun generateUnion(modelSmithy: String, unionName: String = "MyUnion"): RustWriter {
        val model = "namespace test\n$modelSmithy".asSmithyModel()
        val provider: SymbolProvider = testSymbolProvider(model)
        val writer = RustWriter.forModule("model")
        UnionGenerator(model, provider, writer, model.lookup("test#$unionName")).render()
        return writer
    }
}