Add structured coroutines and explicit error handling
This commit is contained in:
parent
de1262b4cf
commit
fe62e81152
31 changed files with 1701 additions and 325 deletions
|
|
@ -36,18 +36,6 @@ class EpicControllerImpl(val db: *bun.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
worker Counter {
|
||||
var counter = 0
|
||||
|
||||
fun getCount(): Int {
|
||||
return counter
|
||||
}
|
||||
fun increment() {
|
||||
counter += 1
|
||||
println(counter)
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val postgresDsn = "postgresql://postgres:postgres@localhost/postgres?sslmode=disable"
|
||||
val sqlDb = sql.OpenDB(
|
||||
|
|
@ -56,21 +44,10 @@ fun main() {
|
|||
)
|
||||
)
|
||||
val db = bun.NewDB(sqlDb, pgdialect.New())
|
||||
val counter = Counter()
|
||||
go {
|
||||
while(true) {
|
||||
select {
|
||||
every(1000) -> counter.increment()
|
||||
}
|
||||
}
|
||||
}
|
||||
val epicController = EpicControllerImpl(db)
|
||||
fmt.Println("serving http://localhost:8080")
|
||||
http.HandleFunc("/", epicController.hello)
|
||||
http.HandleFunc("/bun", epicController.bunHealth)
|
||||
http.HandleFunc("/bun/users", epicController.bunUsers)
|
||||
http.HandleFunc("/counter") { w, r ->
|
||||
fmt.Fprintln(w, "bun users total:", counter.getCount())
|
||||
}
|
||||
http.ListenAndServe(":8080", http.DefaultServeMux)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ enum ResponseState {
|
|||
|
||||
data class AccountEntity(
|
||||
var id: String,
|
||||
var address: *AddressEntity,
|
||||
var address: AddressEntity,
|
||||
var labels: List<String>,
|
||||
var state: EntityState
|
||||
)
|
||||
|
||||
data class AccountResponse(
|
||||
var address: *AddressResponse,
|
||||
var address: AddressResponse,
|
||||
var id: String,
|
||||
var labels: List<String>,
|
||||
var state: ResponseState
|
||||
|
|
|
|||
18
examples/nullability.gt
Normal file
18
examples/nullability.gt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
data class User(var email: String)
|
||||
|
||||
fun emailOrMissing(user: User?): String {
|
||||
if (user == null) { return "missing" }
|
||||
return user.email
|
||||
}
|
||||
|
||||
fun optionalEmail(user: User?): String? {
|
||||
return user?.email
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val user: User? = User("user@example.com")
|
||||
println(emailOrMissing(user))
|
||||
println(user!!.email)
|
||||
}
|
||||
18
examples/results.gt
Normal file
18
examples/results.gt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import strconv
|
||||
import os
|
||||
|
||||
fun parse(value: String): Result<Int, Error> {
|
||||
return strconv.atoi(value)
|
||||
}
|
||||
|
||||
fun changeDirectory(path: String): Result<Unit, Error> {
|
||||
return os.chdir(path)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
println(parse("42").unwrap())
|
||||
println(parse("invalid").unwrapOr(0))
|
||||
changeDirectory(".").unwrap()
|
||||
}
|
||||
|
|
@ -13,25 +13,6 @@ class PrefixGreeter(val prefix: String): Greeter {
|
|||
}
|
||||
}
|
||||
|
||||
worker Counter {
|
||||
var count = 0
|
||||
|
||||
fun increment() {
|
||||
count += 1
|
||||
}
|
||||
|
||||
fun value(): Int {
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
fun risky(input: String): String {
|
||||
if (input == "boom") {
|
||||
throw "boom requested"
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val greeter: Greeter = PrefixGreeter("hello")
|
||||
println(greeter.greet("gotlin"))
|
||||
|
|
@ -44,35 +25,25 @@ fun main() {
|
|||
val upper = strings.ToUpper("gotlin")
|
||||
fmt.Println("interop:", upper)
|
||||
|
||||
val result = runCatching({ risky("boom") })
|
||||
if (result.isSuccess()) {
|
||||
println("runCatching: success")
|
||||
} else {
|
||||
println("runCatching:")
|
||||
println(result.exceptionOrNull())
|
||||
}
|
||||
|
||||
val maybe: any = null
|
||||
val maybe: String? = null
|
||||
if (maybe == null) {
|
||||
println("null check works")
|
||||
}
|
||||
|
||||
val counter = Counter()
|
||||
counter.increment()
|
||||
counter.increment()
|
||||
fmt.Println("worker value:", counter.value())
|
||||
|
||||
val ready = Channel<String>()
|
||||
go {
|
||||
select {
|
||||
after(120) -> ready.send("timer fired")
|
||||
runBlocking {
|
||||
val ready = Channel<String>()
|
||||
launch {
|
||||
delay(120)
|
||||
ready.send("timer fired")
|
||||
}
|
||||
select {
|
||||
ready -> println("channel says: " + it)
|
||||
}
|
||||
}
|
||||
select {
|
||||
ready -> println("channel says: " + it)
|
||||
}
|
||||
|
||||
select {
|
||||
every(50) -> println("one periodic tick")
|
||||
val answer = async<Int> {
|
||||
delay(50)
|
||||
return 42
|
||||
}
|
||||
fmt.Println("async value:", answer.await())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue