Skip to content

Commit

Permalink
Rewrite the kotlin example with sdk-api-kotlin-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Mar 14, 2024
1 parent eb2bd0e commit bace074
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"))
Expand All @@ -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)
Expand All @@ -38,3 +36,5 @@ application {
project.findProperty("mainClass")?.toString() ?: "my.restate.sdk.examples.Counter"
mainClass.set(mainClassValue)
}

tasks.withType<ShadowJar> { transform(ServiceFileTransformer::class.java) }
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 0 additions & 40 deletions examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt

This file was deleted.

56 changes: 56 additions & 0 deletions examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt
Original file line number Diff line number Diff line change
@@ -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<Long>("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()
}

0 comments on commit bace074

Please sign in to comment.