-
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.
* Factor out common codegen module in sdk-api-gen-common * Implement sdk-api-kotlin-gen and test it * Rewrite the kotlin example with sdk-api-kotlin-gen
- Loading branch information
1 parent
3952fe5
commit ac82682
Showing
34 changed files
with
1,748 additions
and
746 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() | ||
} |
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,13 @@ | ||
plugins { | ||
`java-library` | ||
`library-publishing-conventions` | ||
} | ||
|
||
description = "Restate SDK API Gen Common" | ||
|
||
dependencies { | ||
compileOnly(coreLibs.jspecify) | ||
|
||
api("com.github.jknack:handlebars:4.3.1") | ||
api(project(":sdk-common")) | ||
} |
140 changes: 140 additions & 0 deletions
140
sdk-api-gen-common/src/main/java/dev/restate/sdk/gen/model/Component.java
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,140 @@ | ||
// 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 dev.restate.sdk.gen.model; | ||
|
||
import dev.restate.sdk.common.ComponentType; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class Component { | ||
|
||
private final CharSequence targetPkg; | ||
private final CharSequence targetFqcn; | ||
private final String componentName; | ||
private final ComponentType componentType; | ||
private final List<Handler> handlers; | ||
|
||
public Component( | ||
CharSequence targetPkg, | ||
CharSequence targetFqcn, | ||
String componentName, | ||
ComponentType componentType, | ||
List<Handler> handlers) { | ||
this.targetPkg = targetPkg; | ||
this.targetFqcn = targetFqcn; | ||
this.componentName = componentName; | ||
|
||
this.componentType = componentType; | ||
this.handlers = handlers; | ||
} | ||
|
||
public CharSequence getTargetPkg() { | ||
return this.targetPkg; | ||
} | ||
|
||
public CharSequence getTargetFqcn() { | ||
return this.targetFqcn; | ||
} | ||
|
||
public String getFullyQualifiedComponentName() { | ||
return this.componentName; | ||
} | ||
|
||
public String getSimpleComponentName() { | ||
return this.componentName.substring(this.componentName.lastIndexOf('.') + 1); | ||
} | ||
|
||
public CharSequence getGeneratedClassFqcnPrefix() { | ||
if (this.targetPkg == null || this.targetPkg.length() == 0) { | ||
return getSimpleComponentName(); | ||
} | ||
return this.targetPkg + "." + getSimpleComponentName(); | ||
} | ||
|
||
public ComponentType getComponentType() { | ||
return componentType; | ||
} | ||
|
||
public List<Handler> getMethods() { | ||
return handlers; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private CharSequence targetPkg; | ||
private CharSequence targetFqcn; | ||
private String componentName; | ||
private ComponentType componentType; | ||
private final List<Handler> handlers = new ArrayList<>(); | ||
|
||
public Builder withTargetPkg(CharSequence targetPkg) { | ||
this.targetPkg = targetPkg; | ||
return this; | ||
} | ||
|
||
public Builder withTargetFqcn(CharSequence targetFqcn) { | ||
this.targetFqcn = targetFqcn; | ||
return this; | ||
} | ||
|
||
public Builder withComponentName(String componentName) { | ||
this.componentName = componentName; | ||
return this; | ||
} | ||
|
||
public Builder withComponentType(ComponentType componentType) { | ||
this.componentType = componentType; | ||
return this; | ||
} | ||
|
||
public Builder withHandlers(Collection<Handler> handlers) { | ||
this.handlers.addAll(handlers); | ||
return this; | ||
} | ||
|
||
public Builder withHandler(Handler handler) { | ||
this.handlers.add(handler); | ||
return this; | ||
} | ||
|
||
public CharSequence getTargetPkg() { | ||
return targetPkg; | ||
} | ||
|
||
public CharSequence getTargetFqcn() { | ||
return targetFqcn; | ||
} | ||
|
||
public String getComponentName() { | ||
return componentName; | ||
} | ||
|
||
public ComponentType getComponentType() { | ||
return componentType; | ||
} | ||
|
||
public List<Handler> getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public Component build() { | ||
return new Component( | ||
Objects.requireNonNull(targetPkg), | ||
Objects.requireNonNull(targetFqcn), | ||
Objects.requireNonNull(componentName), | ||
Objects.requireNonNull(componentType), | ||
handlers); | ||
} | ||
} | ||
} |
Oops, something went wrong.