Skip to content

Instantly share code, notes, and snippets.

@dilrajsingh1997
Last active Nov 21, 2021
Embed
What would you like to do?
file addLine "fun parseArguments(backStackEntry: NavBackStackEntry): $dataClassName {"
tabs++
file addLine "return "
file addPhrase "$dataClassName("
tabs++
properties.forEach { property ->
val argumentName = property.simpleName.asString()
val resolvedType: KSType = property.type.resolve()
fun getParsedElement(): String {
return try {
when (resolver.getClassDeclarationByName(resolvedType.declaration.qualifiedName!!)
?.toString()) {
"Boolean" -> "backStackEntry.arguments?.getBoolean(\"$argumentName\") ?: false
"String" -> "backStackEntry.arguments?.getString(\"$argumentName\") ?: \"\""
"Float" -> "backStackEntry.arguments?.getFloat(\"$argumentName\") ?: 0F"
"Int" -> "backStackEntry.arguments?.getInt(\"$argumentName\") ?: 0"
"Long" -> "backStackEntry.arguments?.getLong(\"$argumentName\") ?: 0L"
else -> {
logger.error("complex data types not yet supported", property)
""
}
}
} catch (e: Exception) {
""
}
}
file addLine "$argumentName = ${getParsedElement()}"
file addPhrase ", "
}
tabs--
file addLine ")"
tabs--
file addLine "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment