From bace07454a5a0886bf5b1e6ee83089041548d6be Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 14 Mar 2024 10:25:56 +0100 Subject: [PATCH] Rewrite the kotlin example with sdk-api-kotlin-gen --- examples/README.md | 2 +- examples/build.gradle.kts | 16 +++--- .../restate/sdk/examples/LambdaHandler.java | 2 +- .../kotlin/my/restate/sdk/examples/Counter.kt | 40 ------------- .../my/restate/sdk/examples/CounterKt.kt | 56 +++++++++++++++++++ 5 files changed, 66 insertions(+), 50 deletions(-) delete mode 100644 examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt create mode 100644 examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt diff --git a/examples/README.md b/examples/README.md index 8d1b62c8..6f49b2ed 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,7 +7,7 @@ For a sample project configuration and more elaborated examples, check out the [ Available examples: * [`Counter`](src/main/java/my/restate/sdk/examples/Counter.java): Shows a simple virtual object using state primitives. -* [`Counter`](src/main/kotlin/my/restate/sdk/examples/Counter.kt): Same as `Counter` but using Kotlin. +* [`CounterKt`](src/main/kotlin/my/restate/sdk/examples/CounterKt.kt): Same as `Counter` but using Kotlin. * [`LoanWorkflow`](src/main/java/my/restate/sdk/examples/LoanWorkflow.java): Shows a simple workflow example using the Workflow API. ## Package the examples for Lambda diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 6937d0a5..bb9b40fd 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -1,13 +1,18 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer + plugins { java kotlin("jvm") kotlin("plugin.serialization") + id("com.google.devtools.ksp") version "1.9.22-1.0.17" application - id("com.github.johnrengelman.shadow").version("7.1.2") + id("com.github.johnrengelman.shadow").version("8.1.1") } dependencies { annotationProcessor(project(":sdk-api-gen")) + ksp(project(":sdk-api-kotlin-gen")) implementation(project(":sdk-api")) implementation(project(":sdk-lambda")) @@ -19,13 +24,6 @@ dependencies { implementation(platform(jacksonLibs.jackson.bom)) implementation(jacksonLibs.jackson.jsr310) - implementation(coreLibs.protobuf.java) - implementation(coreLibs.protobuf.kotlin) - - implementation(platform(vertxLibs.vertx.bom)) - implementation(vertxLibs.vertx.core) - implementation(vertxLibs.vertx.kotlin.coroutines) - implementation(kotlinLibs.kotlinx.coroutines) implementation(kotlinLibs.kotlinx.serialization.core) implementation(kotlinLibs.kotlinx.serialization.json) @@ -38,3 +36,5 @@ application { project.findProperty("mainClass")?.toString() ?: "my.restate.sdk.examples.Counter" mainClass.set(mainClassValue) } + +tasks.withType { transform(ServiceFileTransformer::class.java) } diff --git a/examples/src/main/java/my/restate/sdk/examples/LambdaHandler.java b/examples/src/main/java/my/restate/sdk/examples/LambdaHandler.java index ce1be35d..264bddb1 100644 --- a/examples/src/main/java/my/restate/sdk/examples/LambdaHandler.java +++ b/examples/src/main/java/my/restate/sdk/examples/LambdaHandler.java @@ -24,7 +24,7 @@ public void register(RestateLambdaEndpointBuilder builder) { if (Counter.class.getCanonicalName().equals(serviceClass)) { builder.with(new Counter()); } else if (CounterKt.class.getCanonicalName().equals(serviceClass)) { - builder.with(CounterKt.getCounter()); + builder.with(new CounterKt()); } else { throw new IllegalArgumentException( "Bad \"LAMBDA_FACTORY_SERVICE_CLASS\" env: " + serviceClass); diff --git a/examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt b/examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt deleted file mode 100644 index 417b6719..00000000 --- a/examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH -// -// This file is part of the Restate Java SDK, -// which is released under the MIT license. -// -// You can find a copy of the license in file LICENSE in the root -// directory of this repository or package, or at -// https://github.com/restatedev/sdk-java/blob/main/LICENSE -package my.restate.sdk.examples - -import dev.restate.sdk.common.StateKey -import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder -import dev.restate.sdk.kotlin.Component -import dev.restate.sdk.kotlin.KtSerdes -import kotlinx.serialization.Serializable - -@Serializable data class CounterUpdate(var oldValue: Long, val newValue: Long) - -private val totalKey = StateKey.of("total", KtSerdes.json()) - -val counter = - Component.virtualObject("Counter") { - handler("reset") { ctx, _: Unit -> ctx.clear(totalKey) } - handler("add") { ctx, value: Long -> - val currentValue = ctx.get(totalKey) ?: 0L - val newValue = currentValue + value - ctx.set(totalKey, newValue) - } - handler("get") { ctx, _: Unit -> ctx.get(totalKey) ?: 0L } - handler("getAndAdd") { ctx, value: Long -> - val currentValue = ctx.get(totalKey) ?: 0L - val newValue = currentValue + value - ctx.set(totalKey, newValue) - CounterUpdate(currentValue, newValue) - } - } - -fun main() { - RestateHttpEndpointBuilder.builder().with(counter).buildAndListen() -} diff --git a/examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt b/examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt new file mode 100644 index 00000000..c17c3e68 --- /dev/null +++ b/examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt @@ -0,0 +1,56 @@ +// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH +// +// This file is part of the Restate Java SDK, +// which is released under the MIT license. +// +// You can find a copy of the license in file LICENSE in the root +// directory of this repository or package, or at +// https://github.com/restatedev/sdk-java/blob/main/LICENSE +package my.restate.sdk.examples + +import dev.restate.sdk.annotation.Handler +import dev.restate.sdk.annotation.VirtualObject +import dev.restate.sdk.common.StateKey +import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder +import dev.restate.sdk.kotlin.KtSerdes +import dev.restate.sdk.kotlin.ObjectContext +import kotlinx.serialization.Serializable + +@Serializable data class CounterUpdate(var oldValue: Long, val newValue: Long) + +@VirtualObject +class CounterKt { + + companion object { + private val TOTAL = StateKey.of("total", KtSerdes.json()) + } + + @Handler + suspend fun reset(ctx: ObjectContext) { + ctx.clear(TOTAL) + } + + @Handler + suspend fun add(ctx: ObjectContext, value: Long) { + val currentValue = ctx.get(TOTAL) ?: 0L + val newValue = currentValue + value + ctx.set(TOTAL, newValue) + } + + @Handler + suspend fun get(ctx: ObjectContext): Long { + return ctx.get(TOTAL) ?: 0L + } + + @Handler + suspend fun getAndAdd(ctx: ObjectContext, value: Long): CounterUpdate { + val currentValue = ctx.get(TOTAL) ?: 0L + val newValue = currentValue + value + ctx.set(TOTAL, newValue) + return CounterUpdate(currentValue, newValue) + } +} + +fun main() { + RestateHttpEndpointBuilder.builder().with(CounterKt()).buildAndListen() +}