Kotlin
Kotlin output is written with data classes and sealed interfaces. There are two runtimes: types, validation, cookies and endpoints come from Java's comatype (packages/java), and the c / s that you write with coroutines (suspend) come from Kotlin's comatype.ktx (packages/kotlin). Both are written with the standard library only, and kotlinx.coroutines is not needed. You need JDK 21 or later and kotlinc.
What is generated#
<out>/ default is src/main/kotlin/comatype/gen (the package name is decided from out; you can also set it with the target's package)
├─ Types.kt Named types (data classes and enum classes)
├─ Init.kt Wrappers, ctx and wrapper statuses
└─ UserPutId.kt One per endpoint (the object name is a PascalCase joined name)Client#
user_put_id.kt
import comatype.ktx.CK
import comatype.gen.Types
import comatype.gen.UserPutId
suspend fun putUser(id: java.util.UUID, user: Types.UserData) {
// Request has params / headers / body, typed from the params block of put__id.comat
val res = CK.put(UserPutId.ENDPOINT, UserPutId.Request(params = UserPutId.Params(id), body = user))
when (res) {
is UserPutId.S200 -> res.body // UserData (res.headers is S200Headers)
is UserPutId.S404 -> res.body // ApiError
else -> {}
}
}- The argument is
Requestif there areparams/search/ headers, the body itself if there is only abody, andcomatype.NoArg.INSTANCEif there is neither. Optional properties have a default (null), so you can use named arguments, as inRequest(params = ...). - The return value is
Response(a sealed interface). Narrow it withwhen. - A status that is not in the
.comatisComatypeException.UnexpectedStatus, and a validation error isValidationException. CK.get/CK.put... are suspend functions. Communication usesHttpClient.sendAsync(it does not block the thread). Wrappers are suspend functions too (CWrap).
init.kt
// Client init file: pass the implemented ComatypeWrap
// The base url defaults to baseUrl in init.comat. Override with the COMATYPE_BASE_URL environment variable or Config.baseUrl
// Call initComatype() once at startup (whether a wrapper is on or off is decided by defaults.comat and `wrap` in each .comat)
import comatype.CookieJar
import comatype.gen.Init
import comatype.ktx.CK
fun initComatype() {
CK.init(
CK.Config(
// With the generated types, a missing wrapper declared in init.comat, or a wrong type, is a compile error
wrappers = Init.Wrappers(auth),
// Used by `cookie on` endpoints. The cookie jar keeps the httpOnly cookie
cookieJar = CookieJar(),
)
)
}wrappers/auth.kt
// Implement a wrapper as a suspend function value (CWrap) (corresponds to the `wrapper auth` of init.comat)
import comatype.ktx.CWrap
val auth: CWrap = { req, next ->
req.headers.set("Authorization", "Bearer " + System.getenv("API_TOKEN"))
next(req)
}Server#
user_put_id.kt
import comatype.S
import comatype.gen.Types
import comatype.gen.UserPutId
import comatype.ktx.coPut
fun S.userPutId() {
// The handler is a suspend function. The returned Reply is used to add a cookie (S.Reply.of(res).withCookie(...))
coPut(UserPutId.ENDPOINT) { req ->
// req.params.id: UUID, req.body: UserData, req.ctx.auth.user (wrap auth is on)
val user = db.user.update(req.params.id, req.body) // the app's own storage
?: return@coPut S.Reply.of<UserPutId.Response>(UserPutId.S404(Types.ApiError("not_found", "user not found")))
// headerRes (X-Resource-Version) of user/defaults.comat
S.Reply.of<UserPutId.Response>(UserPutId.S200(user, UserPutId.S200Headers(xResourceVersion = "v2")))
}
}- Register suspend handlers on
comatype.Swiths.coGet/s.coPut... (synchronous handlers uses.get/s.put). A handler for an endpoint withcookie onreturnsS.Reply<Response>. s.serve("127.0.0.1", 3000)is the JDK server, ands.handle(...)is the entry point for any framework. It uses one thread per request and waits for the handler to finish.
init.kt
// Server init file: pass the implemented ComatypeWrap
// The cookie settings (name, httpOnly, etc.) are in init.comat
import comatype.S
import comatype.gen.Init
import comatype.ktx.init
fun initComatype(s: S) {
// With the generated types, a missing wrapper declared in init.comat, or a wrong type, is a compile error
s.init(Init.Wrappers(auth))
}wrappers/auth.kt
// Implement a wrapper as a suspend function (SWrap<AuthCtx>) value (corresponds to the `wrapper auth` of init.comat)
import comatype.gen.Init
import comatype.gen.Types
import comatype.ktx.SWrap
val auth: SWrap<Init.AuthCtx> = { req, next ->
val user = verifySession(req.cookie("session")) // the app's own logic
if (user == null) {
Init.s401(Types.ApiError("unauthorized", "login required"))
} else {
// On endpoints with `wrap auth` on, a handler can read req.ctx.auth.user, typed
next(req.withCtx(Init.AuthCtx(user)))
}
}Type mapping#
The mapping is the same as Java (UUID LocalDate Instant BigDecimal URI ByteArray; int and int64 are Long). Optional and T | null are T?.
Building#
shell
javac -d rt $(find packages/java/src/main -name '*.java')
kotlinc -cp rt -include-runtime -d app.jar <out> packages/kotlin/src/main/kotlin src/...
java -cp app.jar:rt MainKt