Skip to content

Commit 5c86ab1

Browse files
luigi617luigi
andauthored
Service sdk cbor serde (#1314)
* service init, code to separate client and service * server can be ran * application in build.gradle.kts * detele non-finished code * ktlintformat * change style * ktlintformat * add abstraction for service * minor fix * minor fix * cbor serde * fix * formatting * comment * fix * fix * FIX nit * FIX * fix * fix warning * fix * fix * fix --------- Co-authored-by: luigi <[email protected]>
1 parent b2d64af commit 5c86ab1

File tree

12 files changed

+360
-139
lines changed

12 files changed

+360
-139
lines changed

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/aws/protocols/RpcV2Cbor.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,16 @@ class RpcV2Cbor : AwsHttpBindingProtocolGenerator() {
103103
if (!op.hasHttpBody(ctx)) return
104104

105105
// payload member(s)
106-
val requestBindings = resolver.requestBindings(op)
107-
val httpPayload = requestBindings.firstOrNull { it.location == HttpBinding.Location.PAYLOAD }
106+
val bindings = if (ctx.settings.build.generateServiceProject) {
107+
resolver.responseBindings(op)
108+
} else {
109+
resolver.requestBindings(op)
110+
}
111+
val httpPayload = bindings.firstOrNull { it.location == HttpBinding.Location.PAYLOAD }
108112
if (httpPayload != null) {
109113
renderExplicitHttpPayloadSerializer(ctx, httpPayload, writer)
110114
} else {
111-
val documentMembers = requestBindings.filterDocumentBoundMembers()
115+
val documentMembers = bindings.filterDocumentBoundMembers()
112116
// Unbound document members that should be serialized into the document format for the protocol.
113117
// delegate to the generate operation body serializer function
114118
val sdg = structuredDataSerializer(ctx)

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/aws/protocols/core/StaticHttpBindingResolver.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ open class StaticHttpBindingResolver(
5656
/**
5757
* By default returns all inputs as [HttpBinding.Location.DOCUMENT]
5858
*/
59-
override fun requestBindings(operationShape: OperationShape): List<HttpBindingDescriptor> {
60-
if (!operationShape.input.isPresent) return emptyList()
61-
val input = model.expectShape(operationShape.input.get())
62-
return input.members().map { member -> HttpBindingDescriptor(member, HttpBinding.Location.DOCUMENT) }.toList()
59+
override fun requestBindings(shape: Shape): List<HttpBindingDescriptor> {
60+
when (shape) {
61+
is OperationShape -> {
62+
if (!shape.input.isPresent) return emptyList()
63+
val input = model.expectShape(shape.input.get())
64+
return input.members().map { member -> HttpBindingDescriptor(member, HttpBinding.Location.DOCUMENT) }.toList()
65+
}
66+
is StructureShape -> return shape.members().map { member -> member.toHttpBindingDescriptor() }.toList()
67+
else -> error("unimplemented shape type for http response bindings: $shape")
68+
}
6369
}
6470

6571
/**

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,17 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Unit>() {
143143
logger.info("[${service.id}] Generating service client for protocol $protocol")
144144
generateProtocolClient(ctx)
145145

146-
logger.info("[${service.id}] Generating endpoint provider for protocol $protocol")
147-
generateEndpointsSources(ctx)
146+
if (!generateServiceProject) {
147+
logger.info("[${service.id}] Generating endpoint provider for protocol $protocol")
148+
generateEndpointsSources(ctx)
148149

149-
logger.info("[${service.id}] Generating auth scheme provider for protocol $protocol")
150-
generateAuthSchemeProvider(ctx)
150+
logger.info("[${service.id}] Generating auth scheme provider for protocol $protocol")
151+
generateAuthSchemeProvider(ctx)
152+
}
151153
}
154+
152155
if (generateServiceProject) {
153-
val serviceStubGenerator = ServiceStubGenerator(settings, writers)
156+
val serviceStubGenerator = ServiceStubGenerator(baseGenerationContext, writers)
154157
serviceStubGenerator.render()
155158
}
156159

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ data class KotlinSettings(
107107
* @return Returns the extracted settings
108108
*/
109109
fun from(model: Model, config: ObjectNode): KotlinSettings {
110-
config.warnIfAdditionalProperties(listOf(SERVICE, PACKAGE_SETTINGS, BUILD_SETTINGS, SDK_ID, API_SETTINGS))
110+
config.warnIfAdditionalProperties(listOf(SERVICE, PACKAGE_SETTINGS, BUILD_SETTINGS, SDK_ID, API_SETTINGS, SERVICE_STUB_SETTINGS))
111111

112112
val serviceId = config.getStringMember(SERVICE)
113113
.map(StringNode::expectShapeId)

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinDependency.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ data class KotlinDependency(
142142
// FIXME: version numbers should not be hardcoded, they should be setting dynamically based on the Gradle library versions
143143
val KTOR_SERVER_CORE = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server", "io.ktor", "ktor-server-core", KTOR_VERSION)
144144
val KTOR_SERVER_NETTY = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server.netty", "io.ktor", "ktor-server-netty", KTOR_VERSION)
145+
val KTOR_SERVER_HTTP = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.http", "io.ktor", "ktor-http-jvm", KTOR_VERSION)
145146
val KTOR_LOGGING_BACKEND = KotlinDependency(GradleConfiguration.Implementation, "ch.qos.logback", "ch.qos.logback", "logback-classic", KTOR_LOGGING_BACKEND_VERSION)
146147
val KTOR_SERVER_CONTENT_NEGOTIATION = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server.plugins.contentnegotiation", "io.ktor", "ktor-server-content-negotiation", KTOR_VERSION)
147148
val KTOR_SERVER_CBOR_SERDE = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.serialization.kotlinx.cbor", "io.ktor", "ktor-serialization-kotlinx-cbor", KTOR_VERSION)

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinSymbolProvider.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package software.amazon.smithy.kotlin.codegen.core
66

77
import software.amazon.smithy.codegen.core.*
88
import software.amazon.smithy.kotlin.codegen.KotlinSettings
9+
import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes
910
import software.amazon.smithy.kotlin.codegen.lang.kotlinReservedWords
1011
import software.amazon.smithy.kotlin.codegen.model.*
1112
import software.amazon.smithy.kotlin.codegen.utils.dq
@@ -332,6 +333,10 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
332333
}
333334

334335
override fun serviceShape(shape: ServiceShape): Symbol {
336+
if (settings.build.generateServiceProject) {
337+
// Intentionally not generating a *client symbol* for the service
338+
return KotlinTypes.Nothing
339+
}
335340
val serviceName = clientName(settings.sdkId)
336341
return createSymbolBuilder(shape, "${serviceName}Client")
337342
.namespace(rootNamespace, ".")

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ object RuntimeTypes {
486486
}
487487

488488
object KtorServerCore : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CORE) {
489-
val responseText = symbol("respondText", "response")
490489
val embeddedServer = symbol("embeddedServer", "engine")
491490
val Application = symbol("Application", "application")
492491
val applicationCall = symbol("call", "application")
@@ -496,12 +495,23 @@ object RuntimeTypes {
496495
object KtorServerRouting : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CORE) {
497496
val routing = symbol("routing", "routing")
498497
val get = symbol("get", "routing")
498+
val post = symbol("post", "routing")
499+
500+
val responseText = symbol("respondText", "response")
501+
val requestReceive = symbol("receive", "request")
502+
val requestRespondBytes = symbol("respondBytes", "response")
499503
}
500504

501505
object KtorServerNetty : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_NETTY) {
502506
val Netty = symbol("Netty")
503507
}
504508

509+
object KtorServerHttp : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_HTTP) {
510+
val ContentType = symbol("ContentType")
511+
val HttpStatusCode = symbol("HttpStatusCode")
512+
val Cbor = symbol("Cbor", "ContentType.Application")
513+
}
514+
505515
object KtorServerContentNegotiation : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CONTENT_NEGOTIATION) {
506516
val ContentNegotiation = symbol("ContentNegotiation")
507517
}

0 commit comments

Comments
 (0)