Rohan Maity
04/17/2025, 2:07 PM"kotlinx.coroutines.flow.mapNotNull"
using kotlin poet?
I tried the below it does not add import for the extension function
FileSpec.builder(packageName, factoryClassName)
.addImport("kotlinx.coroutines.flow", "mapNotNull")
hfhbd
04/17/2025, 2:11 PMhfhbd
04/17/2025, 2:11 PMRohan Maity
04/17/2025, 2:12 PMfunBuilder.addStatement("""
return connectionManager
.rawMessageFlow
.mapNotNull { rawJson ->
runCatching { messageAdapter.fromJson(rawJson, $messageType::class.java) }
.onFailure {
// Log exception
}
.getOrNull()
}
""".trimIndent())
how can I use MemberName here?Chrimaeon
04/17/2025, 2:12 PMhfhbd
04/17/2025, 2:13 PMhfhbd
04/17/2025, 2:13 PMRohan Maity
04/17/2025, 2:20 PMRohan Maity
04/17/2025, 2:20 PM.mapNotNull { rawJson ->
runCatching { messageAdapter.fromJson(rawJson, $messageType::class.java) }
.onFailure {
// Log exception
}
.getOrNull()
}
Chrimaeon
04/17/2025, 2:22 PM.%M { rawJson ->
runCatching { messageAdapter.fromJson(rawJson, $messageType::class.java) }
basically its just an ‘intelligent’ string format argument.Rohan Maity
04/17/2025, 2:23 PMhfhbd
04/17/2025, 2:35 PMfunBuilder.addStatement(
"""return connectionManager
.rawMessageFlow
.%M { rawJson ->
runCatching { messageAdapter.fromJson(rawJson, %T::class.java) }
.onFailure {
// Log exception
}
.getOrNull()
}
""".trimIndent(),
MemberName("kotlinx.coroutines.flow", "mapNotNull", isExtension = true),
ClassName("my-package", messageType),
)
Or refactor the code and use beginControlFlow
etc. to minify the String but that's a little bit over-engineered.