Unverified Commit 48eda404 authored by Matteo Bigoi's avatar Matteo Bigoi Committed by GitHub
Browse files

Implement Python unions (#2427)



* Add initial implementation of unions with very broken symbol provider

* Add support for creating new unions in Python

* Generate getters and static methods for unions

* Allow to compile misc model

Signed-off-by: default avatarBigo <1781140+crisidev@users.noreply.github.com>

* Doesn't work

* Now it works

* Simplify code a little

* Remove leftover from the many tries I did

* Finally fixed model generation with unions

* Fix wrong import

* Update to reflect changes in decorators

* Remove debugging output

* Simplify symbol provider

* Follow PR suggestions

* Remove union operation from python example

* Return `PyUnionMarker` for wrapped type in `IntoPy` impl

---------

Signed-off-by: default avatarBigo <1781140+crisidev@users.noreply.github.com>
Co-authored-by: default avatarBurak Varlı <burakvar@amazon.co.uk>
parent d8a7d999
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -50,3 +50,9 @@ target/

# IDEs
.idea/
.project
.settings
.classpath

# tools
.tool-versions
+1 −0
Original line number Diff line number Diff line
@@ -466,6 +466,7 @@ class Attribute(val inner: Writable) {
        val AllowClippyUnnecessaryWraps = Attribute(allow("clippy::unnecessary_wraps"))
        val AllowClippyUselessConversion = Attribute(allow("clippy::useless_conversion"))
        val AllowClippyUnnecessaryLazyEvaluations = Attribute(allow("clippy::unnecessary_lazy_evaluations"))
        val AllowClippyTooManyArguments = Attribute(allow("clippy::too_many_arguments"))
        val AllowDeadCode = Attribute(allow("dead_code"))
        val AllowDeprecated = Attribute(allow("deprecated"))
        val AllowIrrefutableLetPatterns = Attribute(allow("irrefutable_let_patterns"))
+2 −2
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ open class StructureGenerator(
        writer.rustBlock("impl $name") {
            // Render field accessor methods
            forEachMember(accessorMembers) { member, memberName, memberSymbol ->
                renderMemberDoc(member, memberSymbol)
                writer.renderMemberDoc(member, memberSymbol)
                writer.deprecatedShape(member)
                val memberType = memberSymbol.rustType()
                val returnType = when {
@@ -140,7 +140,7 @@ open class StructureGenerator(
                    memberType.isDeref() -> memberType.asDeref().asRef()
                    else -> memberType.asRef()
                }
                rustBlock("pub fn $memberName(&self) -> ${returnType.render()}") {
                writer.rustBlock("pub fn $memberName(&self) -> ${returnType.render()}") {
                    when {
                        memberType.isCopy() -> rust("self.$memberName")
                        memberType is RustType.Option && memberType.member.isDeref() -> rust("self.$memberName.as_deref()")
+2 −2
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ fun CodegenTarget.renderUnknownVariant() = when (this) {
 * Finally, if `[renderUnknownVariant]` is true (the default), it will render an `Unknown` variant. This is used by
 * clients to allow response parsing to succeed, even if the server has added a new variant since the client was generated.
 */
class UnionGenerator(
open class UnionGenerator(
    val model: Model,
    private val symbolProvider: SymbolProvider,
    private val writer: RustWriter,
@@ -60,7 +60,7 @@ class UnionGenerator(
    private val sortedMembers: List<MemberShape> = shape.allMembers.values.sortedBy { symbolProvider.toMemberName(it) }
    private val unionSymbol = symbolProvider.toSymbol(shape)

    fun render() {
    open fun render() {
        writer.documentShape(shape, model)
        writer.deprecatedShape(shape)

+12 −0
Original line number Diff line number Diff line
@@ -42,6 +42,18 @@ val allCodegenTests = "../../codegen-core/common-test-models".let { commonModels
    listOf(
        CodegenTest("com.amazonaws.simple#SimpleService", "simple", imports = listOf("$commonModels/simple.smithy")),
        CodegenTest("com.aws.example.python#PokemonService", "pokemon-service-server-sdk"),
        CodegenTest(
            "com.amazonaws.ebs#Ebs", "ebs",
            imports = listOf("$commonModels/ebs.json"),
            extraConfig = """, "codegen": { "ignoreUnsupportedConstraints": true } """,
        ),
        CodegenTest(
            "aws.protocoltests.misc#MiscService",
            "misc",
            imports = listOf("$commonModels/misc.smithy"),
            // TODO(https://github.com/awslabs/smithy-rs/issues/1401) `@uniqueItems` is used.
            extraConfig = """, "codegen": { "ignoreUnsupportedConstraints": true } """,
        ),
    )
}

Loading