Dajuan Mcdonald
12/23/2022, 5:59 PMNat Strangerweather
12/26/2022, 5:54 PMsearchJob
every time my search parameter changed in the example below. What happens is that the searchJob
is called successfully the first time I pass my parameter, but never again after that. Could someone please have a look ?Ayfri
12/26/2022, 9:22 PMSam Stone
12/26/2022, 10:56 PMMinh Knitsch
12/27/2022, 2:42 PMAlexandre A Barbosa
12/28/2022, 3:40 PMconst val
for name of the topic. This information comes from a parameter. Even if I assign the value to a new variable I get this issue…
Could you please help me to implement that correctly?Alexandre A Barbosa
12/28/2022, 8:40 PMasavio
12/29/2022, 10:35 AMMap<String, Any?>
to a data class object using Kotlinx Serialisation.
The Data Classes:
import kotlinx.serialization.Serializable
@Serializable
data class Person(
val uuid: String,
val age: Int,
val name: String,
val address: Address
)
@Serializable
data class Address(
val street: String,
val city: String,
val zip: Int
)
This is the function I use to convert to data class.
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.Json
internal inline fun <reified T : Any> Map<String, Any?>.toPojo(): T {
val JSON = Json { encodeDefaults = true }
val jsonObject = JSON.encodeToJsonElement(this).jsonObject
return JSON.decodeFromJsonElement<T>(jsonObject)
}
Here’s the function call:
val map = mapOf(
"uuid" to "agsdjhdg",
"age" to 26,
"name" to "Aseem",
"address" to mapOf(
"city" to "Chennai",
"street" to "street1"
)
)
println(map.toPojo<Person>())
I get the following error:
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:29)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:60)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at kotlinx.serialization.SerializersKt__SerializersKt.serializersForParameters(Serializers.kt:117)
at kotlinx.serialization.SerializersKt.serializersForParameters(Unknown Source)
at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:99)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at MainKt.main(Main.kt:98)
How does one convert a Map<String, Any?> to data class? Thanks in advance!Robert Jaros
12/29/2022, 4:43 PMAminu Babayo Shehu
12/29/2022, 6:47 PMDockerfile
but when I try to upload image and read file I’m getting error of file or directory not found
BUT in reality the directory exist or file do existColton Idle
12/29/2022, 10:02 PM@elizarov
was talking about here that itd be "harmful" to have it?mcpiroman
12/30/2022, 3:58 PMSalman
12/30/2022, 5:02 PMpoohbar
12/30/2022, 6:32 PM.forEach()
on Iterator
but not .map()
. What is the right way to map over an iterator?Devan Lai
12/30/2022, 11:56 PMdf
12/31/2022, 11:49 AMmvn clean package
output, Kotlin file and generated class file, as well as the pom file, can be found here
Any help is highly appreciated 🙂oday
01/02/2023, 12:38 PMwhen (string){
"SomeString" -> { }
}
can i have it somewhere set to ignore case when matching?Sam Stone
01/02/2023, 8:14 PMJ
was written to parse the AST of a KT file(s) (the first Kotlin compiler, written in Java) and produce an executable/binary. Program J
(the binary that Java produced from source code J
) was then used to compile a Kotlin program K
that itself could read KT files (the second Kotlin compiler, written in Kotlin) and produce a binary. The binary produced from K
was used to compile an iteratively better version K1
, which was used to compile K2
, etc.
Somewhat based on this.
Does the current compiler rely on the JVM to run?Tech
01/03/2023, 1:59 AMannotation class Example(val a: String)
@Example("t")
class Foo()
// to
@Example("t")
class Foo(val: a = "t")
nkiesel
01/03/2023, 5:06 AM{a=[1, 3, 4, 5], b=[1, 3, 4, 5]}
instead of the expected {a=[1, 3, 5], b=[4]}
. Looks the 2nd variant re-uses the initial value for all groups. What is a good use case for the 2nd variant?
val g = listOf("a" to listOf(1, 3), "b" to listOf(4), "a" to listOf(5)).groupingBy { it.first }
g.fold({ _, _ -> mutableListOf<Int>() }, { _, acc, item -> acc.addAll(item.second); acc })
g.fold(mutableListOf<Int>(), { acc, item -> acc.addAll(item.second); acc })
y
01/03/2023, 12:39 PMreturn@
considered harmful or unidiomatic?
for example, I want to make a function that sums up the number of values of a list, or returns null
if a certain value is detected. I wrote it as
fun List<Foo>.calcSum() = this.fold(0) { sum, val -> if (something) { sum + val } else { return@calcSum null } }
is this bad style?
I had to add a type hint for the function to even compile.Alexandre A Barbosa
01/03/2023, 2:54 PMlateinit
… should I?
In java I would inject the interface like this
@Autowired MyFeignClient client
and I can use it just like this
client.postMyData()
but please, how can I do this in Kotlin?y
01/04/2023, 4:52 AMplus
overload with Kotlin pattern matching?
sealed class MyType {
data class A(val amount: Int) : MyType()
object B : MyType()
operator fun plus(rhs: MyType) =
when (this to rhs) {
(A, B), (B, A) -> { B }
(A, A) -> { A(this.amount + rhs.amount) }
}
}
note, I’m aware that I can turn this into an if
/`else` :
operator fun plus(rhs: MyType) =
if (this is B || rhs is B) {
B
} else {
val lhsAmount = (this as A).amount
val rhsAmount = (rhs as A).amount
A(lhsAmount + rhsAmount)
}
ank
01/04/2023, 5:23 AMsuspend fun x() {
print("A")
y()
print("C")
}
suspend fun y() {
print("B")
}
Ubed
01/04/2023, 1:05 PMMatti Viljamaa
01/04/2023, 3:51 PMdave08
01/04/2023, 4:05 PMdata class Foo(val id: String, ...)
and one of data class Baz(val id: String, ...)
and output a map of the id against a pair of Foo and Baz that correspond Map<String, Pair<Foo?, Baz?>>
where either could not exist?Matti Viljamaa
01/04/2023, 5:55 PMMatti Viljamaa
01/04/2023, 5:56 PMLandry Norris
01/04/2023, 7:17 PM::something.isInitialized
?