Skip to content

Commit

Permalink
Add formatting for Kotlin code snippets (#438)
Browse files Browse the repository at this point in the history
* Add microservices code snippets for Java and Kotlin

* Fix paths

* Fix package names in java

* Fix kotlin package names

* Add Kotlin and Java to the event processing use case page

* Add Kotlin and Java to event processing pages part 2

* Add async tasks in Java and Kotlin

* Fixes to use case pages

* Incorporate review feedback

* Format java code snippets

* Formatting

* Fix imports
  • Loading branch information
gvdongen authored Jul 30, 2024
1 parent 680fccb commit 480e8fc
Show file tree
Hide file tree
Showing 35 changed files with 742 additions and 824 deletions.
27 changes: 8 additions & 19 deletions code_snippets/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.net.URI

plugins {
application
kotlin("jvm") version "2.0.0"
Expand All @@ -10,9 +8,7 @@ plugins {
id("com.diffplug.spotless") version "6.25.0"
}

repositories {
mavenCentral()
}
repositories { mavenCentral() }

val restateVersion = "1.0.1"

Expand All @@ -32,22 +28,15 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}

kotlin {
jvmToolchain(21)
}

kotlin { jvmToolchain(21) }

// Set main class
application {
mainClass.set("develop.Greeter")
}
application { mainClass.set("develop.Greeter") }

spotless {
java {
googleJavaFormat()
importOrder()
removeUnusedImports()
formatAnnotations()
toggleOffOn("//", "/n")
kotlin {
targetExclude("build/generated/**/*.kt")
ktfmt()
}
}
kotlinGradle { ktfmt() }
}
4 changes: 1 addition & 3 deletions code_snippets/kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.5.0")
}
plugins { id("org.gradle.toolchains.foojay-resolver-convention") version ("0.5.0") }
58 changes: 27 additions & 31 deletions code_snippets/kotlin/src/main/kotlin/develop/Awakeables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,30 @@ package develop
import dev.restate.sdk.kotlin.*

class Awakeables {
suspend fun awakeables(ctx: ObjectContext) {
// <start_here>
// <mark_1>
val awakeable = ctx.awakeable<String>()
val awakeableId: String = awakeable.id
// </mark_1>

// <mark_2>
ctx.runBlock{ triggerTaskAndDeliverId(awakeableId) }
// </mark_2>

// <mark_3>
val payload: String = awakeable.await()
// </mark_3>
// <end_here>


// <start_resolve>
ctx.awakeableHandle(awakeableId)
.resolve("hello")
// <end_resolve>

// <start_reject>
ctx.awakeableHandle(awakeableId)
.reject("my error reason")
// <end_reject>
}

private fun triggerTaskAndDeliverId(awakeableId: String): Unit {
}
}
suspend fun awakeables(ctx: ObjectContext) {
// <start_here>
// <mark_1>
val awakeable = ctx.awakeable<String>()
val awakeableId: String = awakeable.id
// </mark_1>

// <mark_2>
ctx.runBlock { triggerTaskAndDeliverId(awakeableId) }
// </mark_2>

// <mark_3>
val payload: String = awakeable.await()
// </mark_3>
// <end_here>

// <start_resolve>
ctx.awakeableHandle(awakeableId).resolve("hello")
// <end_resolve>

// <start_reject>
ctx.awakeableHandle(awakeableId).reject("my error reason")
// <end_reject>
}

private fun triggerTaskAndDeliverId(awakeableId: String) {}
}
13 changes: 6 additions & 7 deletions code_snippets/kotlin/src/main/kotlin/develop/DurableTimers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package develop
import dev.restate.sdk.kotlin.Context
import kotlin.time.Duration.Companion.seconds


class DurableTimers {
suspend fun timers(ctx: Context) {
// <start_sleep>
ctx.sleep(10.seconds)
// <end_sleep>
}
}
suspend fun timers(ctx: Context) {
// <start_sleep>
ctx.sleep(10.seconds)
// <end_sleep>
}
}
12 changes: 5 additions & 7 deletions code_snippets/kotlin/src/main/kotlin/develop/ErrorHandling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import dev.restate.sdk.common.TerminalException
import dev.restate.sdk.kotlin.Context

class ErrorHandling {
fun errorHandling(ctx: Context) {

// <start_here>
throw TerminalException(500, "Something went wrong")
// <end_here>

}
fun errorHandling(ctx: Context) {
// <start_here>
throw TerminalException(500, "Something went wrong")
// <end_here>
}
}
37 changes: 18 additions & 19 deletions code_snippets/kotlin/src/main/kotlin/develop/Greeter.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package develop

import dev.restate.sdk.annotation.*
import dev.restate.sdk.annotation.Handler
import dev.restate.sdk.annotation.VirtualObject
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
import dev.restate.sdk.kotlin.*
import dev.restate.sdk.kotlin.KtStateKey
import dev.restate.sdk.kotlin.ObjectContext

