diff --git a/frameworks/Kotlin/vertx-web-kotlinx/.gitignore b/frameworks/Kotlin/vertx-web-kotlinx/.gitignore new file mode 100644 index 000000000000..eb713db19674 --- /dev/null +++ b/frameworks/Kotlin/vertx-web-kotlinx/.gitignore @@ -0,0 +1 @@ +.m2 diff --git a/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts b/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts index 2e0035ab3e76..a9eb1c72af03 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts +++ b/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts @@ -10,11 +10,12 @@ plugins { } repositories { + mavenLocal() // TODO mavenCentral() } val vertxVersion = "4.5.10" -val kotlinxSerializationVersion = "1.7.3" +val kotlinxSerializationVersion = "1.7.4-vertx-buffer-SNAPSHOT" // TODO dependencies { implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion")) implementation("io.vertx:vertx-web") @@ -30,6 +31,7 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-io:$kotlinxSerializationVersion") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-vertx:$kotlinxSerializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4") implementation("org.jetbrains.kotlinx:kotlinx-html:0.11.0") diff --git a/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh b/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh new file mode 100755 index 000000000000..e3e3282e0878 --- /dev/null +++ b/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh @@ -0,0 +1,2 @@ +mkdir -p .m2/repository/org/jetbrains/kotlinx/ +cp -r ~/.m2/repository/org/jetbrains/kotlinx/kotlinx-serialization-* .m2/repository/org/jetbrains/kotlinx/ diff --git a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt deleted file mode 100644 index 6427b91e85ef..000000000000 --- a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt +++ /dev/null @@ -1,47 +0,0 @@ -import io.vertx.core.streams.WriteStream -import io.vertx.kotlin.coroutines.coAwait -import kotlinx.coroutines.runBlocking -import kotlinx.io.RawSink -import kotlinx.io.readByteArray -import io.vertx.core.buffer.Buffer as VertxBuffer -import kotlinx.io.Buffer as KotlinxIoBuffer - -@Suppress("NOTHING_TO_INLINE") -private inline fun Long.toIntOrThrow(): Int { - require(this in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) - return toInt() -} - -@JvmInline -value class VertxBufferWriteStreamRawSink(val writeStream: WriteStream) : RawSink { - override fun write(source: KotlinxIoBuffer, byteCount: Long) { - runBlocking { - writeStream.write(VertxBuffer.buffer(source.readByteArray(byteCount.toIntOrThrow()))).coAwait() - } - } - - override fun flush() {} - - override fun close() { - writeStream.end() - } -} - -// not used currently -fun WriteStream.toRawSink(): RawSink = - VertxBufferWriteStreamRawSink(this) - - -@JvmInline -value class VertxBufferRawSink(val vertxBuffer: VertxBuffer) : RawSink { - override fun write(source: KotlinxIoBuffer, byteCount: Long) { - vertxBuffer.appendBytes(source.readByteArray(byteCount.toIntOrThrow())) - } - - override fun flush() {} - - override fun close() {} -} - -fun VertxBuffer.toRawSink(): RawSink = - VertxBufferRawSink(this) diff --git a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt index 2bd701fa0838..77d5bee2e61e 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt +++ b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt @@ -20,12 +20,11 @@ import io.vertx.sqlclient.Tuple import kotlinx.coroutines.Dispatchers import kotlinx.html.* import kotlinx.html.stream.appendHTML -import kotlinx.io.buffered import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.Serializable import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.json.Json -import kotlinx.serialization.json.io.encodeToSink +import kotlinx.serialization.json.vertx.encodeToBuffer import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @@ -134,11 +133,9 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup */ // approach 3 - end(Buffer.buffer().apply { - toRawSink().buffered().use { bufferedSink -> - @OptIn(ExperimentalSerializationApi::class) - Json.encodeToSink(serializer, requestHandler(it), bufferedSink) - } + end(Buffer.buffer().also { buffer -> + @OptIn(ExperimentalSerializationApi::class) + Json.encodeToBuffer(serializer, requestHandler(it), buffer) }) } } diff --git a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile index 17b43ebcb5c3..1e704fc27d01 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile +++ b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile @@ -1,5 +1,7 @@ FROM gradle:8.10.2-jdk21 +# Publish the dependencies to Maven local and run "copy_maven_local.sh" first. +COPY .m2/repository/org/jetbrains/kotlinx /root/.m2/repository/org/jetbrains/kotlinx WORKDIR /vertx-web-kotlinx COPY build.gradle.kts build.gradle.kts COPY settings.gradle.kts settings.gradle.kts diff --git a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile index 4bdc993707dc..07fff6b9a576 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile +++ b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile @@ -1,5 +1,7 @@ FROM gradle:8.10.2-jdk21 +# Publish the dependencies to Maven local and run "copy_maven_local.sh" first. +COPY .m2/repository/org/jetbrains/kotlinx /root/.m2/repository/org/jetbrains/kotlinx WORKDIR /vertx-web-kotlinx COPY build.gradle.kts build.gradle.kts COPY settings.gradle.kts settings.gradle.kts