Skip to content

Instantly share code, notes, and snippets.

@PaulWoitaschek
Created April 6, 2023 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulWoitaschek/3441b731a51ab6350dface411c2bfd7c to your computer and use it in GitHub Desktop.
Save PaulWoitaschek/3441b731a51ab6350dface411c2bfd7c to your computer and use it in GitHub Desktop.
private fun generateIosFlow(
declaration: KSDeclaration,
): FunSpec? {
val returnType = when (declaration) {
is KSFunctionDeclaration -> {
declaration.returnType
}
is KSPropertyDeclaration -> {
declaration.getter?.returnType
}
else -> error("Unexpected declaration=$declaration")
} ?: return null
if (!declaration.isPublic()) return null
if (returnType.toString() != "Flow") return null
val classDeclaration = declaration.closestClassDeclaration() ?: return null
if (!classDeclaration.isPublic()) {
return null
}
val resolvedReturnType = returnType.resolve()
if (resolvedReturnType.declaration.qualifiedName?.asString() != "kotlinx.coroutines.flow.Flow") {
return null
}
val flowType = resolvedReturnType.arguments.single().type?.resolve() ?: return null
val nullableFlowType = flowType.isMarkedNullable
val iosFlowName = if (nullableFlowType) {
"NullableIOSFlow"
} else {
"IOSFlow"
}
val flowTypeName = try {
flowType.makeNotNullable().toTypeName()
} catch (e: NoSuchElementException) {
logger.info("Can't parse type", symbol = declaration)
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment