Transforming a Flow<Byte> to a Flow<String> in Kotlin - kotlin

Consider I have a cold source of UTF-8 bytes (e. g.: reading a file on disk, or the body of an HTTP response), in a form of a Flow<Byte>. How do I convert the above source to a flow of strings?
In other words, I want the following behaviour:
/*
* A multi-line string, not terminated with a newline character.
*/
val string = """
first line
第二行
третья строка
""".trimIndent()
assertNotEquals('\n', string.last())
assertEquals(2, string.asSequence().count { it == '\n' })
val source: Flow<Byte> = string.toByteArray().asSequence().asFlow()
val transformed: Flow<String> = TODO()
val target = runBlocking {
transformed.toList(mutableListOf()).toTypedArray()
}
assertArrayEquals(
arrayOf("first line", "第二行", "третья строка"),
target
)
As an extra restriction, this is a Kotlin/JS project, so java.io APIs can't be used.

Eventually, I came up with the following solution:
fun Flow<Byte>.decodeToString(): Flow<String> =
flow {
val buffer: MutableList<Byte> = arrayListOf()
collect { value ->
when (value) {
/*
* Ignore.
*/
'\r'.code.toByte() -> Unit
'\n'.code.toByte() -> {
emit(buffer)
buffer.clear()
}
else -> buffer.add(value)
}
}
if (buffer.isNotEmpty()) {
emit(buffer)
}
}
.map(Collection<Byte>::toByteArray)
.map(ByteArray::decodeToString)
The ArrayList<Byte> above can be replaced with either okio.Buffer from okio or kotlinx.io.core.BytePacketBuilder from kotlinx-io, e.g.:
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okio.Buffer
fun Flow<Byte>.decodeToString(): Flow<String> =
flow {
val buffer = Buffer()
collect { value ->
when (value) {
/*
* Ignore.
*/
'\r'.code.toByte() -> Unit
'\n'.code.toByte() -> {
emit(buffer.readUtf8())
buffer.clear()
}
else -> buffer.writeByte(value.toInt())
}
}
if (buffer.size > 0) {
emit(buffer.readUtf8())
}
}

Related

Wait for all volley request in a for loop

In my function, I need to return a list that is populated by a for loop with some Volley Request. So I need to wait that all of these requests to be terminated before return the list.
I think I need the async CoroutineScope to do this work but I don't know how can I wait for all of that response.
This is my code:
suspend fun getListOfAbility(pokemon: Pokemon) : MutableList<Ability> {
val listOfAbility: MutableList<Ability> = emptyList<Ability>() as MutableList<Ability>
CoroutineScope(Dispatchers.IO).launch {
/**
* get the pokemon json
*/
val pokemonJsonObjectRequest = JsonObjectRequest(
Request.Method.GET,
"$pokemonUrl${pokemon.id}",
null,
{
/**
* onResponse
*
* get the list of pokemon abilities
*/
val abilitiesJO = it.getJSONObject("abilities")
val abilityObjectType = object : TypeToken<List<PokemonGson.AbilityObjectGson>>() { }.type
val abilityListGson = Gson().fromJson<List<PokemonGson.AbilityObjectGson>>(abilitiesJO.toString(), abilityObjectType)
/**
* for each ability listed on pokemon info get the full Ability Object
*/
for((index, abilityObjectGson) in abilityListGson.withIndex()) {
val abilityJsonObjectRequest = JsonObjectRequest(
Request.Method.GET,
abilityObjectGson.ability.url,
null,
{
abilityJson ->
/**
* onResponse
*
* get the full ability info
*/
val abilityType = object : TypeToken<AbilityGson>() { }.type
val abilityGson = Gson().fromJson<AbilityGson>(abilityJson.toString(), abilityType)
/**
* fill the Ability entry of listOfAbility with the correct language
*/
val ability = Ability(abilityGson, abilityListGson[index].is_hidden)
listOfAbility.add(ability)
},
{
/**
* onError
*/
Log.d("POKEMON", "Pokemon ability error")
}
)
requestQueue.add(abilityJsonObjectRequest)
}
},
{
/**
* onError
*/
Log.d("POKEMON", "Pokemon request error")
}
)
requestQueue.add(pokemonJsonObjectRequest)
}
//wait
return listOfAbility
}
To use callback-based code in a suspend function, you need to convert it to a suspend function using suspendCoroutine or suspendCancellableCoroutine. So in this case to replace the action of creating a JSONObjectRequest and listener, queuing it to the RequestQueue, and waiting for it somehow, I would create a suspend function like this:
suspend inline fun RequestQueue.getJSONObjectOrNull(
method: Int,
url: String,
jsonRequest: JSONObject?,
crossinline onError: (VolleyError)->Unit = {}
): JSONObject? = suspendCancellableCoroutine { continuation ->
val request = JsonObjectRequest(
method,
url,
jsonRequest,
{ result: JSONObject -> continuation.resume(result) },
{ error ->
onError(error)
continuation.resume(null)
}
)
add(request)
continuation.invokeOnCancellation { request.cancel() }
}
It directly returns the JSONObject result, or null if there's a failure. You can optionally run a callback on errors in case you want to log it.
Then you can use it to write a more sequential version of your function instead of the callback-based version. You can use the pattern of coroutineScope { async { list.map { ... } } }.awaitAll() to convert each item of a list to something else using parallel coroutines.
Here is an untested version of your function. I am having it return an empty list on failure. You could alternatively return null on failure, which might be more useful so the calling function can decide to do something differently when there's a failure.
private fun VolleyError.logDebug() {
Log.d("POKEMON", "Pokemon request error: $this")
}
suspend fun getListOfAbility(pokemon: Pokemon): List<Ability> {
val pokemonJsonObject = requestQueue.getJSONObjectOrNull(Request.Method.GET, "$pokemonUrl${pokemon.id}", null, VolleyError::logDebug)
pokemonJsonObject ?: return emptyList()
val abilitiesJO = pokemonJsonObject.getJSONObject("abilities")
val abilityObjectType = object : TypeToken<List<PokemonGson.AbilityObjectGson>>() {}.type
val abilityListGson: List<Wrapper> = Gson().fromJson<List<PokemonGson.AbilityObjectGson>>(
abilitiesJO.toString(),
abilityObjectType
)
return coroutineScope {
abilityListGson.map {
async {
requestQueue.getJSONObjectOrNull(Request.Method.GET, it.ability.url, null, VolleyError::logDebug)
}
}
}
.awaitAll()
.filterNotNull()
.map { abilityJson ->
val abilityType = object : TypeToken<AbilityGson>() {}.type
val abilityGson = Gson().fromJson<AbilityGson>(abilityJson.toString(), abilityType)
Ability(abilityGson, abilityListGson[index].is_hidden)
}
}

Why is Kotlin's generateSequence returning one too many items in the example below?

I'm calculating the projection of instants in time based on a cron expression and returning them as a Sequence. Here's the class:
// (package omitted)
import org.springframework.scheduling.support.CronExpression
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
class Recurrence(val cronExpression: String) {
private val cron = CronExpression.parse(cronExpression)
fun instants(
fromInclusive: LocalDate = LocalDate.now(),
toExclusive: LocalDate = fromInclusive.plusMonths(1)
): Sequence<LocalDateTime> = instants(fromInclusive.atStartOfDay(), toExclusive.atStartOfDay())
fun instants(
fromInclusive: LocalDateTime = LocalDateTime.now(),
toExclusive: LocalDateTime = fromInclusive.plusMonths(1)
): Sequence<LocalDateTime> {
return generateSequence(cron.next(fromInclusive.minusNanos(1))) {
if (it.isBefore(toExclusive)) {
cron.next(it)
} else {
null
}
}
}
}
The following test fails because the first assertion is false: the returned list has one extra, unexpected element at the end.
// (package omitted)
import java.time.LocalDate
import java.time.Month
import kotlin.test.Test
import kotlin.test.assertEquals
class RecurrenceTest {
#Test
fun testInstants() {
val r = Recurrence("#daily")
val from = LocalDate.of(2021, Month.JANUARY, 1)
val forDays = 31
val instants = r.instants(from, from.plusDays(forDays.toLong())).toList()
assertEquals(forDays, instants.size)
(1..forDays).forEach {
assertEquals(from.plusDays(it.toLong() - 1).atStartOfDay(), instants[it - 1])
}
}
}
If I reimplement by building an ArrayList instead, it works as expected:
// new collection-based methods in Recurrence
fun instantsList(
fromInclusive: LocalDate = LocalDate.now(),
toExclusive: LocalDate = fromInclusive.plusMonths(1)
): List<LocalDateTime> = instantsList(fromInclusive.atStartOfDay(), toExclusive.atStartOfDay())
fun instantsList(
fromInclusive: LocalDateTime = LocalDateTime.now(),
toExclusive: LocalDateTime = fromInclusive.plusMonths(1)
): List<LocalDateTime> {
val list = arrayListOf<LocalDateTime>()
var it = cron.next(fromInclusive.minusNanos(1))
while (it !== null) {
if (it.isBefore(toExclusive)) {
list.add(it)
it = cron.next(it)
} else {
break
}
}
return list
}
The one line to change in the test is to use the new method:
val instants = r.instantsList(from, from.plusDays(forDays.toLong()))
Why is the sequence-based implementation returning me one more element than the list-based one?
If I read your code correctly, in list implementation you check if it.isBefore(toExclusive) and only then you add it to the list. In sequence implementation you do the same check it.isBefore(toExclusive) and then you add next item to the sequence.
Similar with the first item. In list implementation you check if cron.next(fromInclusive.minusNanos(1)) meets the requirement. In sequence implementation you always add it.
Thanks, #broot -- you spotted the issue. Just took another set of eyeballs. Correct sequence implementation is
fun instants(
fromInclusive: LocalDateTime = LocalDateTime.now(),
toExclusive: LocalDateTime = fromInclusive.plusMonths(1)
): Sequence<LocalDateTime> {
val seed = cron.next(fromInclusive.minusNanos(1))
return generateSequence(seed) {
val next = cron.next(it)
if (next.isBefore(toExclusive)) {
next
} else {
null
}
}
}

Writing an addition visitor function in Kotlin

I'm trying to write a visitor function in Kotlin that adds two integers together. I've been working off of some sample code and I can't figure out what these .value or .visit functions are. It doesn't seem to be declared in the sample code, so I'm unsure how to declare it in my code. Whenever I compile the code, I get an error saying that value is an unresolved reference.
Relevant Kotlin code:
package backend
import org.antlr.v4.runtime.*
import grammar.*
abstract class Data
class IntData(val value: Int): Data() {
override fun toString(): String
= "Int($value)"
}
class Context(): HashMap<String, Data>() {
constructor(parent: Context): this() {
this.putAll(parent)
}
}
abstract class Expr {
abstract fun eval(scope: Context): Data
fun run(program: Expr) {
try {
val data = program.eval(Context())
println("=> ${data}")
} catch(e: Exception) {
println("[err] ${e}")
}
}
}
class IntLiteral(val value: Int): Expr() {
override fun eval(scope:Context): Data
= IntData(value)
}
enum class Op {
Add,
Sub,
Mul,
Div
}
class Arithmetic(
val op: Op,
val left: Expr,
val right: Expr): Expr() {
override fun eval(scope: Context): Data {
val x = (left.eval(scope) as IntData).value
val y = (right.eval(scope) as IntData).value
return IntData(
when(op) {
Op.Add -> x + y
Op.Mul -> x * y
Op.Sub -> x - y
Op.Div -> x / y
}
)
}
}
}
class Compiler: PLBaseVisitor<Expr>() {
val scope = mutableMapOf<String, Expr>()
override fun visitAddExpr(ctx: PLParser.AddExprContext): Expr {
val xValue = this.visit(ctx.x)
val yValue = this.visit(ctx.y)
val result = xValue.value + yValue.value
return IntLiteral(result)
}
}
Relevant Antlr Grammar:
expr : x=expr '+' y=expr # addExpr
| x=expr '-' y=expr # subExpr
| x=expr '*' y=expr # mulExpr
| x=expr '/' y=expr # divExpr
;
Code I'm trying to execute:
val test = """
x=1+2
print(x)
"""
fun parse(source: String): PLParser.ProgramContext {
val input = CharStreams.fromString(source)
val lexer = PLLexer(input)
val tokens = CommonTokenStream(lexer)
val parser = PLParser(tokens)
}
val testTree = parse(source1)
val testTree = parse(source1)
fun execute(program: Expr?) {
if(program == null) {
println("Program is null.")
return
}
try {
val data = program.eval(Context())
println("> ${data}")
} catch(e: Exception) {
println("[err] ${e}")
}
}
execute(testProgram)
Code from sample:
data class NodeValue(val value: Int)
val visitor = object: CalcBaseVisitor<NodeValue>() {
override fun visitAddition(ctx: CalcParser.AdditionContext): NodeValue {
val xValue = this.visit(ctx.x)
val yValue = this.visit(ctx.y)
return NodeValue(xValue.value + yValue.value)
}
override fun visitValue(ctx: CalcParser.ValueContext): NodeValue {
val lexeme = ctx.Number().getText()
return NodeValue(lexeme.toInt())
}
}
You don’t show the code for your program.eval() method.
The eval function would need to create an instance of your Visitor. (You’ve done that and called it visitor).
You also have the root expr node in you program variable.
Now you would have your visitor “visit” that node and save the return value:
val nodeVal = visitor.visit(program)
At that point nodeVal.value will have the result of visiting that expression.
note: since you’re doing the evaluation in your visitor, there’s not really any use for your Arithmetic class (unless you refactor your visitor to use it instead of just doing the math, but I don’t see much value in that as the visitor is already pretty easy to read).

Kotlin Linting RuleSetProvider 'trailing-comma'

