-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite the kotlin example with sdk-api-kotlin-gen
- Loading branch information
1 parent
eb2bd0e
commit bace074
Showing
5 changed files
with
66 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
examples/src/main/kotlin/my/restate/sdk/examples/Counter.kt
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
examples/src/main/kotlin/my/restate/sdk/examples/CounterKt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |