Skip to content

Commit

Permalink
Update the example to use the kotlin protoc gen
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Dec 22, 2023
1 parent 8c9cc2a commit f905899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
14 changes: 4 additions & 10 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ val pluginJar =
protobuf {
plugins {
id("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:${coreLibs.versions.grpc.get()}" }
id("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:${coreLibs.versions.grpckt.get()}:jdk8@jar"
}
id("restate") {
// NOTE: This is not needed in a regular project configuration, you should rather use:
// artifact = "dev.restate.sdk:protoc-gen-restate-java-blocking:1.0-SNAPSHOT:all@jar"
Expand All @@ -55,15 +52,12 @@ protobuf {
it.dependsOn(":protoc-gen-restate:shadowJar")
it.plugins {
id("grpc")
id("grpckt")
id("restate")
id("restate") {
option("java")
option("kotlin")
}
}
it.builtins { id("kotlin") }

// Generate descriptor set including the imports in order to make it easier to
// invoke the services via grpcurl (using -protoset).
it.generateDescriptorSet = true
it.descriptorSetOptions.includeImports = true
}
}
}
Expand Down
37 changes: 16 additions & 21 deletions examples/src/main/kotlin/dev/restate/sdk/examples/CounterKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,48 @@
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.examples

import com.google.protobuf.Empty
import dev.restate.sdk.common.CoreSerdes
import dev.restate.sdk.common.StateKey
import dev.restate.sdk.examples.generated.*
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
import dev.restate.sdk.kotlin.RestateKtService
import kotlinx.coroutines.Dispatchers
import dev.restate.sdk.kotlin.RestateContext
import org.apache.logging.log4j.LogManager

class CounterKt : CounterGrpcKt.CounterCoroutineImplBase(Dispatchers.Unconfined), RestateKtService {
class CounterKt : CounterRestateKt.CounterRestateKtImplBase() {

private val LOG = LogManager.getLogger(CounterKt::class.java)

private val TOTAL = StateKey.of("total", CoreSerdes.LONG)

override suspend fun reset(request: CounterRequest): Empty {
restateContext().clear(TOTAL)
return Empty.getDefaultInstance()
override suspend fun reset(context: RestateContext, request: CounterRequest) {
context.clear(TOTAL)
}

override suspend fun add(request: CounterAddRequest): Empty {
updateCounter(request.value)
return Empty.getDefaultInstance()
override suspend fun add(context: RestateContext, request: CounterAddRequest) {
updateCounter(context, request.value)
}

override suspend fun get(request: CounterRequest): GetResponse {
return getResponse { value = getCounter() }
override suspend fun get(context: RestateContext, request: CounterRequest): GetResponse {
return getResponse { value = context.get(TOTAL) ?: 0L }
}

override suspend fun getAndAdd(request: CounterAddRequest): CounterUpdateResult {
override suspend fun getAndAdd(
context: RestateContext,
request: CounterAddRequest
): CounterUpdateResult {
LOG.info("Invoked get and add with " + request.value)
val (old, new) = updateCounter(request.value)
val (old, new) = updateCounter(context, request.value)
return counterUpdateResult {
oldValue = old
newValue = new
}
}

private suspend fun getCounter(): Long {
return restateContext().get(TOTAL) ?: 0L
}

private suspend fun updateCounter(add: Long): Pair<Long, Long> {
val currentValue = getCounter()
private suspend fun updateCounter(context: RestateContext, add: Long): Pair<Long, Long> {
val currentValue = context.get(TOTAL) ?: 0L
val newValue = currentValue + add

restateContext().set(TOTAL, newValue)
context.set(TOTAL, newValue)

return currentValue to newValue
}
Expand Down

0 comments on commit f905899

Please sign in to comment.