I been trying to create a Ruleset using Pinterest ktlint Library but I can't remove the part of a children parameter list.
https://github.com/pinterest/ktlint/issues/709
Due to the update on Kotlin to support 'trailing-commas', is breaking all my static code analysis (SonarQube Gradle plugin 2.8). So I decided to create a RuleSetProvider to find a remove from the code this annoying comma ',' at the end of all the parameter list found in the project.
class NoTrailingCommaRule : Rule("no-trailing-comma") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.elementType == ElementType.COMMA) {
node.parents().forEach {
if (it.elementType == ElementType.VALUE_PARAMETER_LIST) {
if (it.text.contains("pepe")) {
println("############# IS PEPE ###############")
println("ParamList-> " + it.text)
println("-------------------------------------")
if (it is PsiParameterList) {
it.parameters.forEach { param ->
println(" -> ${param.text}")
// if (next.elementType == ElementType.COMMA)
// println(" -> comma,")
println("---==---")
}
println("#####################################")
}
}
}
}
}
}
}
/// Sample class to lint
data class PEPE(
val pepe: String,
var pepe1: List<String> = emptyList(), //<- This is the kind of comma I want to remove
)
Thats my current attempt of trying to get the comma and replace, but when I'm able to print the parameter line the comma is not there. 😿
See rule below for which I have sent a PR:
package com.pinterest.ktlint.ruleset.experimental
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.children
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class NoTrailingCommaRule : Rule("no-trailing-comma") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.elementType == ElementType.VALUE_ARGUMENT_LIST || node.elementType == ElementType.VALUE_PARAMETER_LIST) {
val lastNode = node
.children()
.filter { it.elementType != ElementType.WHITE_SPACE }
.filter { it.elementType != ElementType.EOL_COMMENT }
.filter { it.elementType != ElementType.RPAR }
.last()
if (lastNode.elementType == ElementType.COMMA) {
emit(lastNode.psi.endOffset - 1, "Trailing command in argument list is redundant", true)
if (autoCorrect) {
node.removeChild(lastNode)
}
}
}
}
}
and the tests:
package com.pinterest.ktlint.ruleset.experimental
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.test.format
import com.pinterest.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
class NoTrailingCommaRuleTest {
#Test
fun testFormatIsCorrectWithArgumentList() {
val code =
"""
val list1 = listOf("a", "b",)
val list2 = listOf(
"a",
"b", // The comma before the comment should be removed without removing the comment itself
)
""".trimIndent()
val autoCorrectedCode =
"""
val list1 = listOf("a", "b")
val list2 = listOf(
"a",
"b" // The comma before the comment should be removed without removing the comment itself
)
""".trimIndent()
assertThat(NoTrailingCommaRule().lint(code)).isEqualTo(
listOf(
LintError(line = 1, col = 28, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"),
LintError(line = 4, col = 8, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"),
)
)
assertThat(NoTrailingCommaRule().format(code))
.isEqualTo(autoCorrectedCode)
}
#Test
fun testFormatIsCorrectWithValueList() {
val code =
"""
data class Foo1(
val bar: Int, // The comma before the comment should be removed without removing the comment itself
)
data class Foo2(val bar: Int,)
""".trimIndent()
val autoCorrectedCode =
"""
data class Foo1(
val bar: Int // The comma before the comment should be removed without removing the comment itself
)
data class Foo2(val bar: Int)
""".trimIndent()
assertThat(NoTrailingCommaRule().lint(code)).isEqualTo(
listOf(
LintError(line = 2, col = 16, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"),
LintError(line = 4, col = 29, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"),
)
)
assertThat(NoTrailingCommaRule().format(code))
.isEqualTo(autoCorrectedCode)
}
}

Cast away nested Option in Kotlin arrow

I have a value with below type in my data class
Option<Option<List<Pair<String, Option<String>>>>>
How would I access the right-most Option<Sring>. I have tried with when expression like below
when(Option<Option<List<Pair<String, Option<String>>>>>) {
is Some -> when(Option<Option<List<Pair<String, Option<String>>>>>.t) {
is Some -> when(Option<List<Pair<String, Option<String>>>>.t) {
......
but it's not looking good. Is there any other way to cast away those Options
val t: Option<Option<List<Pair<String, Option<String>>>>> =
Some(Some(listOf(
Pair("a", Some("A")),
Pair("b", Some("B")),
Pair("c", None)
)))
val l = t.flatten()
.map { list ->
list.map { pair -> pair.second }
}
.getOrElse { emptyList() }
val first = l.getOrElse(0) { None }
val second = l.getOrElse(1) { None }
val missing = l.getOrElse(7) { None }
nested // Option<Option<List<Pair<String, Option<String>>>>>
.flatten() // Option<List<Pair<String, Option<String>>>>
.map { it.map { it.second() } } // Option<List<Option<String>>>
.sequence(Option.applicative()) // Option<Option<List<String>>>
.flatten() // Option<List<String>>
flatten gets rid of nested options, and sequence goes from List<Option<A>> to Option<List<A>>.