generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redesign kotlin verb interface
fixes #803 with this change, verbs in kotlin are defined as top-level functions rather than class member functions. this change also introduces the `Module` file annotation in Kotlin, which will be used to declare an FTL module as the package of the annotated file.
- Loading branch information
Showing
23 changed files
with
681 additions
and
659 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
19 changes: 10 additions & 9 deletions
19
examples/kotlin/ftl-module-echo/src/main/kotlin/ftl/echo/Echo.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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
@file:Module | ||
|
||
package ftl.echo | ||
|
||
import ftl.time.TimeModuleClient | ||
import ftl.time.TimeRequest | ||
import ftl.builtin.Empty | ||
import ftl.time.time | ||
import xyz.block.ftl.Context | ||
import xyz.block.ftl.Method | ||
import xyz.block.ftl.Module | ||
import xyz.block.ftl.Verb | ||
|
||
class InvalidInput(val field: String) : Exception() | ||
|
||
data class EchoRequest(val name: String?) | ||
data class EchoResponse(val message: String) | ||
|
||
class Echo { | ||
@Throws(InvalidInput::class) | ||
@Verb | ||
fun echo(context: Context, req: EchoRequest): EchoResponse { | ||
val response = context.call(TimeModuleClient::time, TimeRequest) | ||
return EchoResponse(message = "Hello, ${req.name ?: "anonymous"}! The time is ${response.time}.") | ||
} | ||
@Throws(InvalidInput::class) | ||
@Verb | ||
fun echo(context: Context, req: EchoRequest): EchoResponse { | ||
val response = context.call(::time, Empty()) | ||
return EchoResponse(message = "Hello, ${req.name ?: "anonymous"}! The time is ${response.time}.") | ||
} |
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 |
---|---|---|
@@ -1,36 +1,36 @@ | ||
@file:Module | ||
|
||
package ftl.echo | ||
|
||
import ftl.builtin.Empty | ||
import xyz.block.ftl.Context | ||
import xyz.block.ftl.Verb | ||
import xyz.block.ftl.Database | ||
import xyz.block.ftl.Module | ||
|
||
data class InsertRequest(val data: String) | ||
|
||
val db = Database("testdb") | ||
|
||
class Echo { | ||
|
||
@Verb | ||
fun insert(context: Context, req: InsertRequest): Empty { | ||
persistRequest(req) | ||
return Empty() | ||
} | ||
@Verb | ||
fun insert(context: Context, req: InsertRequest): Empty { | ||
persistRequest(req) | ||
return Empty() | ||
} | ||
|
||
fun persistRequest(req: InsertRequest) { | ||
db.conn { | ||
it.prepareStatement( | ||
""" | ||
fun persistRequest(req: InsertRequest) { | ||
db.conn { | ||
it.prepareStatement( | ||
""" | ||
CREATE TABLE IF NOT EXISTS requests | ||
( | ||
data TEXT, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc'), | ||
updated_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc') | ||
); | ||
""" | ||
).execute() | ||
it.prepareStatement("INSERT INTO requests (data) VALUES ('${req.data}');") | ||
.execute() | ||
} | ||
).execute() | ||
it.prepareStatement("INSERT INTO requests (data) VALUES ('${req.data}');") | ||
.execute() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
@file:Module | ||
|
||
package ftl.echo | ||
|
||
import ftl.echo2.Echo2ModuleClient | ||
import ftl.echo2.echo as echo2 | ||
import xyz.block.ftl.Context | ||
import xyz.block.ftl.Module | ||
import xyz.block.ftl.Verb | ||
|
||
data class EchoRequest(val name: String) | ||
data class EchoResponse(val message: String) | ||
|
||
class Echo { | ||
@Verb | ||
fun echo(context: Context, req: EchoRequest): EchoResponse { | ||
return EchoResponse(message = "Hello, ${req.name}!") | ||
} | ||
@Verb | ||
fun echo(context: Context, req: EchoRequest): EchoResponse { | ||
return EchoResponse(message = "Hello, ${req.name}!") | ||
} | ||
|
||
@Verb | ||
fun call(context: Context, req: EchoRequest): EchoResponse { | ||
val res = context.call(Echo2ModuleClient::echo, ftl.echo2.EchoRequest(name = req.name)) | ||
return EchoResponse(message = res.message) | ||
} | ||
@Verb | ||
fun call(context: Context, req: EchoRequest): EchoResponse { | ||
val res = context.call(::echo2, ftl.echo2.EchoRequest(name = req.name)) | ||
return EchoResponse(message = res.message) | ||
} |
Oops, something went wrong.