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

[POC] Basic server type serde for RestJson1 (#737)



* Initial implementation of RestJson and Http ser/de

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

* Visitor render operation shapes from server generators

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

* Use a symlink for the ebs test model

* Revert "Use a symlink for the ebs test model"

This reverts commit 0c2adcbf12f452959252134b7fbb9c70dab154c7.

* Now the model is emitted in different files

* A little more general model generation

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

* Use a very simple model to try the service generators

* Move serializer in its own folder

* Initial implementation of RestJson and Http ser/de

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

* Visitor render operation shapes from server generators

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

* Use a symlink for the ebs test model

* Revert "Use a symlink for the ebs test model"

This reverts commit 0c2adcbf12f452959252134b7fbb9c70dab154c7.

* Now the model is emitted in different files

* A little more general model generation

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

* Use a very simple model to try the service generators

* Move serializer in its own folder

* Refactor RestJson to accomodate upstream changes

* Refactor a little RestJson1 and add simple RFC doc

* Remove inheritance from Json ser/de and refactor codegen visitor to
directly use the rendering functions

* Make operation.rs module public

* Use the protocol ID instead of its string representation

Signed-off-by: default avatarBigo <1781140+crisidev@users.noreply.github.com>
parent f9771d51
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
smithy-build.json
+8 −0
Original line number Diff line number Diff line
# Codegen Integration Test
This module defines an integration test of the code generation machinery. `.build.gradle.kts` will generate a `smithy-build.json` file as part of the build. The Smithy build plugin then invokes our codegen machinery and generates Rust crates.

The `test` task will run `cargo check` and `cargo clippy` to validate that the generated Rust compiles and is idiomatic.
## Usage
```
../gradlew test
```
+117 −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.
 */

extra["displayName"] = "Smithy :: Rust :: Codegen :: Server :: Test"

extra["moduleName"] = "software.amazon.smithy.rust.kotlin.codegen.server.test"

tasks["jar"].enabled = false

plugins { id("software.amazon.smithy").version("0.5.3") }

val smithyVersion: String by project

dependencies {
    implementation(project(":codegen-server"))
    implementation("software.amazon.smithy:smithy-aws-protocol-tests:$smithyVersion")
    implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
}

data class CodegenTest(val service: String, val module: String, val extraConfig: String? = null)

val CodegenTests = listOf(CodegenTest("com.amazonaws.simple#SimpleService", "simple"))

fun generateSmithyBuild(tests: List<CodegenTest>): String {
    val projections =
            tests.joinToString(",\n") {
                """
            "${it.module}": {
                "plugins": {
                    "rust-server-codegen": {
                      "codegen": {
                        "includeFluentClient": false
                      },
                      "runtimeConfig": {
                        "relativePath": "${rootProject.projectDir.absolutePath}/rust-runtime"
                      },
                      "service": "${it.service}",
                      "module": "${it.module}",
                      "moduleVersion": "0.0.1",
                      "moduleAuthors": ["protocoltest@example.com"]
                      ${it.extraConfig ?: ""}
                 }
               }
            }
        """.trimIndent()
            }
    return """
    {
        "version": "1.0",
        "projections": { $projections }
    }
    """
}

task("generateSmithyBuild") {
    description = "generate smithy-build.json"
    doFirst { projectDir.resolve("smithy-build.json").writeText(generateSmithyBuild(CodegenTests)) }
}

fun generateCargoWorkspace(tests: List<CodegenTest>): String {
    return """
    [workspace]
    members = [
        ${tests.joinToString(",") { "\"${it.module}/rust-server-codegen\"" }}
    ]
    """.trimIndent()
}

task("generateCargoWorkspace") {
    description = "generate Cargo.toml workspace file"
    doFirst {
        buildDir.resolve("smithyprojections/codegen-server-test/Cargo.toml")
                .writeText(generateCargoWorkspace(CodegenTests))
    }
}

tasks["smithyBuildJar"].dependsOn("generateSmithyBuild")

tasks["assemble"].finalizedBy("generateCargoWorkspace")

tasks.register<Exec>("cargoCheck") {
    workingDir("build/smithyprojections/codegen-server-test/")
    // disallow warnings
    environment("RUSTFLAGS", "-D warnings")
    commandLine("cargo", "check")
    dependsOn("assemble")
}

tasks.register<Exec>("cargoTest") {
    workingDir("build/smithyprojections/codegen-server-test/")
    // disallow warnings
    environment("RUSTFLAGS", "-D warnings")
    commandLine("cargo", "test")
    dependsOn("assemble")
}

tasks.register<Exec>("cargoDocs") {
    workingDir("build/smithyprojections/codegen-server-test/")
    // disallow warnings
    environment("RUSTFLAGS", "-D warnings")
    commandLine("cargo", "doc", "--no-deps")
    dependsOn("assemble")
}

tasks.register<Exec>("cargoClippy") {
    workingDir("build/smithyprojections/codegen-server-test/")
    // disallow warnings
    commandLine("cargo", "clippy")
    dependsOn("assemble")
}

tasks["test"].finalizedBy("cargoCheck", "cargoClippy", "cargoTest", "cargoDocs")

tasks["clean"].doFirst { delete("smithy-build.json") }
+78 −0
Original line number Diff line number Diff line
$version: "1.0"

namespace com.amazonaws.simple

use aws.protocols#restJson1

@restJson1
@title("SimpleService")
@documentation("A simple service example, with a Service resource that can be registered and a readonly healthcheck")
service SimpleService {
    version: "2022-01-01",
    resources: [
        Service,
    ],
    operations: [
        Healthcheck,
    ],
}

@documentation("Id of the service that will be registered")
string ServiceId

@error("client")
@documentation(
    """
    Returned when a new resource cannot be created because one already exists.
    """
)
structure ResourceAlreadyExists {
    @required
    message: String
}

@documentation("A resource that can register services")
resource Service {
    identifiers: { id: ServiceId },
    put: RegisterService,
}

@idempotent
@http(method: "PUT", uri: "/service/{id}")
@documentation("Service register operation")
operation RegisterService {
    input: RegisterServiceInput,
    output: RegisterServiceOutput,
    errors: [ResourceAlreadyExists]
}

@documentation("Service register input structure")
structure RegisterServiceInput {
    @required
    @httpLabel
    id: ServiceId,
}

@documentation("Service register output structure")
structure RegisterServiceOutput {
    @required
    id: ServiceId
}

@readonly
@http(uri: "/healthcheck", method: "GET")
@documentation("Read-only healthcheck operation")
operation Healthcheck {
    input: HealthcheckInput,
    output: HealthcheckOutput
}

@documentation("Service healthcheck output structure")
structure HealthcheckInput {

}

@documentation("Service healthcheck input structure")
structure HealthcheckOutput {

}
+14 −0
Original line number Diff line number Diff line
# Smithy Rust Server Generator

Server-side Smithy code generator

** This is a work in progress and generates serialization/de-serialization code that is probably unusable for the time being. **

[Design documentation (WIP)](https://awslabs.github.io/smithy-rs/)

## Project Layout

* `codegen-server`: Server-side code generation
* `codegen-server-test`: Server-side Smithy test and validation generation
  Common commands:
  * `./gradlew :codegen-server-test:test` Generate code and run tests against it
Loading