---
source: tests/backends_kotlin_jni_emitter_test.rs
expression: content
---
// Generated by alef. Do not edit by hand.
@file:Suppress(
    "ktlint:standard:max-line-length",
    "ktlint:standard:trailing-comma-on-declaration-site",
    "ktlint:standard:trailing-comma-on-call-site",
    "ktlint:standard:annotation",
    "MaxLineLength",
    "TooManyFunctions",
    "LongParameterList",
    "LongMethod",
)

package dev.sample_crate

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.withContext

@Suppress("TooManyFunctions")
class DefaultClient internal constructor(internal val handle: Long) : AutoCloseable {
    companion object {
        private val MAPPER = com.fasterxml.jackson.databind.ObjectMapper()
            .registerModule(com.fasterxml.jackson.datatype.jdk8.Jdk8Module())
            .findAndRegisterModules()
            .setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE)
    }

    suspend fun doThing(input: String): String {
        return withContext(Dispatchers.IO) { DemoBridge.nativeDefaultClientDoThing(handle, MAPPER.writeValueAsString(input)) }
    }

    @Suppress("TooGenericExceptionCaught")
    fun streamData(req: StreamRequest): kotlinx.coroutines.flow.Flow<DataChunk> = kotlinx.coroutines.flow.callbackFlow {
        val mapper = com.fasterxml.jackson.databind.ObjectMapper()
            .registerModule(com.fasterxml.jackson.datatype.jdk8.Jdk8Module())
            .findAndRegisterModules()
            .setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE)
        val streamHandle: Long = withContext(Dispatchers.IO) {
            DemoBridge.nativeDefaultClientStreamDataStart(handle, mapper.writeValueAsString(req))
        }
        try {
            while (true) {
                val chunkJson: String? = withContext(Dispatchers.IO) {
                    DemoBridge.nativeDefaultClientStreamDataNext(streamHandle)
                }
                if (chunkJson == null) break
                val chunk = mapper.readValue(chunkJson, DataChunk::class.java)
                send(chunk)
            }
            close()
        } catch (e: Throwable) {
            close(e)
        }
        awaitClose {
            DemoBridge.nativeDefaultClientStreamDataFree(streamHandle)
        }
    }
    override fun close() { DemoBridge.nativeFreeDefaultClient(handle) }
}
