Unverified Commit 14f435e8 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

EC2 & fixes (#495)

* add python script to synchronize models

* Synchronize models

* Add EC2 model & boxing customization
parent 0622f5be
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -7,9 +7,11 @@ package software.amazon.smithy.rustsdk

import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator
import software.amazon.smithy.rustsdk.customize.apigateway.ApiGatewayDecorator
import software.amazon.smithy.rustsdk.customize.ec2.Ec2Decorator
import software.amazon.smithy.rustsdk.customize.s3.S3Decorator

val DECORATORS = listOf(
    // General AWS Decorators
    CredentialsProviderDecorator(),
    RegionDecorator(),
    AwsEndpointDecorator(),
@@ -18,9 +20,12 @@ val DECORATORS = listOf(
    RetryPolicyDecorator(),
    IntegrationTestDecorator(),
    FluentClientDecorator(),
    ApiGatewayDecorator(),
    CrateLicenseDecorator(),
    S3Decorator()

    // Service specific decorators
    ApiGatewayDecorator(),
    S3Decorator(),
    Ec2Decorator()
)

class AwsCodegenDecorator : CombinedCodegenDecorator(DECORATORS) {
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.smithy.rustsdk.customize.ec2

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.AbstractShapeBuilder
import software.amazon.smithy.model.shapes.BigDecimalShape
import software.amazon.smithy.model.shapes.BigIntegerShape
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.ByteShape
import software.amazon.smithy.model.shapes.DoubleShape
import software.amazon.smithy.model.shapes.FloatShape
import software.amazon.smithy.model.shapes.IntegerShape
import software.amazon.smithy.model.shapes.LongShape
import software.amazon.smithy.model.shapes.NumberShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.ShortShape
import software.amazon.smithy.model.traits.BoxTrait
import software.amazon.smithy.model.transform.ModelTransformer
import software.amazon.smithy.utils.ToSmithyBuilder

object BoxPrimitiveShapes {
    fun processModel(model: Model): Model {
        val transformer = ModelTransformer.create()
        return transformer.mapShapes(model, ::boxPrimitives)
    }

    private fun boxPrimitives(shape: Shape): Shape {
        return when (shape) {
            is NumberShape -> {
                when (shape) {
                    is ByteShape -> box(shape)
                    is DoubleShape -> box(shape)
                    is LongShape -> box(shape)
                    is ShortShape -> box(shape)
                    is FloatShape -> box(shape)
                    is BigDecimalShape -> box(shape)
                    is BigIntegerShape -> box(shape)
                    is IntegerShape -> box(shape)
                    else -> TODO("unhandled numeric shape: $shape")
                }
            }
            is BooleanShape -> box(shape)
            else -> shape
        }
    }

    private fun <T> box(shape: T): Shape where T : Shape, T : ToSmithyBuilder<T> {
        return (shape.toBuilder() as AbstractShapeBuilder<*, T>).addTrait(BoxTrait()).build()
    }
}
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.smithy.rustsdk.customize.ec2

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.smithy.letIf

class Ec2Decorator : RustCodegenDecorator {
    override val name: String = "Ec2"
    override val order: Byte = 0
    private val ec2 = ShapeId.from("com.amazonaws.ec2#AmazonEC2")

    private fun applies(serviceShape: ServiceShape) =
        serviceShape.id == ec2

    override fun transformModel(service: ServiceShape, model: Model): Model {
        // EC2 incorrectly models primitive shapes as unboxed when they actually
        // need to be boxed for the API to work properly
        return model.letIf(
            applies(service),
            BoxPrimitiveShapes::processModel
        )
    }
}
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.smithy.rustsdk.customize.ec2

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.BoxTrait
import software.amazon.smithy.rust.codegen.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.util.hasTrait
import software.amazon.smithy.rust.codegen.util.lookup

internal class BoxPrimitiveShapesTest {
    @Test
    fun `primitive shapes are boxed`() {
        val baseModel = """
            namespace test
            structure Primitives {
                int: PrimitiveInteger,
                bool: PrimitiveBoolean,
                long: PrimitiveLong,
                double: PrimitiveDouble,
                boxedAlready: BoxedField,
                other: Other
            }

            @box
            integer BoxedField

            structure Other {}

        """.asSmithyModel()
        val model = BoxPrimitiveShapes.processModel(baseModel)

        val struct = model.lookup<StructureShape>("test#Primitives")
        struct.members().forEach {
            val target = model.expectShape(it.target)
            when (target) {
                is StructureShape -> target.hasTrait<BoxTrait>() shouldBe false
                else -> target.hasTrait<BoxTrait>() shouldBe true
            }
        }
    }
}
+9221 −9107

File changed.

Preview size limit exceeded, changes collapsed.

Loading