// withClass tooltip java-overview-virtual-object
@VirtualObject
class Greeter {

// withClass(1:3) tooltip java-overview-state-key
companion object {
private val COUNT = KtStateKey.json<Int>("count")
}
// withClass(1:3) tooltip java-overview-state-key
companion object {
private val COUNT = KtStateKey.json<Int>("count")
}

// withClass tooltip java-overview-virtual-object-handler
@Handler
suspend fun greet(ctx: ObjectContext, greeting: String): String {
// Get the count and increment it
val count = ctx.get(COUNT) ?: 1
ctx.set(COUNT, count + 1)
// withClass tooltip java-overview-virtual-object-handler
@Handler
suspend fun greet(ctx: ObjectContext, greeting: String): String {
// Get the count and increment it
val count = ctx.get(COUNT) ?: 1
ctx.set(COUNT, count + 1)

// Send the response back
return "$greeting ${ctx.key()} for the $count time!"
}
// Send the response back
return "$greeting ${ctx.key()} for the $count time!"
}
}

fun main() {
RestateHttpEndpointBuilder
.builder()
.bind(Greeter())
.buildAndListen()
RestateHttpEndpointBuilder.builder().bind(Greeter()).buildAndListen()
}
126 changes: 62 additions & 64 deletions code_snippets/kotlin/src/main/kotlin/develop/JournalingResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,81 @@ package develop

import dev.restate.sdk.common.TerminalException
import dev.restate.sdk.kotlin.*
import java.util.*
import java.util.UUID

internal class SideEffects {
suspend fun sideEffect(ctx: Context) {
// <start_side_effect>
val output: String = ctx.runBlock { doDbRequest() }
// <end_side_effect>
suspend fun sideEffect(ctx: Context) {
// <start_side_effect>
val output: String = ctx.runBlock { doDbRequest() }
// <end_side_effect>

val paymentClient = PaymentClient()
val txId = ""
val amount = 1

// <start_retry_settings>
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw IllegalStateException("Payment failed")
} else {
result
}
}
// <end_retry_settings>
val paymentClient = PaymentClient()
val txId = ""
val amount = 1

// <start_retry_settings>
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw IllegalStateException("Payment failed")
} else {
result
}
}
// <end_retry_settings>

// <start_terminal>
try {
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw TerminalException(TerminalException.INTERNAL_SERVER_ERROR_CODE, "Payment failed")
} else {
result
}
}
} catch (e: TerminalException) {
// handle terminal error
// <start_terminal>
try {
ctx.runBlock {
val result = paymentClient.call(txId, amount)
if (result) {
// withClass highlight-line
throw TerminalException(TerminalException.INTERNAL_SERVER_ERROR_CODE, "Payment failed")
} else {
result
}
// <end_terminal>
}
} catch (e: TerminalException) {
// handle terminal error
}
// <end_terminal>

val a1 = ctx.awakeable<Boolean>()
val a2 = ctx.awakeable<Boolean>()
val a3 = ctx.awakeable<Boolean>()
val a1 = ctx.awakeable<Boolean>()
val a2 = ctx.awakeable<Boolean>()
val a3 = ctx.awakeable<Boolean>()

// <start_combine_all>
listOf(a1, a2, a3).awaitAll()
// <end_combine_all>
// <start_combine_all>
listOf(a1, a2, a3).awaitAll()
// <end_combine_all>

// <start_combine_any>
val res = Awaitable.any(a1, a2, a3)
.await() as Boolean
// <start_combine_any>
val res = Awaitable.any(a1, a2, a3).await() as Boolean

// Or using the select statement
val resSelect = select {
a1.onAwait { it }
a2.onAwait { it }
a3.onAwait { it }
}
// <end_combine_any>
// Or using the select statement
val resSelect = select {
a1.onAwait { it }
a2.onAwait { it }
a3.onAwait { it }
}
// <end_combine_any>

// <start_uuid>
val uuid: UUID = ctx.random().nextUUID()
// <end_uuid>
// <start_uuid>
val uuid: UUID = ctx.random().nextUUID()
// <end_uuid>

// <start_random_nb>
val value: Int = ctx.random().nextInt()
// <end_random_nb>
}
// <start_random_nb>
val value: Int = ctx.random().nextInt()
// <end_random_nb>
}

private fun doDbRequest(): String {
return ""
}
private fun doDbRequest(): String {
return ""
}
}

internal class PaymentClient {
fun call(txId: String?, amount: Int): Boolean {
return true
}
}
fun call(txId: String?, amount: Int): Boolean {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import dev.restate.sdk.lambda.BaseRestateLambdaHandler
import dev.restate.sdk.lambda.RestateLambdaEndpointBuilder

class MyLambdaHandler : BaseRestateLambdaHandler() {
override fun register(builder: RestateLambdaEndpointBuilder) {
builder
.bind(MyService())
.bind(MyVirtualObject())
}
override fun register(builder: RestateLambdaEndpointBuilder) {
builder.bind(MyService()).bind(MyVirtualObject())
}
}
// <end_here>
13 changes: 5 additions & 8 deletions code_snippets/kotlin/src/main/kotlin/develop/MyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import dev.restate.sdk.kotlin.Context
// <start_here>
@Service
class MyService {
@Handler
suspend fun myHandler(ctx: Context, input: String): String {
return "my-output"
}
@Handler
suspend fun myHandler(ctx: Context, input: String): String {
return "my-output"
}
}

fun main() {
RestateHttpEndpointBuilder
.builder()
.bind(MyService())
.buildAndListen()
RestateHttpEndpointBuilder.builder().bind(MyService()).buildAndListen()
}
// <end_here>
Loading

0 comments on commit 480e8fc

Please sign in to comment.