Skip to content

Commit 3952fe5

Browse files
Now sdk-api-kotlin works again with the new interface system. Fix #237 (#238)
1 parent d2ec402 commit 3952fe5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+907
-1090
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import net.ltgt.gradle.errorprone.errorprone
22

33
plugins {
44
java
5-
kotlin("jvm") version "1.9.20" apply false
6-
kotlin("plugin.serialization") version "1.9.20" apply false
5+
kotlin("jvm") version "1.9.22" apply false
6+
kotlin("plugin.serialization") version "1.9.22" apply false
77

88
id("net.ltgt.errorprone") version "3.0.1"
99
id("com.github.jk1.dependency-license-report") version "2.0"

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ For a sample project configuration and more elaborated examples, check out the [
77
Available examples:
88

99
* [`Counter`](src/main/java/my/restate/sdk/examples/Counter.java): Shows a simple virtual object using state primitives.
10-
* [`CounterKt`](src/main/kotlin/dev/restate/sdk/examples/CounterKt.kt): Same as `Counter` but using Kotlin.
10+
* [`Counter`](src/main/kotlin/my/restate/sdk/examples/Counter.kt): Same as `Counter` but using Kotlin.
1111
* [`LoanWorkflow`](src/main/java/my/restate/sdk/examples/LoanWorkflow.java): Shows a simple workflow example using the Workflow API.
1212

1313
## Package the examples for Lambda
@@ -35,7 +35,7 @@ You can run the Java Counter example via:
3535
You can modify the class to run setting `-PmainClass=<FQCN>`, for example, in order to run the Kotlin implementation:
3636

3737
```shell
38-
./gradlew :examples:run -PmainClass=dev.restate.sdk.examples.CounterKt
38+
./gradlew :examples:run -PmainClass=my.restate.sdk.examples.CounterKt
3939
```
4040

4141
## Invoking the counter bindableComponent

examples/build.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
java
3-
// kotlin("jvm")
3+
kotlin("jvm")
4+
kotlin("plugin.serialization")
45
application
56
id("com.github.johnrengelman.shadow").version("7.1.2")
67
}
@@ -11,7 +12,7 @@ dependencies {
1112
implementation(project(":sdk-api"))
1213
implementation(project(":sdk-lambda"))
1314
implementation(project(":sdk-http-vertx"))
14-
// implementation(project(":sdk-api-kotlin"))
15+
implementation(project(":sdk-api-kotlin"))
1516
implementation(project(":sdk-serde-jackson"))
1617
implementation(project(":sdk-workflow-api"))
1718

@@ -26,6 +27,8 @@ dependencies {
2627
implementation(vertxLibs.vertx.kotlin.coroutines)
2728

2829
implementation(kotlinLibs.kotlinx.coroutines)
30+
implementation(kotlinLibs.kotlinx.serialization.core)
31+
implementation(kotlinLibs.kotlinx.serialization.json)
2932

3033
implementation(coreLibs.log4j.core)
3134
}

examples/src/main/java/my/restate/sdk/examples/LambdaHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public void register(RestateLambdaEndpointBuilder builder) {
2323
.split(Pattern.quote(","))) {
2424
if (Counter.class.getCanonicalName().equals(serviceClass)) {
2525
builder.with(new Counter());
26-
// } else if (CounterKt.class.getCanonicalName().equals(serviceClass)) {
27-
// builder.withService(new CounterKt());
26+
} else if (CounterKt.class.getCanonicalName().equals(serviceClass)) {
27+
builder.with(CounterKt.getCounter());
2828
} else {
2929
throw new IllegalArgumentException(
3030
"Bad \"LAMBDA_FACTORY_SERVICE_CLASS\" env: " + serviceClass);

examples/src/main/kotlin/dev/restate/sdk/examples/CounterKt.kt

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package my.restate.sdk.examples
10+
11+
import dev.restate.sdk.common.StateKey
12+
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
13+
import dev.restate.sdk.kotlin.Component
14+
import dev.restate.sdk.kotlin.KtSerdes
15+
import kotlinx.serialization.Serializable
16+
17+
@Serializable data class CounterUpdate(var oldValue: Long, val newValue: Long)
18+
19+
private val totalKey = StateKey.of<Long>("total", KtSerdes.json())
20+
21+
val counter =
22+
Component.virtualObject("Counter") {
23+
handler("reset") { ctx, _: Unit -> ctx.clear(totalKey) }
24+
handler("add") { ctx, value: Long ->
25+
val currentValue = ctx.get(totalKey) ?: 0L
26+
val newValue = currentValue + value
27+
ctx.set(totalKey, newValue)
28+
}
29+
handler("get") { ctx, _: Unit -> ctx.get(totalKey) ?: 0L }
30+
handler("getAndAdd") { ctx, value: Long ->
31+
val currentValue = ctx.get(totalKey) ?: 0L
32+
val newValue = currentValue + value
33+
ctx.set(totalKey, newValue)
34+
CounterUpdate(currentValue, newValue)
35+
}
36+
}
37+
38+
fun main() {
39+
RestateHttpEndpointBuilder.builder().with(counter).buildAndListen()
40+
}

sdk-api-kotlin/build.gradle.kts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import com.google.protobuf.gradle.id
2-
31
plugins {
42
java
53
kotlin("jvm")
@@ -16,50 +14,14 @@ dependencies {
1614
implementation(kotlinLibs.kotlinx.serialization.core)
1715
implementation(kotlinLibs.kotlinx.serialization.json)
1816

17+
implementation(coreLibs.log4j.api)
18+
1919
testImplementation(project(":sdk-core"))
2020
testImplementation(testingLibs.junit.jupiter)
2121
testImplementation(testingLibs.assertj)
22-
testImplementation(coreLibs.protobuf.java)
23-
testImplementation(coreLibs.protobuf.kotlin)
24-
testImplementation(coreLibs.grpc.stub)
25-
testImplementation(coreLibs.grpc.protobuf)
26-
testImplementation(coreLibs.grpc.kotlin.stub)
2722
testImplementation(coreLibs.log4j.core)
2823

2924
testImplementation(project(":sdk-core", "testArchive"))
30-
testProtobuf(project(":sdk-core", "testArchive"))
31-
}
32-
33-
val pluginJar =
34-
file(
35-
"${project.rootProject.rootDir}/protoc-gen-restate/build/libs/protoc-gen-restate-${project.version}-all.jar")
36-
37-
protobuf {
38-
plugins {
39-
id("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:${coreLibs.versions.grpc.get()}" }
40-
id("grpckt") {
41-
artifact = "io.grpc:protoc-gen-grpc-kotlin:${coreLibs.versions.grpckt.get()}:jdk8@jar"
42-
}
43-
id("restate") {
44-
// NOTE: This is not needed in a regular project configuration, you should rather use:
45-
// artifact = "dev.restate.sdk:protoc-gen-restate-java-blocking:1.0-SNAPSHOT:all@jar"
46-
path = pluginJar.path
47-
}
48-
}
49-
50-
generateProtoTasks {
51-
ofSourceSet("test").forEach {
52-
// Make sure we depend on shadowJar from protoc-gen-restate
53-
it.dependsOn(":protoc-gen-restate:shadowJar")
54-
55-
it.plugins {
56-
id("grpc")
57-
id("grpckt")
58-
id("restate") { option("kotlin") }
59-
}
60-
it.builtins { id("kotlin") }
61-
}
62-
}
6325
}
6426

6527
// Generate test jar

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/Awaitables.kt

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,25 @@ internal abstract class BaseSingleMappedAwaitableImpl<T : Any, U : Any>(
7676
}
7777
}
7878

79-
// internal class SingleMappedAwaitableImpl<T: Any, U: Any>(inner: BaseAwaitableImpl<T>, private val
80-
// mapper: suspend (res: Result<T>) -> Result<U>) : BaseSingleMappedAwaitableImpl<T, U>(inner) {
81-
// override suspend fun map(res: Result<T>): Result<U> {
82-
// return mapper(res)
83-
// }
84-
//
85-
// }
79+
internal open class SingleSerdeAwaitableImpl<T : Any>
80+
internal constructor(
81+
syscalls: Syscalls,
82+
deferred: Deferred<ByteString>,
83+
private val serde: Serde<T>,
84+
) :
85+
BaseSingleMappedAwaitableImpl<ByteString, T>(
86+
SingleAwaitableImpl(syscalls, deferred),
87+
) {
88+
@Suppress("UNCHECKED_CAST")
89+
override suspend fun map(res: Result<ByteString>): Result<T> {
90+
return if (res.isSuccess) {
91+
// This propagates exceptions as non-terminal
92+
Result.success(serde.deserializeWrappingException(syscalls, res.value!!))
93+
} else {
94+
res as Result<T>
95+
}
96+
}
97+
}
8698

8799
internal class UnitAwakeableImpl(syscalls: Syscalls, deferred: Deferred<Void>) :
88100
BaseSingleMappedAwaitableImpl<Void, Unit>(SingleAwaitableImpl(syscalls, deferred)) {
@@ -140,23 +152,9 @@ internal class AwakeableImpl<T : Any>
140152
internal constructor(
141153
syscalls: Syscalls,
142154
deferred: Deferred<ByteString>,
143-
private val serde: Serde<T>,
155+
serde: Serde<T>,
144156
override val id: String
145-
) :
146-
BaseSingleMappedAwaitableImpl<ByteString, T>(
147-
SingleAwaitableImpl(syscalls, deferred),
148-
),
149-
Awakeable<T> {
150-
151-
@Suppress("UNCHECKED_CAST")
152-
override suspend fun map(res: Result<ByteString>): Result<T> {
153-
return if (res.isSuccess) {
154-
Result.success(serde.deserializeWrappingException(syscalls, res.value!!))
155-
} else {
156-
res as Result<T>
157-
}
158-
}
159-
}
157+
) : SingleSerdeAwaitableImpl<T>(syscalls, deferred, serde), Awakeable<T> {}
160158

161159
internal class AwakeableHandleImpl(val syscalls: Syscalls, val id: String) : AwakeableHandle {
162160
override suspend fun <T : Any> resolve(serde: Serde<T>, payload: T) {

0 commit comments

Comments
 (0)